Compare commits

..

11 Commits

Author SHA1 Message Date
cryptogopher
6a0a954814 Update countdown 2026-02-07 16:58:15 +01:00
cryptogopher
d6f9bb6634 Add some TODOs 2022-10-10 15:40:01 +02:00
cryptogopher
1e206b248c Merge branch 'timerisr' 2022-10-09 22:25:05 +02:00
cryptogopher
8594905748 Add temperature hysteresis
Do not refresh display if value not changed
2022-10-09 22:18:59 +02:00
cryptogopher
814a99f733 Remove buzzer state variable 2022-10-09 21:48:16 +02:00
cryptogopher
7fe04d76cb Extract buzzer to separate file 2022-10-09 21:36:40 +02:00
cryptogopher
f8878d3c37 Replace tone() with custom timer 2022-10-09 20:34:25 +02:00
cryptogopher
374a72fb07 Move tube to separate file 2022-10-09 15:29:30 +02:00
cryptogopher
21512bc4f4 Add temperature and break countdown display 2022-10-09 15:09:52 +02:00
cryptogopher
3872ece37b Join ISRs 2022-10-09 14:40:33 +02:00
cryptogopher
315aa9996c Update break time and beep freq 2022-10-09 14:26:33 +02:00
5 changed files with 99 additions and 14 deletions

23
bme280.ino Normal file
View File

@@ -0,0 +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() {
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;
};
}

View File

@@ -1,5 +1,7 @@
volatile unsigned long lastButtonPress = millis(); volatile unsigned long lastButtonPress = millis();
// Debouncing na timerze:
// 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);
// Order of ISRs matter: RISING should be invoked first // Order of ISRs matter: RISING should be invoked first
@@ -10,12 +12,15 @@ void initButton() {
void buttonISRstate() { void buttonISRstate() {
if ((millis() - lastButtonPress) > 100) { if ((millis() - lastButtonPress) > 100) {
if (countdown < 0) if (countdown < 0)
// TODO: enable metronome timer
countdown = 0; countdown = 0;
else if (countdown == 0) else if (countdown == 0)
// TODO: restart metronome timer to align countdown
countdown = COUNTDOWN; countdown = COUNTDOWN;
else { else {
countdown = -1; countdown = -1;
digitalWrite(BUTTON_LED_PIN, LOW); digitalWrite(BUTTON_LED_PIN, LOW);
// TODO: disable metronome timer
} }
} }
} }

22
buzzer.ino Normal file
View File

@@ -0,0 +1,22 @@
SAMDTimer BuzzerTimer(TIMER_TC5);
bool attached = false;
void initBuzzer() {
pinMode(BUZZER_PIN, OUTPUT);
}
void buzzerISR() {
digitalWrite(BUZZER_PIN, !digitalRead(BUZZER_PIN));
}
void buzz() {
if (attached)
BuzzerTimer.enableTimer();
else
BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzzerISR);
}
void noBuzz() {
BuzzerTimer.disableTimer();
}

View File

@@ -1,14 +1,15 @@
#include "SAMDTimerInterrupt.h" #include "SAMDTimerInterrupt.h"
#include "SAMD_ISR_Timer.h" #include "SAMD_ISR_Timer.h"
#define HW_TIMER_INTERVAL_MS 10 SAMDTimer TaskTimer(TIMER_TC3);
SAMDTimer ITimer(TIMER_TC3); SAMD_ISR_Timer TasksISRs;
SAMD_ISR_Timer ISR_Timer;
void TimerHandler(void) { ISR_Timer.run(); } void TasksHandler(void) { TasksISRs.run(); }
const int COUNTDOWN = 300;
const int COUNTDOWN = 600;
const int PERIOD_MS = 1000; const int PERIOD_MS = 1000;
const int 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;
@@ -20,26 +21,30 @@ const uint BUZZER_PIN = 0;
* >0 - COUNTDOWN * >0 - COUNTDOWN
*/ */
volatile int countdown = -1; volatile int countdown = -1;
volatile float temperature = 0.0;
void setup() { void setup() {
initButton(); initButton();
initBME280();
initBuzzer();
initTube();
ITimer.attachInterruptInterval_MS(10, TimerHandler); TaskTimer.attachInterruptInterval_MS(20, TasksHandler);
ISR_Timer.setInterval(PERIOD_MS, metronomeBeat); TasksISRs.setInterval(PERIOD_MS, runMetronome);
ISR_Timer.setInterval(PERIOD_MS, metronomeCountdown); TasksISRs.setInterval(PERIOD_MS, readTemperature);
// TODO: move display from ISR to loop()
TasksISRs.setInterval(200, updateTube);
} }
void metronomeCountdown() { void runMetronome() {
if (countdown > 0) { if (countdown > 0) {
countdown -= 1; countdown -= 1;
digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW);
} else if (countdown == 0) {
TasksISRs.setTimeout(100, noBuzz);
buzz();
} }
} }
void metronomeBeat() {
if (countdown == 0)
tone(BUZZER_PIN, 1000, 100);
}
void loop() {} void loop() {}

30
tube.ino Normal file
View File

@@ -0,0 +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);
}
void updateTube() {
char newText[5];
bool highPoint = false, lowPoint = false;
if (countdown <= 0) {
sprintf(newText, "%4u", (unsigned int) (temperature * 100.0));
lowPoint = true;
} else {
sprintf(newText, "%4u", countdown);
}
if (strcmp(tubeText, newText)) {
strcpy(tubeText, newText);
tube.displayString(tubeText);
tube.setPoint(highPoint, lowPoint);
tube.display();
}
}