From 0a30a709f075aca4e10b5140633d2a05e8660b49 Mon Sep 17 00:00:00 2001 From: ChrisGallagher Date: Wed, 20 Apr 2016 22:08:43 +0100 Subject: [PATCH] Add compile option: BOUNCE_WITH_PROMPT_DETECTION New compile time opiton for alternative 'prompt' debounce algorithm. Button state changes are avaliable imideatley, so long as the previous state has been stable for the timeout period. Otherwise the state will be updated when the timeout period allows. - Able to report acurate switch times normally with no delay. - Compatable with exising usage. - Use when prompt switch transition timing is important. --- Bounce2.cpp | 31 ++++++++++++++++++++++++++++++- Bounce2.h | 10 ++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Bounce2.cpp b/Bounce2.cpp index c947815..f6470d8 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -21,7 +21,6 @@ Bounce::Bounce() void Bounce::attach(int pin) { this->pin = pin; - bool read = digitalRead(pin); state = 0; if (digitalRead(pin)) { state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE); @@ -58,6 +57,36 @@ bool Bounce::update() } } return state & _BV(STATE_CHANGED); + +#elif defined BOUNCE_WITH_PROMPT_DETECTION + // Read the state of the switch port into a temporary variable. + bool readState = digitalRead(pin); + + // Clear Changed State Flag - will be reset if we confirm a button state change. + state &= ~_BV(STATE_CHANGED); + + if ( readState != (bool)(state & _BV(DEBOUNCED_STATE))) { + // We have seen a change from the current button state. + + if ( millis() - previous_millis >= interval_millis ) { + // We have passed the time threshold, so a new change of state is allowed. + // set the STATE_CHANGED flag and the new DEBOUNCED_STATE. + // This will be prompt as long as there has been greater than interval_misllis ms since last change of input. + // Otherwise debounced state will not change again until bouncing is stable for the timeout period. + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } + } + + // If the readState is different from previous readState, reset the debounce timer - as input is still unstable + // and we want to prevent new button state changes until the previous one has remained stable for the timeout. + if ( readState != (bool)(state & _BV(UNSTABLE_STATE)) ) { + // Update Unstable Bit to macth readState + state ^= _BV(UNSTABLE_STATE); + previous_millis = millis(); + } + // return just the sate changed bit + return state & _BV(STATE_CHANGED); #else // Read the state of the switch in a temporary variable. bool currentState = digitalRead(pin); diff --git a/Bounce2.h b/Bounce2.h index 16e411f..cf0dee8 100644 --- a/Bounce2.h +++ b/Bounce2.h @@ -26,16 +26,14 @@ Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#ifdef BOUNCE_LOCK -#error You are using the invalid BOUNCE_LOCK-OUT define. Please update your sources to use BOUNCE_LOCK_OUT -#endif +#ifndef Bounce2_h +#define Bounce2_h // Uncomment the following line for "LOCK-OUT" debounce method //#define BOUNCE_LOCK_OUT - -#ifndef Bounce2_h -#define Bounce2_h +// Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method +//#define BOUNCE_WITH_PROMPT_DETECTION #include -- 2.54.0