So, I am trying to control a heater using a thermocouple. I have a AD495 thermocouple chip and a cheap driver that supports about 13 amps. I send a command to turn the heater on (at 14% duty), read the thermocouple output and turn it off when it reaches my input temperature. Obviously, this loops and tries to maintain temp until I send a command for a lower or higher temp.
My problem is that the serial output will just stop coming through. I can send Tset values and it will display that it received them, but it will stop displaying the current temperature and heater status. I don't understand if this is some kind of buffer issue or if I need to be clearing the serial port or something like that. It will just stop about 1 - 2 minutes in. (Thankfully it shuts down(or maybe maintains) the heater driver or I would have a big issue).
Please look over the code and see if you can figure out why my serial output hangs up.
/*
PinOUT:
Pin A0 - OUT ON AD8495
Pin 3 - RPWM LD8938
Pin 2 - RENABLE LD8938
Pin 4 - LENABLE LD8938
Connect AD8495 power and ground
Connect LD8938 power and ground for both PWM side and Power side
*/
#define VoReadAna A0
#define heatPow 3
#define heatPowEN 2
#define heatPowEN_2 4
float T = 0;
float Vo = 0;
float Tset = 0;
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
int x = millis();
int y = millis();
int z = millis();
void setup() {
Serial.begin(115200);
pinMode(VoReadAna, INPUT);
pinMode(heatPow, OUTPUT);
pinMode(heatPowEN, OUTPUT);
pinMode(heatPowEN_2, OUTPUT);
digitalWrite(heatPowEN, HIGH);
digitalWrite(heatPowEN_2, HIGH);
digitalWrite(heatPow, LOW);
Vo = analogRead(VoReadAna);
T = (Vo - 1250) / 5;
}
void loop() {
recvWithStartEndMarkers();
showNewData();
Tset = atof(receivedChars);
Vo = analogRead(VoReadAna);
Vo = Vo * (5.0 / 1023.0);
T = (Vo - 1.25) / .005; //mv
if (Tset >= T) {
analogWrite(heatPow, 35);
if (millis() - x == 200) {
Serial.println("HEATER ON");
x = millis();
}
}
else {
digitalWrite(heatPow, LOW);
if (millis() - y == 200) {
Serial.println("HEATER OFF");
y = millis();
}
}
if (millis() - z == 200) {
Serial.print("Temp is: ");
Serial.println(T);
z = millis();
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("Tset Received: ");
Serial.println(receivedChars);
newData = false;
}
}
Thank you,
Ricky