Skip to content

Instantly share code, notes, and snippets.

@niektemme
Created July 31, 2015 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save niektemme/b789a02906bd8efea11e to your computer and use it in GitHub Desktop.
Save niektemme/b789a02906bd8efea11e to your computer and use it in GitHub Desktop.
Smart Thermostat Arduino - part control boiler
...
//variables for calculating avarage temperature at Arduino
int avgTotalTemp = 0;
int avgTemp = 0;
int avgNumSamp = 0;
int avgMaxSamp =10;
//control variables
int startup = 0; //initial delay before temparature is set
int controllPin = 9; //pin connected to relay
int runScen = 0; //temperature run level 0 ,1 or 2
unsigned long startScen = 0; //scenario started
unsigned long scenLength = 0; //minutes on
unsigned long maxScen = 600000; //10 minutes
int boilerStat = 0; //boiler status
//variables of inital check if arduino has been running long enough
int vrunl = 0; //
unsigned long startArdMillis = 0; //start millis
unsigned long nextRunl = 10000; //milisecond arduino has to be running
...
void loop() {
unsigned long currentMillis = millis();
//vrunl is used to see of Arduino has been running for x number of second to make sure a viable actual temperature has been received or measured
if (vrunl == 0) {
startArdMillis = currentMillis;
vrunl = 1;
} else if (vrunl == 1) {
if (currentMillis - startArdMillis > nextRunl ){
vrunl = 2;
}
}
...
// calculating avarage temperature using thermistor 1 value
avgNumSamp = avgNumSamp + 1;
if(avgNumSamp > avgMaxSamp) {
avgNumSamp = 1;
avgTotalTemp = 0;
}
avgTotalTemp = avgTotalTemp + thermistorValue;
if(avgNumSamp == avgMaxSamp){
avgTemp = (int) round(avgTotalTemp/avgNumSamp);
startup = 1;
}
..
//main boiler controll loop.
//3 run levels.
// run level 0 = initial,
// run level 1 = determine how many mintues boier shoudl be on,
// run level 2 = continuesly check of boier should be on for 10 minutes
if(runScen == 0) { //no scenario running
if(startup > 0 && vrunl ==2) { //wait for a time delay to make sure there is correctly measured avarage temperature
//check if controll should go to runlevel 1
runScen = frunScen(avgTemp,sensorValue);
}
} else if (runScen == 1) { //start scenario run level 1
startScen = currentMillis;
scenLength = fscenLength(avgTemp,sensorValue); //check how many seconds boiler should be on in 10 minute interval
runScen = 2;
boilerStat = 1; //turn boiler on
//sending minutes boiler on time to Rapsberry PI for monitoring purposses
unsigned long scenLengthCalc = (scenLength/(1000UL));
int sendScenLength = (int) scenLengthCalc;
delay(50);
fxbesend("A06_",sendScenLength);
delay(50);
} else if (runScen == 2) { //runlevel 2 always runs for 10 minutes
if (boilerStat == 1 ) { //onley check boiler stat if boiler is on this to prevent on/off fluctuation af ter overshoot
boilerStat = fboilerStat(startScen,scenLength,currentMillis,avgTemp,sensorValue); //continuesly check if boiler should be on
}
if (boilerStat == 1 ) { //turn relay to on of boiler should be on
digitalWrite(controllPin, HIGH);
} else {
digitalWrite(controllPin, LOW);
}
//after 10 minutes go back to run level 0
if(currentMillis - startScen > maxScen) {
runScen = 0;
}
}
...
}
..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment