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
}