LED hangs up

Hey All,

I started working on a simple thermostat the other day using the TMP36, and I have it working pretty well. Today I decided to add a third LED to show the state between the "Heat ON" and Cool On" states. I called this "All OK", and it's been everything but since then. For some reason it hangs up, leaving the LED on while the thermostat goes on about it's buisness. I tried this every way I could think of, and it stiill hangs up. I'm pretty new at most of this, so please excuse my beginners code.

Thanks for any help you might give,

Bob

// Arduino Thermostat using the TMP36 temp sensor chip
// This code uses a rolling average to smooth the output of the TMP36
// to give accurate temperature control by turning on fans or a heating bulb 
// to keep a constant temperature range. Three LEDs show the state of the heat
// and cool relays, as well as the "all OK" state.

const int numReadings = 25;     // set the number of reading to average
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int inputPin = A0;              // set the input pin for the TMP36 to Analog pin 0
int sensorValue = 0;            // variable to store the value coming from the sensor
int setTemp = 77;               // set desired temp to 77F 
int heat_relay = 4;             // set heat relay trigger pin to digital pin 4
int cool_relay = 8;             // set cool relay trigger pin to digital pin 8
int allOK = 7;                  // set all OK led pin to digital pin 7
float voltage = 0;              //  variable for raw temp reading from TMP36
float average = 0;              //  variable coverted to millivolts
float tempC = 0;                //  Varible for millivolts converted to degrees Celsius
float tempF = 0;                //  variable for degrees Celsius converted to degrees Fahrenheit
void setup()
{
for (int thisReading = 0; thisReading < numReadings; thisReading++)
 readings[thisReading] = 0;     // initialize all the readings to 0: 
pinMode(heat_relay, OUTPUT);    // Set heat relay pin to output
digitalWrite(heat_relay, LOW);  // Turn it off - High to turn it on
pinMode(cool_relay, OUTPUT);    // Set cool relay pin to output
digitalWrite(cool_relay, LOW);  // Turn it off - HIGH to turn it on
pinMode(allOK, OUTPUT);         // Set all ok pin to output
digitalWrite(allOK, LOW);       // Turn it off - HIGH to turn it on
Serial.begin(9600);             // initialize serial communication with computer

}

void loop()
{
   total= total - readings[index];         // subtract the last reading:     
   readings[index] = analogRead(inputPin); // read from the sensor: 
   total= total + readings[index];         // add the reading to the total:    
   index = index + 1;                      // advance to the next position in the array:                
   Serial.print(readings[numReadings]); Serial.println(" numReadings ");
  if (index >= numReadings)                // if we're at the end of the array...      
    index = 0;                             // ...wrap around to the beginning:                    

  // calculate the average:
  average = total / numReadings;          // calculate the average:      
  voltage = (average*5000)/1024;          // convert raw sensor value to millivolts
  voltage = voltage-500;                  // remove voltage offset
  tempC = voltage/10;                     // convert millivolts to Celsius
  tempF = ((tempC * 1.8)+32);             // convert celsius to Fahrenheit
  Serial.print(tempF); Serial.println(" degrees F");
  //Serial.println(tempC);                // send the temp in Celsius to the computer (as ASCII digits)
  //Serial.println(tempF);                // send the temp in Farenheit to the computer (as ASCII digits)
  
if (tempF < (setTemp - 8))                // if the current temp is 8 degrees lower than the setpoint
{
  digitalWrite(heat_relay, HIGH);         // turn on the heater relay
}
else
{
  digitalWrite(heat_relay, LOW);         // otherwise, turn the heater relay off
}

if (tempF  > (setTemp + 8))             // if the current temp is 8 degrees higher than the set point
{
  digitalWrite (cool_relay, HIGH);      // turn the cool relay on 
}
else
{
  digitalWrite (cool_relay, LOW);      // otherwise, turn the cool relay off 
}

if ((tempF <= (setTemp + 8)) && (tempF >= (setTemp - 8))) // if the temp is less than 8 degrees above the set point
{                                                         // and less than 8 degrees below the setpoint
    digitalWrite (allOK, HIGH);                           // turn on the all OK LED
}
else 
{
    digitalWrite, (allOK, LOW);                          // otherwise, turn the all OK LED off
}
  
delay(300);                                           // delay in between reads for stability 
}
digitalWrite, (allOK, LOW);

