Display also humidity and pressure

Display update triggered by value change, not interval
This commit is contained in:
cryptogopher
2026-02-12 00:10:45 +01:00
parent 0c9a7a51d9
commit c2af98d588
5 changed files with 116 additions and 33 deletions

View File

@@ -1,23 +1,50 @@
#include "Seeed_BME280.h" #include "Seeed_BME280.h"
enum {
LOWER = 0,
HIGHER
};
BME280 sensor; BME280 sensor;
float highTemp = 0.0, lowTemp = 0.0; float bounds[2][SENSORS] = {};
const float blindZone = 0.03; const float hysteresisWidth[SENSORS] = {0.03, 0.5, 0.5};
void initBME280() { void initBME280() {
sensor.init(); sensor.init();
} }
void readTemperature() { void readSensors() {
float newTemperature = sensor.getTemperature(); float newValue;
if (newTemperature > highTemp) { for (uint i = 0; i < SENSORS; i++) {
temperature = newTemperature; switch (i) {
highTemp = newTemperature; case TEMPERATURE:
lowTemp = highTemp - blindZone; newValue = sensor.getTemperature();
} else if (newTemperature < lowTemp) { break;
temperature = newTemperature;
lowTemp = newTemperature; case HUMIDITY:
highTemp = lowTemp + blindZone; newValue = sensor.getHumidity();
break;
case PRESSURE:
newValue = sensor.getPressure();
break;
}
if (newValue > bounds[HIGHER][i]) {
sensorValue[i] = newValue;
bounds[HIGHER][i] = newValue;
bounds[LOWER][i] = bounds[HIGHER][i] - hysteresisWidth[i];
if ((countdown <= 0) && (displaySensor == i)) {
displayNeedsUpdate = true;
}
} else if (newValue < bounds[LOWER][i]) {
sensorValue[i] = newValue;
bounds[LOWER][i] = newValue;
bounds[HIGHER][i] = bounds[LOWER][i] + hysteresisWidth[i];
if ((countdown <= 0) && (displaySensor == i)) {
displayNeedsUpdate = true;
}
};
}; };
} }

View File

