From 3f5ce648d5f0eb62f5ad203951cf0e3ce99ad6cb Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 9 Sep 2022 13:07:56 +0200 Subject: [PATCH 1/9] Add BME280 temperature display on timer ISR --- metronom.ino | 16 ++++++++++++++++ temperature.ino | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 temperature.ino diff --git a/metronom.ino b/metronom.ino index e9be3bf..0d054c2 100644 --- a/metronom.ino +++ b/metronom.ino @@ -1,3 +1,15 @@ +#include "SAMDTimerInterrupt.h" +#include "SAMD_ISR_Timer.h" + +#define HW_TIMER_INTERVAL_MS 10 +SAMDTimer ITimer(TIMER_TC3); +SAMD_ISR_Timer ISR_Timer; + +void TimerHandler(void) { + ISR_Timer.run(); +} + + const uint BUZZER_PIN = 0; const uint BUTTON_LED_PIN = 5; @@ -14,6 +26,10 @@ void setup() { // Order of ISRs matter: RISING should be invoked first attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRtime, CHANGE); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING); + + initTemperature(); + ITimer.attachInterruptInterval_MS(10, TimerHandler); + ISR_Timer.setInterval(1000, readTemperature); } void loop() { diff --git a/temperature.ino b/temperature.ino new file mode 100644 index 0000000..08d5ff3 --- /dev/null +++ b/temperature.ino @@ -0,0 +1,21 @@ +#include "grove_alphanumeric_display.h" +#include "Seeed_BME280.h" + +Seeed_Digital_Tube tube; +BME280 sensor; + +void initTemperature() { + // BME280 inits Wire + sensor.init(); + + tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); + tube.setBrightness(15); + tube.setBlinkRate(BLINK_OFF); + tube.setPoint(true,true); +} + +void readTemperature() { + char temperature_str[5]; + sprintf(temperature_str, "%4u", (unsigned int) (sensor.getTemperature() * 100.0)); + tube.displayString(temperature_str); +} From b81b52b204a249af76c72eb9b903206a6dcd59d6 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Mon, 12 Sep 2022 00:07:37 +0200 Subject: [PATCH 2/9] Replace delay() with ISR_Timer --- button.ino | 17 +++++++++++++--- metronom.ino | 53 +++++++++++++++++++++++-------------------------- temperature.ino | 21 -------------------- 3 files changed, 39 insertions(+), 52 deletions(-) delete mode 100644 temperature.ino diff --git a/button.ino b/button.ino index 9fd0b9b..e90ac79 100644 --- a/button.ino +++ b/button.ino @@ -1,11 +1,22 @@ volatile unsigned long lastButtonPress = millis(); +void initButton() { + pinMode(BUTTON_PIN, INPUT_PULLUP); + // Order of ISRs matter: RISING should be invoked first + attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRtime, CHANGE); + attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING); +} + void buttonISRstate() { if ((millis() - lastButtonPress) > 100) { - if (countdown > 0) + if (countdown < 0) countdown = 0; - else - countdown = BREAK_LENGTH; + else if (countdown == 0) + countdown = COUNTDOWN; + else { + countdown = -1; + digitalWrite(BUTTON_LED_PIN, LOW); + } } } diff --git a/metronom.ino b/metronom.ino index 0d054c2..d16f522 100644 --- a/metronom.ino +++ b/metronom.ino @@ -5,44 +5,41 @@ SAMDTimer ITimer(TIMER_TC3); SAMD_ISR_Timer ISR_Timer; -void TimerHandler(void) { - ISR_Timer.run(); -} +void TimerHandler(void) { ISR_Timer.run(); } +const int COUNTDOWN = 300; +const int PERIOD_MS = 1000; +const uint BUTTON_PIN = 6; +const uint BUTTON_LED_PIN = 5; const uint BUZZER_PIN = 0; -const uint BUTTON_LED_PIN = 5; -const uint BUTTON_PIN = 6; - -const int BREAK_LENGTH = 300000; -const int BREAK_STEP = 2000; - -volatile int countdown = 0; -int step; +/* Metronome state is expressed by countdown: + * -1 - IDLE + * 0 - BEATING + * >0 - COUNTDOWN + */ +volatile int countdown = -1; void setup() { - pinMode(BUTTON_PIN, INPUT_PULLUP); - // Order of ISRs matter: RISING should be invoked first - attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRtime, CHANGE); - attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING); + initButton(); - initTemperature(); ITimer.attachInterruptInterval_MS(10, TimerHandler); - ISR_Timer.setInterval(1000, readTemperature); + ISR_Timer.setInterval(PERIOD_MS, metronomeBeat); + ISR_Timer.setInterval(PERIOD_MS, metronomeCountdown); } -void loop() { +void metronomeCountdown() { if (countdown > 0) { - step = min(BREAK_STEP, countdown); - countdown -= step; - - digitalWrite(BUTTON_LED_PIN, HIGH); - delay(min(BREAK_STEP/2, step)); - digitalWrite(BUTTON_LED_PIN, LOW); - delay(max(step-BREAK_STEP/2, 0)); - } else { - tone(BUZZER_PIN, 1000, 100); - delay(1000); + countdown -= 1; + + digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } } + +void metronomeBeat() { + if (countdown == 0) + tone(BUZZER_PIN, 1000, 100); +} + +void loop() {} diff --git a/temperature.ino b/temperature.ino deleted file mode 100644 index 08d5ff3..0000000 --- a/temperature.ino +++ /dev/null @@ -1,21 +0,0 @@ -#include "grove_alphanumeric_display.h" -#include "Seeed_BME280.h" - -Seeed_Digital_Tube tube; -BME280 sensor; - -void initTemperature() { - // BME280 inits Wire - sensor.init(); - - tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); - tube.setBrightness(15); - tube.setBlinkRate(BLINK_OFF); - tube.setPoint(true,true); -} - -void readTemperature() { - char temperature_str[5]; - sprintf(temperature_str, "%4u", (unsigned int) (sensor.getTemperature() * 100.0)); - tube.displayString(temperature_str); -} From 3872ece37be9018ae6290b929c94d54fa841ed77 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 14:40:33 +0200 Subject: [PATCH 3/9] Join ISRs --- metronom.ino | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/metronom.ino b/metronom.ino index d16f522..fa8a1db 100644 --- a/metronom.ino +++ b/metronom.ino @@ -25,21 +25,17 @@ void setup() { initButton(); ITimer.attachInterruptInterval_MS(10, TimerHandler); - ISR_Timer.setInterval(PERIOD_MS, metronomeBeat); - ISR_Timer.setInterval(PERIOD_MS, metronomeCountdown); + ISR_Timer.setInterval(PERIOD_MS, metronomeRun); } -void metronomeCountdown() { +void metronomeRun() { if (countdown > 0) { countdown -= 1; digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); + } else if (countdown == 0) { + //tone(BUZZER_PIN, 1000, 100); } } -void metronomeBeat() { - if (countdown == 0) - tone(BUZZER_PIN, 1000, 100); -} - void loop() {} From 21512bc4f462f31c727019076fd96d281891fa1e Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 15:09:52 +0200 Subject: [PATCH 4/9] Add temperature and break countdown display --- metronom.ino | 30 +++++++++++++++++++++++++++--- temperature.ino | 12 ++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 temperature.ino diff --git a/metronom.ino b/metronom.ino index fa8a1db..1f4dde7 100644 --- a/metronom.ino +++ b/metronom.ino @@ -1,5 +1,6 @@ #include "SAMDTimerInterrupt.h" #include "SAMD_ISR_Timer.h" +#include "grove_alphanumeric_display.h" #define HW_TIMER_INTERVAL_MS 10 SAMDTimer ITimer(TIMER_TC3); @@ -7,7 +8,9 @@ SAMD_ISR_Timer ISR_Timer; void TimerHandler(void) { ISR_Timer.run(); } -const int COUNTDOWN = 300; +Seeed_Digital_Tube tube; + +const int COUNTDOWN = 240; const int PERIOD_MS = 1000; const uint BUTTON_PIN = 6; @@ -20,15 +23,25 @@ const uint BUZZER_PIN = 0; * >0 - COUNTDOWN */ volatile int countdown = -1; +volatile float temperature = 0.0; void setup() { initButton(); + initTemperature(); + + // Wire initialized by temperature sensor + tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); + tube.setBrightness(15); + tube.setBlinkRate(BLINK_OFF); + tube.setPoint(true,true); ITimer.attachInterruptInterval_MS(10, TimerHandler); - ISR_Timer.setInterval(PERIOD_MS, metronomeRun); + ISR_Timer.setInterval(PERIOD_MS, runMetronome); + ISR_Timer.setInterval(PERIOD_MS, readTemperature); + ISR_Timer.setInterval(PERIOD_MS, updateTube); } -void metronomeRun() { +void runMetronome() { if (countdown > 0) { countdown -= 1; @@ -38,4 +51,15 @@ void metronomeRun() { } } +void updateTube() { + char tube_str[5]; + + if (countdown <= 0) + sprintf(tube_str, "%4u", (unsigned int) (temperature * 100.0)); + else + sprintf(tube_str, "%4u", countdown); + + tube.displayString(tube_str); +} + void loop() {} diff --git a/temperature.ino b/temperature.ino new file mode 100644 index 0000000..42f64cf --- /dev/null +++ b/temperature.ino @@ -0,0 +1,12 @@ +#include "Seeed_BME280.h" + +BME280 sensor; + +void initTemperature() { + sensor.init(); +} + +void readTemperature() { + // TODO: add hysteresis + temperature = sensor.getTemperature(); +} From 374a72fb070fd0f46687e8e45cd2eecba1cb42f4 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 15:29:30 +0200 Subject: [PATCH 5/9] Move tube to separate file --- temperature.ino => bme280.ino | 0 metronom.ino | 22 ++-------------------- tube.ino | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) rename temperature.ino => bme280.ino (100%) create mode 100644 tube.ino diff --git a/temperature.ino b/bme280.ino similarity index 100% rename from temperature.ino rename to bme280.ino diff --git a/metronom.ino b/metronom.ino index 1f4dde7..733347e 100644 --- a/metronom.ino +++ b/metronom.ino @@ -1,6 +1,5 @@ #include "SAMDTimerInterrupt.h" #include "SAMD_ISR_Timer.h" -#include "grove_alphanumeric_display.h" #define HW_TIMER_INTERVAL_MS 10 SAMDTimer ITimer(TIMER_TC3); @@ -8,7 +7,6 @@ SAMD_ISR_Timer ISR_Timer; void TimerHandler(void) { ISR_Timer.run(); } -Seeed_Digital_Tube tube; const int COUNTDOWN = 240; const int PERIOD_MS = 1000; @@ -28,12 +26,7 @@ volatile float temperature = 0.0; void setup() { initButton(); initTemperature(); - - // Wire initialized by temperature sensor - tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); - tube.setBrightness(15); - tube.setBlinkRate(BLINK_OFF); - tube.setPoint(true,true); + initTube(); ITimer.attachInterruptInterval_MS(10, TimerHandler); ISR_Timer.setInterval(PERIOD_MS, runMetronome); @@ -47,19 +40,8 @@ void runMetronome() { digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } else if (countdown == 0) { - //tone(BUZZER_PIN, 1000, 100); + //tone(BUZZER_PIN, 300, 100); } } -void updateTube() { - char tube_str[5]; - - if (countdown <= 0) - sprintf(tube_str, "%4u", (unsigned int) (temperature * 100.0)); - else - sprintf(tube_str, "%4u", countdown); - - tube.displayString(tube_str); -} - void loop() {} diff --git a/tube.ino b/tube.ino new file mode 100644 index 0000000..2050ce3 --- /dev/null +++ b/tube.ino @@ -0,0 +1,22 @@ +#include "grove_alphanumeric_display.h" + +Seeed_Digital_Tube tube; + +void initTube() { + // Wire initialized by temperature sensor + tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); + tube.setBrightness(15); + tube.setBlinkRate(BLINK_OFF); + tube.setPoint(true,true); +} + +void updateTube() { + char tube_str[5]; + + if (countdown <= 0) + sprintf(tube_str, "%4u", (unsigned int) (temperature * 100.0)); + else + sprintf(tube_str, "%4u", countdown); + + tube.displayString(tube_str); +} From f8878d3c37b910d7973d3a35de673f5c141f0223 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 20:34:25 +0200 Subject: [PATCH 6/9] Replace tone() with custom timer --- button.ino | 2 ++ metronom.ino | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/button.ino b/button.ino index e90ac79..a29d8c5 100644 --- a/button.ino +++ b/button.ino @@ -1,5 +1,7 @@ volatile unsigned long lastButtonPress = millis(); +// Debouncing na timerze: +// https://github.com/khoih-prog/TimerInterrupt/blob/master/examples/SwitchDebounce/SwitchDebounce.ino void initButton() { pinMode(BUTTON_PIN, INPUT_PULLUP); // Order of ISRs matter: RISING should be invoked first diff --git a/metronom.ino b/metronom.ino index 733347e..fb853f9 100644 --- a/metronom.ino +++ b/metronom.ino @@ -1,15 +1,16 @@ #include "SAMDTimerInterrupt.h" #include "SAMD_ISR_Timer.h" -#define HW_TIMER_INTERVAL_MS 10 -SAMDTimer ITimer(TIMER_TC3); -SAMD_ISR_Timer ISR_Timer; +SAMDTimer TaskTimer(TIMER_TC3); +SAMDTimer BuzzerTimer(TIMER_TC5); +SAMD_ISR_Timer TasksISRs; -void TimerHandler(void) { ISR_Timer.run(); } +void TasksHandler(void) { TasksISRs.run(); } const int COUNTDOWN = 240; const int PERIOD_MS = 1000; +const int BUZZER_FREQ = 300; const uint BUTTON_PIN = 6; const uint BUTTON_LED_PIN = 5; @@ -28,10 +29,25 @@ void setup() { initTemperature(); initTube(); - ITimer.attachInterruptInterval_MS(10, TimerHandler); - ISR_Timer.setInterval(PERIOD_MS, runMetronome); - ISR_Timer.setInterval(PERIOD_MS, readTemperature); - ISR_Timer.setInterval(PERIOD_MS, updateTube); + pinMode(BUZZER_PIN, OUTPUT); + BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzz); + BuzzerTimer.disableTimer(); + + TaskTimer.attachInterruptInterval_MS(25, TasksHandler); + TasksISRs.setInterval(PERIOD_MS, runMetronome); + TasksISRs.setInterval(PERIOD_MS, readTemperature); + TasksISRs.setInterval(PERIOD_MS, updateTube); +} + +volatile bool buzzing = false; +void buzz() { + digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); + buzzing = !buzzing; +} + +void stopBuzzer() { + BuzzerTimer.disableTimer(); + digitalWrite(BUZZER_PIN, LOW); } void runMetronome() { @@ -40,8 +56,10 @@ void runMetronome() { digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } else if (countdown == 0) { - //tone(BUZZER_PIN, 300, 100); + TasksISRs.setTimeout(100, stopBuzzer); + BuzzerTimer.enableTimer(); } } -void loop() {} +void loop() { +} From 7fe04d76cb11a6a0f40b806788d0cf5f6510a7ff Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 21:36:40 +0200 Subject: [PATCH 7/9] Extract buzzer to separate file --- bme280.ino | 2 +- buzzer.ino | 46 ++++++++++++++++++++++++++++++++++++++++++++++ metronom.ino | 28 ++++++---------------------- tube.ino | 2 +- 4 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 buzzer.ino diff --git a/bme280.ino b/bme280.ino index 42f64cf..a04becf 100644 --- a/bme280.ino +++ b/bme280.ino @@ -2,7 +2,7 @@ BME280 sensor; -void initTemperature() { +void initBME280() { sensor.init(); } diff --git a/buzzer.ino b/buzzer.ino new file mode 100644 index 0000000..7edbc85 --- /dev/null +++ b/buzzer.ino @@ -0,0 +1,46 @@ +#include + +SAMDTimer BuzzerTimer(TIMER_TC5); +bool attached = false; + +void initBuzzer() { + pinMode(BUZZER_PIN, OUTPUT); +} + +volatile bool buzzing = false; +void buzzerISR() { + digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); + buzzing = !buzzing; + //digitalToggle(BUZZER_PIN); +} + +void buzz() { + if (attached) + BuzzerTimer.enableTimer(); + else + BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzzerISR); +} + +void noBuzz() { + BuzzerTimer.disableTimer(); +} + +/* +// https://forum.arduino.cc/t/adding-a-toggle-function-to-wiring-digital-c/975608 +void digitalToggle(uint8_t pin) { + uint8_t timer = digitalPinToTimer(pin); + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + volatile uint8_t *out; + + if (port == NOT_A_PIN) return; + if (timer != NOT_ON_TIMER) turnOffPWM(timer); + + out = portOutputRegister(port); + + uint8_t oldSREG = SREG; + cli(); + *out ^= bit; + SREG = oldSREG; +} +*/ diff --git a/metronom.ino b/metronom.ino index fb853f9..e2dc9ac 100644 --- a/metronom.ino +++ b/metronom.ino @@ -2,7 +2,6 @@ #include "SAMD_ISR_Timer.h" SAMDTimer TaskTimer(TIMER_TC3); -SAMDTimer BuzzerTimer(TIMER_TC5); SAMD_ISR_Timer TasksISRs; void TasksHandler(void) { TasksISRs.run(); } @@ -26,40 +25,25 @@ volatile float temperature = 0.0; void setup() { initButton(); - initTemperature(); + initBME280(); + initBuzzer(); initTube(); - pinMode(BUZZER_PIN, OUTPUT); - BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzz); - BuzzerTimer.disableTimer(); - - TaskTimer.attachInterruptInterval_MS(25, TasksHandler); + TaskTimer.attachInterruptInterval_MS(20, TasksHandler); TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, readTemperature); TasksISRs.setInterval(PERIOD_MS, updateTube); } -volatile bool buzzing = false; -void buzz() { - digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); - buzzing = !buzzing; -} - -void stopBuzzer() { - BuzzerTimer.disableTimer(); - digitalWrite(BUZZER_PIN, LOW); -} - void runMetronome() { if (countdown > 0) { countdown -= 1; digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } else if (countdown == 0) { - TasksISRs.setTimeout(100, stopBuzzer); - BuzzerTimer.enableTimer(); + TasksISRs.setTimeout(100, noBuzz); + buzz(); } } -void loop() { -} +void loop() {} diff --git a/tube.ino b/tube.ino index 2050ce3..0eaa44f 100644 --- a/tube.ino +++ b/tube.ino @@ -3,7 +3,7 @@ Seeed_Digital_Tube tube; void initTube() { - // Wire initialized by temperature sensor + // Wire initialized by BME280 sensor tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); tube.setBrightness(15); tube.setBlinkRate(BLINK_OFF); From 814a99f7330408b48561d7691879c3974f571dfa Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 21:48:16 +0200 Subject: [PATCH 8/9] Remove buzzer state variable --- buzzer.ino | 28 ++-------------------------- metronom.ino | 2 +- 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/buzzer.ino b/buzzer.ino index 7edbc85..39ff25c 100644 --- a/buzzer.ino +++ b/buzzer.ino @@ -1,17 +1,13 @@ -#include - SAMDTimer BuzzerTimer(TIMER_TC5); + bool attached = false; void initBuzzer() { pinMode(BUZZER_PIN, OUTPUT); } -volatile bool buzzing = false; void buzzerISR() { - digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); - buzzing = !buzzing; - //digitalToggle(BUZZER_PIN); + digitalWrite(BUZZER_PIN, !digitalRead(BUZZER_PIN)); } void buzz() { @@ -24,23 +20,3 @@ void buzz() { void noBuzz() { BuzzerTimer.disableTimer(); } - -/* -// https://forum.arduino.cc/t/adding-a-toggle-function-to-wiring-digital-c/975608 -void digitalToggle(uint8_t pin) { - uint8_t timer = digitalPinToTimer(pin); - uint8_t bit = digitalPinToBitMask(pin); - uint8_t port = digitalPinToPort(pin); - volatile uint8_t *out; - - if (port == NOT_A_PIN) return; - if (timer != NOT_ON_TIMER) turnOffPWM(timer); - - out = portOutputRegister(port); - - uint8_t oldSREG = SREG; - cli(); - *out ^= bit; - SREG = oldSREG; -} -*/ diff --git a/metronom.ino b/metronom.ino index e2dc9ac..501be2c 100644 --- a/metronom.ino +++ b/metronom.ino @@ -32,7 +32,7 @@ void setup() { TaskTimer.attachInterruptInterval_MS(20, TasksHandler); TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, readTemperature); - TasksISRs.setInterval(PERIOD_MS, updateTube); + TasksISRs.setInterval(200, updateTube); } void runMetronome() { From 85949057480c889b1109a02511fd3182d7027469 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 9 Oct 2022 22:18:59 +0200 Subject: [PATCH 9/9] Add temperature hysteresis Do not refresh display if value not changed --- bme280.ino | 15 +++++++++++++-- metronom.ino | 1 + tube.ino | 22 +++++++++++++++------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/bme280.ino b/bme280.ino index a04becf..93271c8 100644 --- a/bme280.ino +++ b/bme280.ino @@ -1,12 +1,23 @@ #include "Seeed_BME280.h" BME280 sensor; +float highTemp = 0.0, lowTemp = 0.0; +const float blindZone = 0.03; void initBME280() { sensor.init(); } void readTemperature() { - // TODO: add hysteresis - temperature = sensor.getTemperature(); + float newTemperature = sensor.getTemperature(); + + if (newTemperature > highTemp) { + temperature = newTemperature; + highTemp = newTemperature; + lowTemp = highTemp - blindZone; + } else if (newTemperature < lowTemp) { + temperature = newTemperature; + lowTemp = newTemperature; + highTemp = lowTemp + blindZone; + }; } diff --git a/metronom.ino b/metronom.ino index 501be2c..f6a369b 100644 --- a/metronom.ino +++ b/metronom.ino @@ -32,6 +32,7 @@ void setup() { TaskTimer.attachInterruptInterval_MS(20, TasksHandler); TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, readTemperature); + // TODO: move display from ISR to loop() TasksISRs.setInterval(200, updateTube); } diff --git a/tube.ino b/tube.ino index 0eaa44f..9d998d7 100644 --- a/tube.ino +++ b/tube.ino @@ -1,22 +1,30 @@ #include "grove_alphanumeric_display.h" Seeed_Digital_Tube tube; +char tubeText[5] = ""; void initTube() { // Wire initialized by BME280 sensor tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); tube.setBrightness(15); tube.setBlinkRate(BLINK_OFF); - tube.setPoint(true,true); } void updateTube() { - char tube_str[5]; + char newText[5]; + bool highPoint = false, lowPoint = false; - if (countdown <= 0) - sprintf(tube_str, "%4u", (unsigned int) (temperature * 100.0)); - else - sprintf(tube_str, "%4u", countdown); + if (countdown <= 0) { + sprintf(newText, "%4u", (unsigned int) (temperature * 100.0)); + lowPoint = true; + } 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(); + } }