34 lines
1.0 KiB
C++
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();
|
|
}
|