Add temperature hysteresis

Do not refresh display if value not changed
This commit is contained in:
cryptogopher
2022-10-09 22:18:59 +02:00
parent 814a99f733
commit 8594905748
3 changed files with 29 additions and 9 deletions

View File

@@ -1,12 +1,23 @@
#include "Seeed_BME280.h" #include "Seeed_BME280.h"
BME280 sensor; BME280 sensor;
float highTemp = 0.0, lowTemp = 0.0;
const float blindZone = 0.03;
void initBME280() { void initBME280() {
sensor.init(); sensor.init();
} }
void readTemperature() { void readTemperature() {
// TODO: add hysteresis float newTemperature = sensor.getTemperature();
temperature = sensor.getTemperature();
if (newTemperature > highTemp) {
temperature = newTemperature;
highTemp = newTemperature;
lowTemp = highTemp - blindZone;
} else if (newTemperature < lowTemp) {
temperature = newTemperature;
lowTemp = newTemperature;
highTemp = lowTemp + blindZone;
};
} }

View File

@@ -32,6 +32,7 @@ void setup() {
TaskTimer.attachInterruptInterval_MS(20, TasksHandler); TaskTimer.attachInterruptInterval_MS(20, TasksHandler);
TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, runMetronome);
TasksISRs.setInterval(PERIOD_MS, readTemperature); TasksISRs.setInterval(PERIOD_MS, readTemperature);
// TODO: move display from ISR to loop()
TasksISRs.setInterval(200, updateTube); TasksISRs.setInterval(200, updateTube);
} }

View File

@@ -1,22 +1,30 @@
#include "grove_alphanumeric_display.h" #include "grove_alphanumeric_display.h"
Seeed_Digital_Tube tube; Seeed_Digital_Tube tube;
char tubeText[5] = "";
void initTube() { void initTube() {
// Wire initialized by BME280 sensor // Wire initialized by BME280 sensor
tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR);
tube.setBrightness(15); tube.setBrightness(15);
tube.setBlinkRate(BLINK_OFF); tube.setBlinkRate(BLINK_OFF);
tube.setPoint(true,true);
} }
void updateTube() { void updateTube() {
char tube_str[5]; char newText[5];
bool highPoint = false, lowPoint = false;
if (countdown <= 0) if (countdown <= 0) {
sprintf(tube_str, "%4u", (unsigned int) (temperature * 100.0)); sprintf(newText, "%4u", (unsigned int) (temperature * 100.0));
else lowPoint = true;
sprintf(tube_str, "%4u", countdown); } else {
sprintf(newText, "%4u", countdown);
}
tube.displayString(tube_str); if (strcmp(tubeText, newText)) {
strcpy(tubeText, newText);
tube.displayString(tubeText);
tube.setPoint(highPoint, lowPoint);
tube.display();
}
} }