Ok so I have re written my code and tried to make it more of a state machine sequence...i think..haha...and I actually quite like using booleans now!
This system is controlling one hvac system with two zones, pretty straight forward relay board with it. Now, I want to install a mechanical end switch which closes when the damper opens.
My question is how to do that in the code; send 5VDC out to switch, switch closes and inputs 5vdc back to micro controller which allows the code to continue, else if switch does not close system goes into a fault mode and does not allow code to continue?
**you will notice there is a 2 min delay after the heat is turned off, this is due to the fan will stay on (internal mechanical switch in furnace) until it cools down. I dont want the damper closing while the fan is still on! Did not want to go the hassle of a "blink on delay" and besides a two min delay in the system will not affect overall home comfort!!
Again any help is very appreciated thanks all!!
#include <math.h>
//FIRST FLOOR INTEGERS-------------------------------------------------------------------------------
int heatOn = 18;
int heatOff = 19.5;
int airCon = 25;
int acOff = 23;
int wOne = 0;
int yOne = 2;
int gOne = 4;
int damperOpen = 6;
//---------------------------------------------------------------------------------------------------
//SECOND FLOOR INTEGERS------------------------------------------------------------------------------
int heatOnTwo = 18;
int heatOffTwo = 19.5;
int airConTwo = 25;
int acOffTwo = 23;
int wTwo = 1;
int yTwo = 3;
int gTwo = 5;
int damperOpenTwo;
//---------------------------------------------------------------------------------------------------
//TEMPERATURE CHECK DELAY-----------------------------------------------------------------------------
long checkTemp = 20000;
long previoustime;
unsigned long time;
//----------------------------------------------------------------------------------------------------
//FIRST FLOOR BOOLEANS--------------------------------------------------------------------------------
boolean heaterOn = false;
boolean acOn = false;
boolean heatState = false;
boolean acState = false;
boolean damperOn = false;
boolean damperState = false;
//------------------------------------------------------------------------------------------------------
//SECOND FLOOR BOOLEANS---------------------------------------------------------------------------------
boolean heaterOnTwo = false;
boolean acOnTwo = false;
boolean heatStateTwo = false;
boolean acStateTwo = false;
boolean damperOnTwo = false;
boolean damperStateTwo = false;
//-----------------------------------------------------------------------------------------------------
void setup()
{
pinMode (wOne, OUTPUT);
pinMode (gOne, OUTPUT);
pinMode (yOne, OUTPUT);
pinMode (wTwo, OUTPUT);
pinMode (gTwo, OUTPUT);
pinMode (yTwo, OUTPUT);
pinMode (damperOpen, OUTPUT);
pinMode (damperOpenTwo, OUTPUT);
Serial.begin(9600);
}
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()
{
int t = Thermister(analogRead(0));
time = millis();
if((time - previoustime) > checkTemp)
{
//HEAT CYCLE FIRST FLOOR-----------------------------------------------------------------------
if (t <= heatOn) heaterOn = true, damperOn = true;
else if (t >= heatOff) heaterOn = false, damperOn = false;
if ((damperOn == true) && (damperState != true));
{
damperState = true;
digitalWrite(damperOpen, HIGH);
}
if ((heaterOn == true) && (heatState != true));
{
heatState = true;
digitalWrite(wOne, HIGH);
Serial.println("AUTO HEAT ON");
digitalWrite(gOne, HIGH);
}
if((heaterOn == false) && (heatState != false));
{
heatState = false;
digitalWrite(wOne, LOW);
Serial.println("AUTO HEAT OFF");
digitalWrite(gOne, LOW);
delay(120000);
}
if ((damperOn == false) && (damperState != false));
{
damperState = false;
digitalWrite(damperOpen, LOW);
}
//-------------------------------------------------------------------------------------
//AC CYCLE FIRST FLOOR---------------------------------------------------------------------------------
if (t >= airCon) acOn = true, damperOn = true;
else if (t <= acOff) acOn = false, damperOn = false;
if((damperOn == true) && (damperState != true));
{
damperState = true;
digitalWrite(damperOpen, HIGH);
}
if((acOn == true) && (acState != true));
{
acState = true;
digitalWrite(yOne, HIGH);
Serial.println("AUTO AC ON");
digitalWrite(gOne, HIGH);
}
if((acOn == false) && (acState != false));
{
acState = false;
digitalWrite(yOne, LOW);
Serial.println("AC AUTO OFF");
digitalWrite(gOne, LOW);
}
if((damperOn == false) && (damperState != false));
{
damperState = false;
digitalWrite(damperOpen, LOW);
}
//------------------------------------------------------------------------------------------
//HEAT CYCLE SECOND FLOOR-----------------------------------------------------------------------
if (t <= heatOnTwo) heaterOnTwo = true, damperOnTwo = true;
else if (t >= heatOffTwo) heaterOnTwo = false, damperOnTwo = true;
if((damperOnTwo == true) && (damperStateTwo != true));
{
damperStateTwo = true;
digitalWrite(damperOpenTwo, HIGH);
}
if ((heaterOnTwo == true) && (heatStateTwo != true));
{
heatStateTwo = true;
digitalWrite(wTwo, HIGH);
Serial.println("AUTO SECOND FLOOR HEAT ON");
digitalWrite(gTwo, HIGH);
}
if((heaterOnTwo == false) && (heatStateTwo != false));
{
heatStateTwo = false;
digitalWrite(wTwo, LOW);
Serial.println("AUTO SECOND FLOOR HEAT OFF");
digitalWrite(gTwo, LOW);
delay(120000);
}
if((damperOnTwo == false) && (damperState != false));
{
damperState = false;
digitalWrite(damperOpenTwo, LOW);
}
//-------------------------------------------------------------------------------------
//AC CYCLE SECOND FLOOR---------------------------------------------------------------------------------
if (t >= airConTwo) acOnTwo = true;
else if (t <= acOffTwo) acOnTwo = false;
if((damperOnTwo == true) && (damperStateTwo != true));
{
damperStateTwo = true;
digitalWrite(damperOpen, HIGH);
}
if((acOnTwo == true) && (acStateTwo != true));
{
acState = true;
digitalWrite(yTwo, HIGH);
Serial.println("AUTO SECOND FLOOR AC ON");
digitalWrite(gTwo, HIGH);
}
if((acOnTwo == false) && (acStateTwo != false));
{
acStateTwo = false;
digitalWrite(yTwo, LOW);
Serial.println("AUTO SECOND FLOOR AC OFF");
digitalWrite(gTwo, LOW);
}
if((damperOnTwo == false) && (damperState != false));
{
damperState = false;
digitalWrite(damperOpenTwo, LOW);
}
}
}
//------------------------------------------------------------------------------------------