Files
metronom/button.ino
2022-10-09 20:34:25 +02:00

28 lines
768 B
C++

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
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRtime, CHANGE);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING);
}
void buttonISRstate() {
if ((millis() - lastButtonPress) > 100) {
if (countdown < 0)
countdown = 0;
else if (countdown == 0)
countdown = COUNTDOWN;
else {
countdown = -1;
digitalWrite(BUTTON_LED_PIN, LOW);
}
}
}
void buttonISRtime() {
lastButtonPress = millis();
}