Dear Master,
I have project to read temperature with max6675 and thermocouple with PID, Convert the value sensor to integer and then send to my C# app via usb port, but on my c# the first convert was error because the first temp reading result in the arduino is empty, what happen on my code?
Here my code
#include <PID_v1.h>
#include <max6675.h>
#define SO 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 will help us know when to talk with 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 is 5000
unsigned long windowStartTime;
void setup() {
windowStartTime = millis();
Setpoint = 70;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the 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 the clock for dummy bit 15 */
digitalWrite(SCK,HIGH);
digitalWrite(SCK,LOW);
/* Read bits 14-3 from MAX6675 for the Temp
Loop for each bit reading the value and
storing the final value in 'temp'
*/
for (int i=11; i>=0; i--){
digitalWrite(SCK,HIGH); // Set Clock to HIGH
value += digitalRead(SO) << i; // Read data and add it to our variable
digitalWrite(SCK,LOW); // Set Clock to LOW
}
/* Read the TC Input inp to check for TC Errors */
digitalWrite(SCK,HIGH); // Set Clock to HIGH
error_tc = digitalRead(SO); // Read data
digitalWrite(SCK,LOW); // Set Clock to LOW
digitalWrite(pin, HIGH); //Disable Device
}
value = value/samples; // Divide the value by the number of samples to get the average
value = value + error; // Insert the calibration error value
if(type == 0) { // Request temp in [ch730]F
temp = ((value*0.25) * (9.0/5.0)) + 32.0; // Convert value to [ch730]F (ensure proper floats!)
}
else if(type == 1) { // Request temp in [ch730]C
temp = (value*0.25); // Multiply the value by 25 to get temp in [ch730]C
}
temp_out = temp; // Send the float to an int (X10) for ease of printing.
/* Output 9999 if there is a 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 the output pin on/off based on pid output
************************************************/
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output > now - 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 { // This Data structure lets
byte asBytes[24]; // us take the byte array
float asFloat[6]; // sent from processing and
} // easily convert it to 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++;
}
}