Serial does not display data after a short time

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

I AM AN IDIOT! … My problem lies with millis() - x == 200 … the dang double = gets me again… I need to set it to > not to ==. Because I may skip over the 200 millisecond time where it compares and sends out the code. This also helps me realize that the drive is indeed maintaining the temperature.

SORRY!

Variables associated with millis() should be defined as unsigned long - like this, Also, referring to millis() when defining the variables is meaningless because millis() does not have a value at compile time.

unsigned long x;
unsigned long y;
unsigned long z;

Single character variable names are also bad practice IMHO. It is difficult or impossible to find them with the search tool and it is too easy to forget what they represent. Using meaningful names for variables and functions will make the code self-documenting and more easily show up logical errors.

...R

Thank you so much. I am a mechanical engineer by trade. Don't really get all this computer stuff :). I just know how to hit it with a hammer.

rickster434:
I just know how to hit it with a hammer.

I'm always surprised at how often that produces results :slight_smile:

...R