Hey, this is a fairly straight forward code which samples a temperature reading and turns relays on and off which control heat, ac, and fan.
Im new to writing code and I need some help with my code as follows.....Rather than having all the "if" statements within the loop to control the hvac equipment, is it possible to have other functions "do the work" which are called from the loop? Delays are needed for fan control during heating and it's delaying the entire code downstream of the delay.
example>>>>
heatControl()
turn heat on below a setpoint
delay
turn fan on
acControl()
turn ac on below a setpoint
void loop()
if heatControl()
turn heat and fan on
I hope thats clear as mud! hah XD
I'm sure once I see somthing written in my code I could learn alot and use this ability to add saftey functions (if blank happens dont allow the fan to come on) etc. Eventually I'm going to add zones with dampers and there will be a need for an end switch which closes when the damper fully opens, then allowing the fan to turn on. for example.
Any help is much appreciated
Chris
#include <math.h>
//------------------------------------------------------------------------------------------------------------------------------------
long HeatAutoOn = 18;
long AcAutoOn = 25;
long CheckTemp = 20000;
float TempDiffHeat = HeatAutoOn+1.5;
float TempDiffAc = AcAutoOn-1.5;
//--------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------
unsigned long time;
long previoustime;
int w = 7;
int g = 9;
int y = 8;
boolean heat = false;
boolean ac = false;
boolean off = false;
//----------------------------------------------------------------------------------------------------------------------------------------------
void setup(){
pinMode (w, OUTPUT);
pinMode (g, OUTPUT);
pinMode (y, OUTPUT);
Serial.begin(9600);
}
//THERMISTER CALCULATIONS FOR TEMPERATURE CONVERSION----------------------------------------------------------------------------------------------
double Thermister(int RawADC)
{
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
Serial.print("Degrees C");
Serial.println(int(Thermister(analogRead(0)))); // display C
delay(1000);
int command = Serial.read();
if(command == '1')
{
digitalWrite(w, HIGH);
Serial.println("MANUAL HEAT ON");
delay(4000);
digitalWrite(g, HIGH);
}
if (command == '2')
{
digitalWrite(w, LOW);
Serial.println("MANUAL HEAT OFF");
delay(4000);
digitalWrite(g, LOW);
}
if(command == '3')
{
digitalWrite(y, HIGH);
digitalWrite(g, HIGH);
Serial.println("MANUAL A/C ON");
}
if (command == '4')
{
digitalWrite(y, LOW);
digitalWrite(g, LOW);
Serial.println("MANUAL A/C OFF");
}
if(command == '5')
{
digitalWrite(g, HIGH);
Serial.println("MANUAL FAN ON");
}
if (command == '6')
{
digitalWrite(g, LOW);
Serial.println("MANUAL FAN OFF");
}
if ((int(Thermister(analogRead(0)))) >= TempDiffHeat && (int(Thermister(analogRead(0)))) <= TempDiffAc && off == false){ //turns HVAC off
digitalWrite(w, LOW);
digitalWrite(y, LOW);
Serial.println("AUTO HVAC SATISFIED");
delay(5000);
digitalWrite(g, LOW);
off = true;
heat = false;
ac = false;
}
time = millis();
if ((time - previoustime) > CheckTemp ){
previoustime = time;
if ((int(Thermister(analogRead(0)))) <= HeatAutoOn && heat == false) { //turns heat on
digitalWrite(w, HIGH);
Serial.println("AUTO HEAT ON");
delay(4000);
digitalWrite(g, HIGH);
heat = true;
ac = false;
off = false;
}
if ( (int(Thermister(analogRead(0)))) >= AcAutoOn && ac == false) { // turns A/C on
digitalWrite(y, HIGH);
digitalWrite(g, HIGH);
Serial.println("AUTO A/C ON");
heat = false;
ac = true;
off = false;
}
}
}