Debounce for button interrupts
This commit is contained in:
parent
096155c6cd
commit
22f8aca3ca
1 changed files with 20 additions and 2 deletions
|
@ -35,14 +35,18 @@ volatile byte state = STATE_OFF;
|
||||||
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
|
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
|
||||||
volatile unsigned long stateBegan;
|
volatile unsigned long stateBegan;
|
||||||
|
|
||||||
|
const long debouncing_time = 50; //Debouncing Time in Milliseconds
|
||||||
|
volatile unsigned long last_buttonOnPressed;
|
||||||
|
volatile unsigned long last_buttonOffPressed;
|
||||||
|
|
||||||
// wird einmalig beim Start des Arduinos ausgeführt
|
// wird einmalig beim Start des Arduinos ausgeführt
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(LED_G, OUTPUT);
|
pinMode(LED_G, OUTPUT);
|
||||||
pinMode(LED_Y, OUTPUT);
|
pinMode(LED_Y, OUTPUT);
|
||||||
pinMode(LED_R, OUTPUT);
|
pinMode(LED_R, OUTPUT);
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressed, RISING);
|
attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressedDebounce, RISING);
|
||||||
attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressed, RISING);
|
attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressedDebounce, RISING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bildet den aktuellen Zustand auf die LEDs ab
|
// bildet den aktuellen Zustand auf die LEDs ab
|
||||||
|
@ -85,11 +89,25 @@ void loop() {
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void buttonOnPressedDebounce() {
|
||||||
|
if (millis() - last_buttonOnPressed >= debouncing_time) {
|
||||||
|
buttonOnPressed();
|
||||||
|
last_buttonOnPressed = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void buttonOnPressed() {
|
void buttonOnPressed() {
|
||||||
state = STATE_ON;
|
state = STATE_ON;
|
||||||
stateBegan = millis();
|
stateBegan = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void buttonOffPressedDebounce() {
|
||||||
|
if (millis() - last_buttonOffPressed >= debouncing_time) {
|
||||||
|
buttonOffPressed();
|
||||||
|
last_buttonOffPressed = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void buttonOffPressed() {
|
void buttonOffPressed() {
|
||||||
state = STATE_OFF;
|
state = STATE_OFF;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue