First reading sensor missing
dear master,
i have project read temperature max6675 , thermocouple pid, convert value sensor integer , send c# app via usb port, on c# first convert error because first temp reading result in arduino empty, happen on code?
here code
i have project read temperature max6675 , thermocouple pid, convert value sensor integer , send c# app via usb port, on c# first convert error because first temp reading result in arduino empty, happen on code?
here code
code: [select]
#include <pid_v1.h>
#include <max6675.h>
#define 8 // miso
#define sck 10 // serial clock
#define tc_0 9 // cs pin of max6607
#define relaypin 2
// default kp, ki, & kd values
#define kp_def 850
#define ki_def 0.5
#define kd_def 0.1
unsigned long serialtime; //this know when talk processing
char incomingoption;
int tc_0_calib = 0; // calibration compensation value in digital counts (.25[ch730]c)
double setpoint, input, output;
// pid tuning parameters
double kp = kp_def;
double ki = ki_def;
double kd = kd_def;
int intinput, intsetpoint, intoutput, intkp, intki, intkd;
pid mypid(&input, &output, &setpoint, kp, ki, kd, direct);
int windowsize = 5000; //default 5000
unsigned long windowstarttime;
void setup() {
windowstarttime = millis();
setpoint = 70;
//tell pid range between 0 , full window size
mypid.setoutputlimits(0, windowsize);
//turn pid on
mypid.setmode(automatic);
pinmode(so, input);
pinmode(sck, output);
pinmode(tc_0, output);
pinmode(relaypin,output);
digitalwrite(tc_0,high); // disable device
serial.begin(115200);
}
unsigned int read_temp(int pin, int type, int error, int samples) {
unsigned int value = 0;
int error_tc;
float temp;
unsigned int temp_out;
for (int i=samples; i>0; i--){
digitalwrite(pin,low); // enable device
/* cycle clock dummy bit 15 */
digitalwrite(sck,high);
digitalwrite(sck,low);
/* read bits 14-3 max6675 temp
loop each bit reading value ,
storing final value in 'temp'
*/
for (int i=11; i>=0; i--){
digitalwrite(sck,high); // set clock high
value += digitalread(so) << i; // read data , add our variable
digitalwrite(sck,low); // set clock low
}
/* read tc input inp check tc errors */
digitalwrite(sck,high); // set clock high
error_tc = digitalread(so); // read data
digitalwrite(sck,low); // set clock low
digitalwrite(pin, high); //disable device
}
value = value/samples; // divide value number of samples average
value = value + error; // insert calibration error value
if(type == 0) { // request temp in [ch730]f
temp = ((value*0.25) * (9.0/5.0)) + 32.0; // convert value [ch730]f (ensure proper floats!)
}
else if(type == 1) { // request temp in [ch730]c
temp = (value*0.25); // multiply value 25 temp in [ch730]c
}
temp_out = temp; // send float int (x10) ease of printing.
/* output 9999 if there tc error, otherwise return 'temp' */
if(error_tc != 0) {
return 9999;
}
else {
return temp_out;
}
}
void loop() {
intsetpoint = (int)setpoint;
input = read_temp(tc_0,1,tc_0_calib,10);
mypid.compute();
serial.print(read_temp(tc_0,1,tc_0_calib,10));
serial.print(",");
serial.print(intsetpoint);
serial.print(",");
/************************************************
* turn output pin on/off based on pid output
************************************************/
unsigned long = millis();
if(now - windowstarttime>windowsize)
{ //time shift relay window
windowstarttime += windowsize;
}
if(output > - windowstarttime)
{
serial.print("on");
serial.println(",");
digitalwrite(relaypin,high);
}
else
{
serial.print("off");
serial.println(",");
digitalwrite(relaypin,low);
}
//serialreceive();
serialack();
delay(1000);
}
/********************************************
* serial communication functions / helpers
********************************************/
union { // data structure lets
byte asbytes[24]; // take byte array
float asfloat[6]; // sent processing and
} // convert a
foo; // float array
string getvalue(string data, char separator, int index)
{
int found = 0;
int strindex[] = {
0, -1 };
int maxindex = data.length()-1;
for(int i=0; i<=maxindex && found<=index; i++){
if(data.charat(i)==separator || i==maxindex){
found++;
strindex[0] = strindex[1]+1;
strindex[1] = (i == maxindex) ? i+1 : i;
}
}
return found>index ? data.substring(strindex[0], strindex[1]) : "";
}
void serialack(){
string str;
int index=0;
if (serial.available() && index == 0) {
str = serial.readstringuntil('\n');
if (getvalue(str, ' ', 0).tofloat() != 0){
setpoint = getvalue(str, ' ', 0).tofloat();
}
index++;
}
}
dear master,yes, slave?
what happen on code?that interesting question considering have not posted code (in tags). aren't mind readers.
Arduino Forum > Using Arduino > Sensors > First reading sensor missing
arduino
Comments
Post a Comment