@@ -1,6 +1,6 @@
volatile unsigned long lastButtonPress = millis(); volatile unsigned long lastButtonPress = millis();
// Debouncing na timerze: // Debouncing using timer:
// https://github.com/khoih-prog/TimerInterrupt/blob/master/examples/SwitchDebounce/SwitchDebounce.ino // https://github.com/khoih-prog/TimerInterrupt/blob/master/examples/SwitchDebounce/SwitchDebounce.ino
void initButton() { void initButton() {
pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(BUTTON_PIN, INPUT_PULLUP);
@@ -9,17 +9,20 @@ void initButton() {
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING);
} }
// FIXME: runMetronome ISR should be started/stopped by button. Otherwise fraction of first PERIOD_MS is lost from countdown.
void buttonISRstate() { void buttonISRstate() {
if ((millis() - lastButtonPress) > 100) { if ((millis() - lastButtonPress) > 100) {
if (countdown < 0) if (countdown < 0)
// TODO: enable metronome timer // TODO: enable metronome timer
countdown = 0; countdown = 0;
else if (countdown == 0) else if (countdown == 0) {
// TODO: restart metronome timer to align countdown // TODO: restart metronome timer to align countdown
countdown = COUNTDOWN; countdown = COUNTDOWN;
else { updateTube();
} else {
countdown = -1; countdown = -1;
digitalWrite(BUTTON_LED_PIN, LOW); digitalWrite(BUTTON_LED_PIN, LOW);
updateTube();
// TODO: disable metronome timer // TODO: disable metronome timer
} }
} }

Binary file not shown.

View File

@@ -4,24 +4,52 @@
SAMDTimer TaskTimer(TIMER_TC3); SAMDTimer TaskTimer(TIMER_TC3);
SAMD_ISR_Timer TasksISRs; SAMD_ISR_Timer TasksISRs;
void TasksHandler(void) { TasksISRs.run(); } enum {
TEMPERATURE = 0,
HUMIDITY,
PRESSURE,
SENSORS
};
// COUNTDOWN is effectively multiplied by METRONOME_INTERVAL
const int COUNTDOWN = 600; const int COUNTDOWN = 600;
const int PERIOD_MS = 1000; // INTERVALS and TIMESHARES given in [ms]
const int BUZZER_FREQ = 300; const uint TASK_HANDLER_INTERVAL = 20;
const uint METRONOME_INTERVAL = 1000;
const uint SENSOR_INTERVAL = 1000;
const uint SENSOR_TIMESHARE[SENSORS] = {90000, 20000, 10000};
// FREQUENCY in [Hz]
const uint BUZZER_FREQ = 300;
const uint BUTTON_PIN = 6; const uint BUTTON_PIN = 6;
const uint BUTTON_LED_PIN = 5; const uint BUTTON_LED_PIN = 5;
const uint BUZZER_PIN = 0; const uint BUZZER_PIN = 0;
/* Metronome state is expressed by countdown: /* Metronome state is expressed by countdown:
* -1 - IDLE -1 - IDLE
* 0 - BEATING 0 - BEATING
* >0 - COUNTDOWN >0 - COUNTDOWN
*/ Unless metronome is in COUNTDOWN mode, display rotates through sensor readings.
*/
volatile int countdown = -1; volatile int countdown = -1;
volatile float temperature = 0.0; // Units: temperature [C], humidity [%], pressure [P]
volatile float sensorValue[SENSORS] = {};
volatile uint displaySensor = TEMPERATURE;
volatile bool displayNeedsUpdate = false;
volatile bool tubeRotated = false;
int tubeTimerID;
void TasksHandler(void) {
TasksISRs.run();
if (displayNeedsUpdate || tubeRotated)
updateTube();
if (tubeRotated) {
TasksISRs.changeInterval(tubeTimerID, SENSOR_TIMESHARE[displaySensor]);
tubeRotated = false;
}
}
void setup() { void setup() {
initButton(); initButton();
@@ -29,11 +57,13 @@ void setup() {
initBuzzer(); initBuzzer();
initTube(); initTube();
TaskTimer.attachInterruptInterval_MS(20, TasksHandler); readSensors();
TasksISRs.setInterval(PERIOD_MS, runMetronome); updateTube();
TasksISRs.setInterval(PERIOD_MS, readTemperature);
// TODO: move display from ISR to loop() TaskTimer.attachInterruptInterval_MS(TASK_HANDLER_INTERVAL, TasksHandler);
TasksISRs.setInterval(200, updateTube); TasksISRs.setInterval(METRONOME_INTERVAL, runMetronome);
TasksISRs.setInterval(SENSOR_INTERVAL, readSensors);
tubeTimerID = TasksISRs.setInterval(SENSOR_TIMESHARE[displaySensor], rotateTube);
} }
void runMetronome() { void runMetronome() {
@@ -41,6 +71,8 @@ void runMetronome() {
countdown -= 1; countdown -= 1;
digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW);
displayNeedsUpdate = true;
} else if (countdown == 0) { } else if (countdown == 0) {
TasksISRs.setTimeout(100, noBuzz); TasksISRs.setTimeout(100, noBuzz);
buzz(); buzz();

View File

@@ -10,21 +10,42 @@ void initTube() {
tube.setBlinkRate(BLINK_OFF); tube.setBlinkRate(BLINK_OFF);
} }
void rotateTube() {
displaySensor = (displaySensor + 1) % SENSORS;
tubeRotated = true;
}
void updateTube() { void updateTube() {
char newText[5]; char newText[5];
bool highPoint = false, lowPoint = false; bool highPoint = false, lowPoint = false;
uint value = countdown;
if (countdown <= 0) { if (countdown <= 0) {
sprintf(newText, "%4u", (unsigned int) (temperature * 100.0)); switch (displaySensor) {
lowPoint = true; case TEMPERATURE:
} else { value = (uint) (sensorValue[TEMPERATURE] * 100);
sprintf(newText, "%4u", countdown); lowPoint = true;
break;
case HUMIDITY:
// TODO: add % sign?
value = (uint) (sensorValue[HUMIDITY]);
break;
case PRESSURE:
value = (uint) (sensorValue[PRESSURE] / 100);
break;
}
} }
sprintf(newText, "%4u", value);
// TODO: In the current mode of display refresh (displayNeedsUpdate) it should not be necessary to check previous text
// Maybe add LED switching for case when the strings match as a rough check before removing comparison?
// Or LED switching when display is updated?
if (strcmp(tubeText, newText)) { if (strcmp(tubeText, newText)) {
strcpy(tubeText, newText); strcpy(tubeText, newText);
tube.displayString(tubeText); tube.displayString(tubeText);
tube.setPoint(highPoint, lowPoint); tube.setPoint(highPoint, lowPoint);
tube.display(); tube.display();
} }
displayNeedsUpdate = false;
} }