Extra "," could that be the problem?

cyclegadget:
Extra "," could that be the problem?

Yes, that would do it.

While comments are good they can also be made unnecessary by using good constant and variable names. What constitutes "good" symbol naming will vary from person to person and organization to organization.

Also the use of constants can reduce the memory requirements (sometimes considerably).

// Arduino Thermostat using the TMP36 temp sensor chip
// This code uses a rolling average to smooth the output of the TMP36 to give
// accurate temperature control by turning on fans or a heating bulb to keep a
// constant temperature range. Three LEDs show the state of the heat and cool
// relays, as well as the "all OK" state.

// --- GLOBAL CONSTANTS

const uint8_t   pinSENSOR_THERM = A0;       // set the input pin for the TMP36 to Analog pin 0
const uint8_t   pinRELAY_HEAT   =  4;       // set heat relay trigger pin to digital pin 4
const uint8_t   pinRELAY_COOL   =  8;       // set cool relay trigger pin to digital pin 8
const uint8_t   pinLED_OK       =  7;       // set all OK led pin to digital pin 7

const uint8_t   RELAY_OFF       = LOW;
const uint8_t   RELAY_ON        = HIGH;

const uint8_t   LED_OFF         = LOW;
const uint8_t   LED_ON          = HIGH;

const int       setTemp         = 77;       // set desired temp to 77F
const int       numReadings     = 25;       // set the number of reading to average


// --- GLOBAL VARIABLES

//     (COMPLIER WILL ZERO ALL GLOBAL VARIABLES WELL BEFORE 'SETUP' IS CALLLED)

int             readings[numReadings];      // the readings from the analog input
int             index;                      // the index of the current reading
int             total;                      // the running total
float           voltage;                    //  variable for raw temp reading from TMP36
float           average;                    //  variable coverted to millivolts
float           tempC;                      //  Varible for millivolts converted to degrees Celsius
float           tempF;                      //  variable for degrees Celsius converted to degrees Fahrenheit

void setup()
{
    Serial.begin(9600);

    pinMode(pinRELAY_HEAT, OUTPUT);
    digitalWrite(pinRELAY_HEAT, RELAY_OFF);

    pinMode(pinRELAY_COOL, OUTPUT);
    digitalWrite(pinRELAY_COOL, RELAY_OFF);

    pinMode(pinLED_OK, OUTPUT);
    digitalWrite(pinLED_OK, LED_OFF);
}

void loop()
{
    total = total - readings[index];        // subtract the last reading:
    readings[index] = analogRead(inputPin); // read from the sensor:
    total = total + readings[index];        // add the reading to the total:
    index = index + 1;                      // advance to the next position in the array:

    Serial.print(readings[numReadings]); Serial.println(" numReadings ");

    if ( index >= numReadings )             // if we're at the end of the array...
    {
        index = 0;                          // ...wrap around to the beginning:
    }


    // calculate the average:
    average = total / numReadings;          // calculate the average:
    voltage = (average * 5000) / 1024;      // convert raw sensor value to millivolts
    voltage = voltage - 500;                // remove voltage offset
    tempC   = voltage / 10;                 // convert millivolts to Celsius
    tempF   = ((tempC * 1.8) + 32);         // convert celsius to Fahrenheit

    Serial.print(tempF); Serial.println(" degrees F");
//  Serial.println(tempC);                  // send the temp in Celsius to the computer (as ASCII digits)
//  Serial.println(tempF);                  // send the temp in Farenheit to the computer (as ASCII digits)


    // if the current temp is 8 degrees lower than the setpoint
    digitalWrite(pinRELAY_HEAT, ((tempF < (setTemp - 8)) ? (RELAY_ON : RELAY_OFF)));


    // if the current temp is 8 degrees higher than the set point
    digitalWrite(pinRELAY_COOL, ((tempF  > (setTemp + 8)) ? RELAY_ON : RELAY_OFF))


    // if the temp is less than 8 degrees above the set point and less than 8
    // degrees below the setpoint
    digitalWrite(pinLED_OK, (((tempF <= (setTemp + 8)) && (tempF >= (setTemp - 8)) ) ? LED_ON : LED_OFF));

    // delay in between reads for stability

    delay(300);
}

Man, that darn comma......

Thanks guys, I looked at that code for over an hour and couldn't see the forrest through the trees.

Appriciate the help!

Bob