Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ testfile
timeinfo
Tmp
TODO
toupper
typedef
uint
undef
Expand Down
2 changes: 1 addition & 1 deletion src/Devices/Serial_TC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Serial_TC::vprintf(const char *format, va_list args) {
}
#ifdef MOCK_PINS_COUNT
#if DEBUG
// std::cout << "\tSERIAL: " << buffer << std::endl;
std::cout << "\tSERIAL: " << buffer << std::endl;
#endif
#endif
}
51 changes: 35 additions & 16 deletions src/Devices/TemperatureControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ void TemperatureControl::enableHeater(bool flag) {
* protected constructor
*/
TemperatureControl::TemperatureControl() {
COUT("TemperatureControl()");
targetTemperature = EEPROM_TC::instance()->getTemp();
if (isnan(targetTemperature)) {
targetTemperature = DEFAULT_TEMPERATURE;
EEPROM_TC::instance()->setTemp(targetTemperature);
}
pinMode(TEMP_CONTROL_PIN, OUTPUT);
pinValue = TURN_SOLENOID_OFF;
digitalWrite(TEMP_CONTROL_PIN, pinValue);
serial("%s with target temperature of %5.2f C", this->isHeater() ? "Heater" : "Chiller", targetTemperature);
digitalWrite(TEMP_CONTROL_PIN, TURN_SOLENOID_OFF);
serial("%s starts with solenoid off with target temperature of %5.2f C", this->isHeater() ? "Heater" : "Chiller",
targetTemperature);
}

/**
Expand All @@ -60,6 +61,10 @@ bool TemperatureControl::isHeater() {
return true;
}

bool TemperatureControl::isOn() {
return digitalRead(TEMP_CONTROL_PIN) == TURN_SOLENOID_ON;
}

/**
* set target temperature and save in EEPROM
*/
Expand All @@ -73,35 +78,48 @@ void TemperatureControl::setTargetTemperature(float newTemperature) {

void Chiller::updateControl(float currentTemperature) {
uint32_t currentMillis = millis();
COUT("Chiller::updateControl(" << currentTemperature << ") at " << currentMillis);
if (currentMillis < previousMillis) {
COUT("Reset previousMillis from " << previousMillis << " to 0");
previousMillis = 0; // reset if clock went backwards (typical during tests)
}
// pause 30 seconds between switching chiller on and off to prevent damage to chiller
if (currentMillis - previousMillis >= TIME_INTERVAL) {
bool newValue = pinValue;
if (currentMillis - previousMillis < TIME_INTERVAL) {
COUT("Chiller update at " << currentMillis << " ignored due to update at " << previousMillis);
} else {
bool oldValue = digitalRead(TEMP_CONTROL_PIN);
bool newValue;
previousMillis = currentMillis;
// if in calibration, turn unit off
if (TankControllerLib::instance()->isInCalibration()) {
newValue = TURN_SOLENOID_OFF;
COUT("Chiller should be off");
}
// if the observed temperature is above the set-point range turn on the chiller
else if (currentTemperature >= targetTemperature + DELTA) {
newValue = TURN_SOLENOID_ON;
COUT("Chiller should be on");
}
// if the observed temperature is below the set-point range turn off the chiller
else if (currentTemperature <= targetTemperature - DELTA) {
newValue = TURN_SOLENOID_OFF;
COUT("Chiller should be off");
} else {
newValue = oldValue;
}
if (newValue != pinValue) {
pinValue = newValue;
DateTime_TC::now().printToSerial();
if (newValue != oldValue) {
uint32_t currentMS = millis();
serial("chiller turned %s after %lu ms", pinValue ? "off" : "on", currentMS - lastSwitchMS);
serial("chiller turned %s at %lu after %lu ms", newValue ? "off" : "on", currentMS, currentMS - lastSwitchMS);
lastSwitchMS = currentMS;
digitalWrite(TEMP_CONTROL_PIN, pinValue);
digitalWrite(TEMP_CONTROL_PIN, newValue);
}
}
}

void Heater::updateControl(float currentTemperature) {
bool newValue = pinValue;
COUT("Heater::updateControl(" << currentTemperature);
bool oldValue = digitalRead(TEMP_CONTROL_PIN);
bool newValue;
// if in calibration, turn unit off
if (TankControllerLib::instance()->isInCalibration()) {
newValue = TURN_SOLENOID_OFF;
Expand All @@ -113,13 +131,14 @@ void Heater::updateControl(float currentTemperature) {
// if the observed temperature is above the temperature set-point range turn off the heater
else if (currentTemperature >= targetTemperature + DELTA) {
newValue = TURN_SOLENOID_OFF;
} else {
newValue = oldValue;
COUT("Heater update at " << millis() << " ignored due to recent change in state");
}
if (newValue != pinValue) {
pinValue = newValue;
DateTime_TC::now().printToSerial();
if (newValue != oldValue) {
uint32_t currentMS = millis();
serial("heater turned %s after %lu ms", pinValue ? "off" : "on", currentMS - lastSwitchMS);
serial("heater turned %s at %lu after %lu ms", newValue ? "off" : "on", currentMS, currentMS - lastSwitchMS);
lastSwitchMS = currentMS;
digitalWrite(TEMP_CONTROL_PIN, pinValue);
digitalWrite(TEMP_CONTROL_PIN, newValue);
}
}
2 changes: 1 addition & 1 deletion src/Devices/TemperatureControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class TemperatureControl {
const float DELTA = 0.05;
uint32_t lastSwitchMS = 0;
float targetTemperature;
bool pinValue = TURN_SOLENOID_OFF;
TemperatureControl();

public:
Expand All @@ -30,6 +29,7 @@ class TemperatureControl {
return targetTemperature;
}
virtual bool isHeater();
bool isOn();
void setTargetTemperature(float newTemperature);
virtual void updateControl(float currentTemperature) = 0;
};
Expand Down
9 changes: 7 additions & 2 deletions src/UIState/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,13 @@ void MainMenu::idle() {
} else if (99.99 < temp) {
temp = 99.99;
}
snprintf(output, sizeof(output), "T%c%5.2f %c %5.2f", equals, temp, (tempControl->isHeater() ? 'H' : 'C'),
tempControl->getTargetTemperature());
char status = tempControl->isHeater() ? 'h' : 'c';
if (tempControl->isOn()) {
status = toupper(status);
// status -= 'a' - 'A'; // convert to uppercase
}

snprintf(output, sizeof(output), "T%c%5.2f %c %5.2f", equals, temp, status, tempControl->getTargetTemperature());
LiquidCrystal_TC::instance()->writeLine(output, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion test/MenuTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ unittest_teardown() {

unittest(MainMenu) {
assertEqual("pH=0.000 8.100", lc->getLines().at(0));
assertEqual("T=12.23 H 15.75 ", lc->getLines().at(1));
assertEqual("T=12.23 h 15.75 ", lc->getLines().at(1));
delay(1000);
tc->loop();
assertEqual("pH 0.000 8.100", lc->getLines().at(0));
Expand Down
Loading