Heater-Chiller Control

I’m new to arduino and am trying to put together a Heating and cooling system for and aquarium and I made a code and wanted to see if someone would please look it over and suggest any changes to the code or point out any problems. My main question is with the temperature settings, will they conflict or not, also will the chiller not turn on until the water reaches 79 or will it cycle on at anything >77? Here is the sketch below:

float tempC;

float tempF;

int tempPin = 0;

int ledPin = 13;

int ledPin2 = 12;

int relayPin = 11;

int relayPin2 = 10;

void setup()

{

Serial.begin(9600); //opens serial port, sets data rate to 9600 bps

pinMode (relayPin, OUTPUT);

pinMode (relayPin2, OUTPUT);

}

void loop()

{

tempC = analogRead(tempPin); //read the value from the sensor

tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature

tempF = (tempC * 9)/ 5 + 32; // converts to fahrenheit

Serial.print ((int) tempC ); //send the data to the computer

Serial.print(" Celsius, ");

Serial.print ((int) tempF ); //send the data to the computer

Serial.print(" Fahrenheit, → ");

delay(1500); //wait 1.5 second before sending new data

digitalWrite(ledPin, HIGH); // sets the LED on

digitalWrite(ledPin2, HIGH);// sets LED 2 on

if (tempF > 79) digitalWrite(ledPin, HIGH); //sets chiller LED on

if (tempF > 79) digitalWrite(relayPin, HIGH); //Powers up chiller unit

if (tempF > 79) Serial.println (“Chiller On”);

if (tempF <= 77) digitalWrite(ledPin, LOW); //Turns off chiller LED

if (tempF <= 77) digitalWrite(relayPin, LOW); //Turn off power to unit

if (tempF <= 77) Serial.println (“Chiller Off”);

if (tempF <= 76) digitalWrite(ledPin2, HIGH); //turn heater LED on

if (tempF <= 76) digitalWrite(relayPin2, HIGH); //Turn Heater power on

if (tempF <= 76) Serial.println (“Heater On”);

if (tempF >= 77) digitalWrite(ledPin2, LOW); //Turn Heater LED off

if (tempF >= 77) digitalWrite(relayPin2, LOW); //Turn Heater power off

if (tempF >= 77) Serial.println (“Heater Off”);

}

Each time through the main loop, you set the LEDs high regardless of whether the attached widget is turned on. This probably isn’t what you want. I’d get rid of those lines:

digitalWrite(ledPin, HIGH); // sets the LED on
digitalWrite(ledPin2, HIGH);// sets LED 2 on

The code is pretty hard to read with all of those if statements. Let’s modify it a bit:

if (tempF > 79)
  {
    digitalWrite(ledPin, HIGH); //sets chiller LED on
    digitalWrite(relayPin, HIGH); //Powers up chiller unit
    Serial.println ("Chiller On");
  } 
else if (tempF <= 77) 
  {
    digitalWrite(ledPin, LOW); //Turns off chiller LED
    digitalWrite(relayPin, LOW); //Turn off power to unit
    Serial.println ("Chiller Off");
  }

if (tempF <= 76)
  {
    digitalWrite(ledPin2, HIGH); //turn heater LED on
    digitalWrite(relayPin2, HIGH); //Turn Heater power on
    Serial.println ("Heater On");
  }
else if (tempF >= 77)
  {
    digitalWrite(ledPin2, LOW); //Turn Heater LED off
    digitalWrite(relayPin2, LOW); //Turn Heater power off
    Serial.println ("Heater Off");
  }

Looking at it that way, the chiller is on at 79.0, off at 77.0, with a dead band in between. The heater is on at 76.0, off at 77.0, with a dead band in between. Looks like there’s no conflict.

Wow that makes it much more simple, so the new segment just replaces the original if statements right? And when you say there is a dead band in between do you mean the heater and chiller are only on at the specified temperatures or will they come on at the set point and run until the cut off temperature is reached or is there a seperate text needed to allow this to happen? Also is this how the code should be after the changes?

float tempC;

float tempF;

int tempPin = 0;

int ledPin = 13;

int ledPin2 = 12;

int relayPin = 11;

int relayPin2 = 10;

void setup()

{

Serial.begin(9600); //opens serial port, sets data rate to 9600 bps

pinMode (relayPin, OUTPUT);

pinMode (relayPin2, OUTPUT);

}

void loop()

{

tempC = analogRead(tempPin); //read the value from the sensor

tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature

tempF = (tempC * 9)/ 5 + 32; // converts to fahrenheit

Serial.print ((int) tempC ); //send the data to the computer

Serial.print(" Celsius, ");

Serial.print ((int) tempF ); //send the data to the computer

Serial.print(" Fahrenheit, → ");

delay(1500); //wait 1.5 second before sending new data

if (tempF > 79)

{

digitalWrite(ledPin, HIGH); //sets chiller LED on

digitalWrite(relayPin, HIGH); //Powers up chiller unit

Serial.println (“Chiller On”);

}

else if (tempF <= 77)

{

digitalWrite(ledPin, LOW); //Turns off chiller LED

digitalWrite(relayPin, LOW); //Turn off power to unit

Serial.println (“Chiller Off”);

}

if (tempF <= 76)

{

digitalWrite(ledPin2, HIGH); //turn heater LED on

digitalWrite(relayPin2, HIGH); //Turn Heater power on

Serial.println (“Heater On”);

}

else if (tempF >= 77)

{

digitalWrite(ledPin2, LOW); //Turn Heater LED off

digitalWrite(relayPin2, LOW); //Turn Heater power off

Serial.println (“Heater Off”);

}

} // This one should make it loop the entire program right?

scott333:
Wow that makes it much more simple, so the new segment just replaces the original if statements right?

Exactly. The code you posted looks like what I had in mind. On next-to-last line, the } ends the block of statements that gets run if the temp is >= 77, the } on the last line ends the entire loop() function.

In the future, when you post code, surround it with ```


<QUOTE>

> And when you say there is a dead band in between do you mean the heater and chiller [...] come on at the set point and run until the cut off temperature is reached

</QUOTE>

Right. That's what it does the way it's coded now, and that seems like a reasonable way for them to work. My home thermostat has a 1 degree deadband. It's set at 64, so it comes on when the temperature drops to 63, and stays on until the temperature reaches 65. Once it has reached 65, it shuts off and won't turn on again until it drops to 63. It would be coded like:

if (tempF >= 65)
HeaterOff();
else if (tempF < 64)
HeaterOn();
else
/* keep doing whatever we were doing */;


This deadband keeps your equipment from switching on and off every two seconds -- that would probably kill your chiller pretty quickly, and it's pretty hard on mechanical relays too (and the people who have to listen to them). If you were using a solid state relay to switch your heater, you could probably do with no deadband. This would try very hard to keep the temperature at exactly 76 (instead of somewhere between 76.0 and 76.99):

if (tempF > 76)
HeaterOff();
else
HeaterOn();

Ok thanks for all the help, the current setup is good and hopefully everything should go smoothly now.