Files
metronom/button.ino
cryptogopher c2af98d588 Display also humidity and pressure
Display update triggered by value change, not interval
2026-02-12 00:10:45 +01:00

34 lines
1.0 KiB
C++

volatile unsigned long lastButtonPress = millis();
// Debouncing using timer:
// 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
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRtime, CHANGE);
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() {
if ((millis() - lastButtonPress) > 100) {
if (countdown < 0)
// TODO: enable metronome timer
countdown = 0;
else if (countdown == 0) {
// TODO: restart metronome timer to align countdown
countdown = COUNTDOWN;
updateTube();
} else {
countdown = -1;
digitalWrite(BUTTON_LED_PIN, LOW);
updateTube();
// TODO: disable metronome timer
}
}
}
void buttonISRtime() {
lastButtonPress = millis();
}