@@ -42,15 +42,16 @@ void TemperatureControl::enableHeater(bool flag) {
4242 * protected constructor
4343 */
4444TemperatureControl::TemperatureControl () {
45+ COUT (" TemperatureControl()" );
4546 targetTemperature = EEPROM_TC::instance ()->getTemp ();
4647 if (isnan (targetTemperature)) {
4748 targetTemperature = DEFAULT_TEMPERATURE;
4849 EEPROM_TC::instance ()->setTemp (targetTemperature);
4950 }
5051 pinMode (TEMP_CONTROL_PIN, OUTPUT);
51- pinValue = TURN_SOLENOID_OFF;
52- digitalWrite (TEMP_CONTROL_PIN, pinValue);
53- serial ( " %s with target temperature of %5.2f C " , this -> isHeater () ? " Heater " : " Chiller " , targetTemperature);
52+ digitalWrite (TEMP_CONTROL_PIN, TURN_SOLENOID_OFF) ;
53+ serial ( " %s starts with solenoid off with target temperature of %5.2f C " , this -> isHeater () ? " Heater " : " Chiller " ,
54+ targetTemperature);
5455}
5556
5657/* *
@@ -60,6 +61,10 @@ bool TemperatureControl::isHeater() {
6061 return true ;
6162}
6263
64+ bool TemperatureControl::isOn () {
65+ return digitalRead (TEMP_CONTROL_PIN) == TURN_SOLENOID_ON;
66+ }
67+
6368/* *
6469 * set target temperature and save in EEPROM
6570 */
@@ -73,35 +78,48 @@ void TemperatureControl::setTargetTemperature(float newTemperature) {
7378
7479void Chiller::updateControl (float currentTemperature) {
7580 uint32_t currentMillis = millis ();
81+ COUT (" Chiller::updateControl(" << currentTemperature << " ) at " << currentMillis);
82+ if (currentMillis < previousMillis) {
83+ COUT (" Reset previousMillis from " << previousMillis << " to 0" );
84+ previousMillis = 0 ; // reset if clock went backwards (typical during tests)
85+ }
7686 // pause 30 seconds between switching chiller on and off to prevent damage to chiller
77- if (currentMillis - previousMillis >= TIME_INTERVAL) {
78- bool newValue = pinValue;
87+ if (currentMillis - previousMillis < TIME_INTERVAL) {
88+ COUT (" Chiller update at " << currentMillis << " ignored due to update at " << previousMillis);
89+ } else {
90+ bool oldValue = digitalRead (TEMP_CONTROL_PIN);
91+ bool newValue;
7992 previousMillis = currentMillis;
8093 // if in calibration, turn unit off
8194 if (TankControllerLib::instance ()->isInCalibration ()) {
8295 newValue = TURN_SOLENOID_OFF;
96+ COUT (" Chiller should be off" );
8397 }
8498 // if the observed temperature is above the set-point range turn on the chiller
8599 else if (currentTemperature >= targetTemperature + DELTA) {
86100 newValue = TURN_SOLENOID_ON;
101+ COUT (" Chiller should be on" );
87102 }
88103 // if the observed temperature is below the set-point range turn off the chiller
89104 else if (currentTemperature <= targetTemperature - DELTA) {
90105 newValue = TURN_SOLENOID_OFF;
106+ COUT (" Chiller should be off" );
107+ } else {
108+ newValue = oldValue;
91109 }
92- if (newValue != pinValue) {
93- pinValue = newValue;
94- DateTime_TC::now ().printToSerial ();
110+ if (newValue != oldValue) {
95111 uint32_t currentMS = millis ();
96- serial (" chiller turned %s after %lu ms" , pinValue ? " off" : " on" , currentMS - lastSwitchMS);
112+ serial (" chiller turned %s at %lu after %lu ms" , newValue ? " off" : " on" , currentMS , currentMS - lastSwitchMS);
97113 lastSwitchMS = currentMS;
98- digitalWrite (TEMP_CONTROL_PIN, pinValue );
114+ digitalWrite (TEMP_CONTROL_PIN, newValue );
99115 }
100116 }
101117}
102118
103119void Heater::updateControl (float currentTemperature) {
104- bool newValue = pinValue;
120+ COUT (" Heater::updateControl(" << currentTemperature);
121+ bool oldValue = digitalRead (TEMP_CONTROL_PIN);
122+ bool newValue;
105123 // if in calibration, turn unit off
106124 if (TankControllerLib::instance ()->isInCalibration ()) {
107125 newValue = TURN_SOLENOID_OFF;
@@ -113,13 +131,14 @@ void Heater::updateControl(float currentTemperature) {
113131 // if the observed temperature is above the temperature set-point range turn off the heater
114132 else if (currentTemperature >= targetTemperature + DELTA) {
115133 newValue = TURN_SOLENOID_OFF;
134+ } else {
135+ newValue = oldValue;
136+ COUT (" Heater update at " << millis () << " ignored due to recent change in state" );
116137 }
117- if (newValue != pinValue) {
118- pinValue = newValue;
119- DateTime_TC::now ().printToSerial ();
138+ if (newValue != oldValue) {
120139 uint32_t currentMS = millis ();
121- serial (" heater turned %s after %lu ms" , pinValue ? " off" : " on" , currentMS - lastSwitchMS);
140+ serial (" heater turned %s at %lu after %lu ms" , newValue ? " off" : " on" , currentMS , currentMS - lastSwitchMS);
122141 lastSwitchMS = currentMS;
123- digitalWrite (TEMP_CONTROL_PIN, pinValue );
142+ digitalWrite (TEMP_CONTROL_PIN, newValue );
124143 }
125144}
0 commit comments