From 31dec770d7562aced48038d07d5aba6df9d44cb4 Mon Sep 17 00:00:00 2001 From: Martin Ness Date: Tue, 1 Sep 2015 17:41:24 +0200 Subject: [PATCH 1/4] use interrupts for the buttons --- status/status.ino | 67 +++++++++-------------------------------------- 1 file changed, 13 insertions(+), 54 deletions(-) diff --git a/status/status.ino b/status/status.ino index 90bd397..7ffa80b 100644 --- a/status/status.ino +++ b/status/status.ino @@ -22,33 +22,18 @@ // Ein-/Ausgänge Bezeichnen const int BTN_ON = 2; // Einschalter +#define INTERRUPT_NAME_BTN_ON 0 const int BTN_OFF = 3; // Ausschalter +#define INTERRUPT_NAME_BTN_OFF 1 const int LED_G = 9; // grüne LED const int LED_Y = 8; // gelbe LED const int LED_R = 7; // rote LED // hier wird der aktuelle Zustand gespeichert -byte state = STATE_OFF; +volatile byte state = STATE_OFF; // hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime. -unsigned long stateBegan; - -// Debouncer -class Debounce -{ - public: - Debounce(int pin); - boolean update(); - int read(); - private: - int _pin; - int _state; - int _time; - int _delay; -}; - -Debounce debounceBtnOn(BTN_ON); -Debounce debounceBtnOff(BTN_OFF); +volatile unsigned long stateBegan; // wird einmalig beim Start des Arduinos ausgeführt void setup() { @@ -57,6 +42,8 @@ void setup() { pinMode(LED_R, OUTPUT); Serial.begin(9600); setStateOnLeds(); + attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressed, RISING); + attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressed, RISING); } // bildet den aktuellen Zustand auf die LEDs ab @@ -77,18 +64,7 @@ unsigned long calcStateTime() { // wird nach dem Starten dauerhaft ausgeführt void loop() { - // Einschalter auslesen - if (debounceBtnOn.update() && debounceBtnOn.read()) { - state = STATE_ON; - stateBegan = millis(); - setStateOnLeds(); - } - // Ausschalter auslesen - if (debounceBtnOff.update() && debounceBtnOff.read()) { - state = STATE_OFF; - setStateOnLeds(); - } - + setStateOnLeds(); // Auswertung des aktuellen Zustandes // ggf Zustand wechseln if (state == STATE_ON) { @@ -110,28 +86,11 @@ void loop() { delay(10); } -// Debouncer Klasse -Debounce::Debounce(int pin) -{ - pinMode(pin, INPUT); - this->_pin = pin; - this->_time = 0; - this->_state = LOW; - this->_delay = 50; +void buttonOnPressed() { + state = STATE_OM; + stateBegan = millis(); } -boolean Debounce::update() -{ - if (millis() - this->_time >= this->_delay) { - int reading = digitalRead(this->_pin); - if (reading != this->_state) { - this->_time = millis(); - this->_state = reading; - return true; - } - } - return false; -} -int Debounce::read() -{ - return this->_state; + +void buttonOffPressed() { + state = STATE_OFF; } From 59ea61af0ff3d0a9be6c97a41ddec5a1eec12781 Mon Sep 17 00:00:00 2001 From: Martin Ness Date: Thu, 24 Sep 2015 13:49:22 +0200 Subject: [PATCH 2/4] typo FIX --- status/status.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status/status.ino b/status/status.ino index 7ffa80b..3efb24b 100644 --- a/status/status.ino +++ b/status/status.ino @@ -87,7 +87,7 @@ void loop() { } void buttonOnPressed() { - state = STATE_OM; + state = STATE_ON; stateBegan = millis(); } From 096155c6cdf9f4b509cc9699dcf610f176821cc3 Mon Sep 17 00:00:00 2001 From: Martin Ness Date: Thu, 24 Sep 2015 13:49:55 +0200 Subject: [PATCH 3/4] cleanup --- status/status.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/status/status.ino b/status/status.ino index 3efb24b..a1b55ad 100644 --- a/status/status.ino +++ b/status/status.ino @@ -41,7 +41,6 @@ void setup() { pinMode(LED_Y, OUTPUT); pinMode(LED_R, OUTPUT); Serial.begin(9600); - setStateOnLeds(); attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressed, RISING); attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressed, RISING); } From 22f8aca3ca67c349f8d402716660c7d45a536dc0 Mon Sep 17 00:00:00 2001 From: Martin Ness Date: Mon, 26 Oct 2015 21:40:51 +0100 Subject: [PATCH 4/4] Debounce for button interrupts --- status/status.ino | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/status/status.ino b/status/status.ino index a1b55ad..68f36f5 100644 --- a/status/status.ino +++ b/status/status.ino @@ -35,14 +35,18 @@ volatile byte state = STATE_OFF; // hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime. 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 void setup() { pinMode(LED_G, OUTPUT); pinMode(LED_Y, OUTPUT); pinMode(LED_R, OUTPUT); Serial.begin(9600); - attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressed, RISING); - attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressed, RISING); + attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressedDebounce, RISING); + attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressedDebounce, RISING); } // bildet den aktuellen Zustand auf die LEDs ab @@ -85,11 +89,25 @@ void loop() { delay(10); } +void buttonOnPressedDebounce() { + if (millis() - last_buttonOnPressed >= debouncing_time) { + buttonOnPressed(); + last_buttonOnPressed = millis(); + } +} + void buttonOnPressed() { state = STATE_ON; stateBegan = millis(); } +void buttonOffPressedDebounce() { + if (millis() - last_buttonOffPressed >= debouncing_time) { + buttonOffPressed(); + last_buttonOffPressed = millis(); + } +} + void buttonOffPressed() { state = STATE_OFF; }