From d75d7d0bbac3b5a0f00fdfa966359d3817da8574 Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Thu, 26 Mar 2015 17:34:10 -0700 Subject: [PATCH 1/7] Many updates including held() and retrigger() Updated Constructors to do all the pin() and interval() setups. All the setup can now be done in one step when instantiating the object. Added held() and retrigger(). See examples. Updated other examples to use the new constructor. --- .gitignore | 5 + Bounce2.cpp | 186 ++++++++++++++++++++++--------- Bounce2.h | 23 +++- examples/bounce/bounce.ino | 15 ++- examples/change/change.ino | 17 +-- examples/duration/duration.ino | 15 ++- examples/held/held.ino | 57 ++++++++++ examples/retrigger/retrigger.ino | 115 +++++++++---------- 8 files changed, 298 insertions(+), 135 deletions(-) create mode 100644 .gitignore create mode 100644 examples/held/held.ino diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da48d73 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +examples/retrigger/Debug/Makefile +*.atsuo +examples/retrigger/retrigger.cppproj +*.xml +examples/retrigger/Visual Micro/.retrigger.vsarduino.h \ No newline at end of file diff --git a/Bounce2.cpp b/Bounce2.cpp index afbcac7..4e00a56 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -10,83 +10,163 @@ #define DEBOUNCED_STATE 0 #define UNSTABLE_STATE 1 #define STATE_CHANGED 3 +#define STATE_HELD_ON 4 -Bounce::Bounce() - : previous_millis(0) - , interval_millis(10) - , state(0) - , pin(0) -{} +Bounce::Bounce(int pin, uint16_t interval_millis, uint16_t interval_retrigger, uint16_t interval_hold) +{ + attach(pin); + interval(interval_millis); + holdinterval(interval_hold); + retriggerinterval(interval_retrigger); + previous_millis_retrigger = 0; +} void Bounce::attach(int pin) { - this->pin = pin; - bool read = digitalRead(pin); - state = 0; - if (digitalRead(pin)) { - state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE); - } -#ifdef BOUNCE_LOCK_OUT - previous_millis = 0; -#else - previous_millis = millis(); -#endif + this->pin = pin; + bool read = digitalRead(pin); + state = 0; + if (digitalRead(pin)) { + state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE); + } + #ifdef BOUNCE_LOCK_OUT + previous_millis = 0; + #else + previous_millis = millis(); + #endif } void Bounce::interval(uint16_t interval_millis) { - this->interval_millis = interval_millis; + this->interval_millis = interval_millis; +} + +void Bounce::holdinterval(uint16_t interval_hold) +{ + this->interval_hold = interval_hold; +} + +void Bounce::retriggerinterval(uint16_t interval_retrigger) +{ + this->interval_retrigger = interval_retrigger; } bool Bounce::update() { -#ifdef BOUNCE_LOCK_OUT - state &= ~_BV(STATE_CHANGED); - // Ignore everything if we are locked out - if (millis() - previous_millis >= interval_millis) { - bool currentState = digitalRead(pin); - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { - previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); - } - } - return state & _BV(STATE_CHANGED); -#else - // Read the state of the switch in a temporary variable. - bool currentState = digitalRead(pin); - state &= ~_BV(STATE_CHANGED); + #ifdef BOUNCE_LOCK_OUT + state &= ~_BV(STATE_CHANGED); + // Ignore everything if we are locked out + if (millis() - previous_millis >= interval_millis) { + bool currentState = digitalRead(pin); + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } + } + return state & _BV(STATE_CHANGED); + #else + // Read the state of the switch in a temporary variable. + bool currentState = digitalRead(pin); + state &= ~_BV(STATE_CHANGED); - // If the reading is different from last reading, reset the debounce counter - if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) { - previous_millis = millis(); - state ^= _BV(UNSTABLE_STATE); - } else - if ( millis() - previous_millis >= interval_millis ) { - // We have passed the threshold time, so the input is now stable - // If it is different from last state, set the STATE_CHANGED flag - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { - previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); - } - } + // If the reading is different from last reading, reset the debounce counter + if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) { + previous_millis = millis(); + state ^= _BV(UNSTABLE_STATE); + } else + if ( millis() - previous_millis >= interval_millis ) { + // We have passed the threshold time, so the input is now stable + // If it is different from last state, set the STATE_CHANGED flag + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } + } + + + #endif + + //code for checking if state has been held on >= 500ms or interval_hold + + // If state has changed and has not been held on previously + if(state & _BV(STATE_CHANGED) && !(state & _BV(STATE_HELD_ON))) + { + //If state has been changed before 500ms since last change, reset state change timer + if(millis() - previous_millis_state_changed < interval_hold) + { + previous_millis_state_changed = 0; + } + else + { + previous_millis_state_changed = millis(); + } + + } + // If state has changed and has been held on previously + else if(state & _BV(STATE_CHANGED) && (state & _BV(STATE_HELD_ON))) + { + // Immediately turn off held on state + state &= ~_BV(STATE_HELD_ON); + previous_millis_state_changed = 0; + } + else if(previous_millis_state_changed != 0) + { + //if button has been at a debounced state for >= 500ms + if(millis() - previous_millis_state_changed >= interval_hold) + { + state |= _BV(STATE_HELD_ON); + previous_millis_state_changed = 0; + } + } + + + return state & _BV(STATE_CHANGED); - return state & _BV(STATE_CHANGED); -#endif } +bool Bounce::retrigger() +{ + // if button is being held + if(this->held()) + { + if(previous_millis_retrigger != 0) + { + // Checks if it has been longer that retrigger time + if (millis() - previous_millis_retrigger >= interval_retrigger) + { + previous_millis_retrigger = millis(); + return true; + } + } + else + { + // sets the millis when first going into function + previous_millis_retrigger = millis(); + return false; + + } + } +} + + bool Bounce::read() { - return state & _BV(DEBOUNCED_STATE); + return state & _BV(DEBOUNCED_STATE); } bool Bounce::rose() { - return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); + return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); } bool Bounce::fell() { - return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); + return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); +} + +bool Bounce::held() +{ + return (state & _BV(STATE_HELD_ON)); } diff --git a/Bounce2.h b/Bounce2.h index 2d44f7c..8c1059b 100644 --- a/Bounce2.h +++ b/Bounce2.h @@ -23,7 +23,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * Main code by Thomas O Fredericks (tof@t-o-f.info) - Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway + Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway, Albert Phan * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #ifdef BOUNCE_LOCK @@ -43,7 +43,7 @@ class Bounce { public: // Create an instance of the bounce library - Bounce(); + Bounce(int pin, uint16_t interval_millis = 10, uint16_t interval_retrigger = 50, uint16_t interval_hold = 500); // Attach to a pin (and also sets initial state) void attach(int pin); @@ -51,7 +51,13 @@ class Bounce // Sets the debounce interval void interval(uint16_t interval_millis); - // Updates the pin + // Changes the interval for held() + void holdinterval(uint16_t interval_hold); + + // Changes the retrigger interval for retrigger() + void retriggerinterval(uint16_t interval_retrigger); + + // Updates the pin // Returns 1 if the state changed // Returns 0 if the state did not change bool update(); @@ -64,9 +70,20 @@ class Bounce // Returns the rising pin state bool rose(); + + // Returns 1 if state has been held for held interval (default is 500ms) + // Returns 0 if not + bool held(); + + // After the button has been held down, retriggers every interval_repeat(default is 50 ms) + bool retrigger(); protected: unsigned long previous_millis; + unsigned long previous_millis_state_changed; + unsigned long previous_millis_retrigger; + uint16_t interval_hold; + uint16_t interval_retrigger; uint16_t interval_millis; uint8_t state; uint8_t pin; diff --git a/examples/bounce/bounce.ino b/examples/bounce/bounce.ino index 139620d..feef92e 100644 --- a/examples/bounce/bounce.ino +++ b/examples/bounce/bounce.ino @@ -11,18 +11,21 @@ #define BUTTON_PIN 2 #define LED_PIN 13 -// Instantiate a Bounce object -Bounce debouncer = Bounce(); +#define DEBOUNCE_TIME 5 +#define HOLD_TIME 1000 +#define RETRIGGER_TIME 100 + +// Instantiate a Bounce object with default values +Bounce debouncer(BUTTON_PIN); + +// Or set your own values when instantiating Bounce object +// Bounce debouncer(BUTTON_PIN,DEBOUNCE_TIME, RETRIGGER_TIME, HOLD_TIME); void setup() { // Setup the button with an internal pull-up : pinMode(BUTTON_PIN,INPUT_PULLUP); - // After setting up the button, setup the Bounce instance : - debouncer.attach(BUTTON_PIN); - debouncer.interval(5); // interval in ms - //Setup the LED : pinMode(LED_PIN,OUTPUT); diff --git a/examples/change/change.ino b/examples/change/change.ino index eb5fa26..9588696 100644 --- a/examples/change/change.ino +++ b/examples/change/change.ino @@ -9,20 +9,23 @@ #define BUTTON_PIN 2 #define LED_PIN 13 +#define DEBOUNCE_TIME 5 +#define HOLD_TIME 1000 +#define RETRIGGER_TIME 100 + int ledState = LOW; +// Instantiate a Bounce object with default values +Bounce debouncer(BUTTON_PIN); + +// Or set your own values when instantiating Bounce object +// Bounce debouncer(BUTTON_PIN,DEBOUNCE_TIME, RETRIGGER_TIME, HOLD_TIME); -// Instantiate a Bounce object : -Bounce debouncer = Bounce(); void setup() { // Setup the button with an internal pull-up : - pinMode(BUTTON_PIN,INPUT_PULLUP); - - // After setting up the button, setup the Bounce instance : - debouncer.attach(BUTTON_PIN); - debouncer.interval(500); + pinMode(BUTTON_PIN,INPUT); // Setup the LED : pinMode(LED_PIN,OUTPUT); diff --git a/examples/duration/duration.ino b/examples/duration/duration.ino index f0abcc5..10cf38d 100644 --- a/examples/duration/duration.ino +++ b/examples/duration/duration.ino @@ -14,8 +14,15 @@ a button press (transition from HIGH to LOW). #define BUTTON_PIN 2 #define LED_PIN 13 -// Instantiate a Bounce object : -Bounce debouncer = Bounce(); +#define DEBOUNCE_TIME 5 +#define HOLD_TIME 1000 +#define RETRIGGER_TIME 100 + +// Instantiate a Bounce object with default values +Bounce debouncer(BUTTON_PIN); + +// Or set your own values when instantiating Bounce object +// Bounce debouncer(BUTTON_PIN,DEBOUNCE_TIME, RETRIGGER_TIME, HOLD_TIME); unsigned long buttonPressTimeStamp; @@ -26,10 +33,6 @@ void setup() { // Setup the button with an internal pull-up : pinMode(BUTTON_PIN,INPUT_PULLUP); - // After setting up the button, setup the Bounce instance : - debouncer.attach(BUTTON_PIN); - debouncer.interval(5); - // Setup the LED : pinMode(LED_PIN,OUTPUT); diff --git a/examples/held/held.ino b/examples/held/held.ino new file mode 100644 index 0000000..ca57e0f --- /dev/null +++ b/examples/held/held.ino @@ -0,0 +1,57 @@ + +/* + DESCRIPTION + ==================== + Example of the held() function for the Bounce 2 library + The held() function returns a 1 when the button has been held for a minimum of the hold interval or 500ms which is the default + + An example of using this is in a menu where a user must hold a key down before he/she can edit something. + + */ +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce-Arduino-Wiring +#include + +#define BUTTON_PIN 2 +#define LED_PIN 13 + +#define DEBOUNCE_TIME 5 +#define HOLD_TIME 1000 +#define RETRIGGER_TIME 100 + +// Instantiate a Bounce object with default values +Bounce debouncer(BUTTON_PIN); + +// Or set your own values when instantiating Bounce object +// Bounce debouncer(BUTTON_PIN,DEBOUNCE_TIME, RETRIGGER_TIME, HOLD_TIME); + + +void setup() { + + // Setup the button with an internal pull-up : + //pinMode(BUTTON_PIN,INPUT_PULLUP); + pinMode(BUTTON_PIN, INPUT); + + //Setup the LED : + pinMode(LED_PIN,OUTPUT); + +} + +void loop() { + // Update the Bounce instance : + debouncer.update(); + + // Get the updated value : + int value = debouncer.held(); + + // Turn on or off the LED as determined by the state : + if (value) { + digitalWrite(LED_PIN, HIGH ); + } + else { + digitalWrite(LED_PIN, LOW ); + } + +} + + diff --git a/examples/retrigger/retrigger.ino b/examples/retrigger/retrigger.ino index b1143ee..4604b74 100644 --- a/examples/retrigger/retrigger.ino +++ b/examples/retrigger/retrigger.ino @@ -1,86 +1,81 @@ -/* +/* DESCRIPTION ==================== -Example of the bounce library that shows how to retrigger an event when a button is held down. -In this case, the debug LED will blink every 500 ms as long as the button is held down. +Example of the retrigger() function of the Bounce 2 library. This function will retrigger a HIGH value after +the button has been held on for at least the hold interval (default 500 ms) and then retrigger every retrigger_interval afterwards (default 50ms) +This function is great for editing values without repeatedly pressing the button. Open the Serial Monitor (57600 baud) for debug messages. */ // Include the Bounce2 library found here : // https://github.com/thomasfredericks/Bounce-Arduino-Wiring -#include +#include #define BUTTON_PIN 2 #define LED_PIN 13 -// Instantiate a Bounce object -Bounce debouncer = Bounce(); +#define DEBOUNCE_TIME 5 +#define HOLD_TIME 1000 +#define RETRIGGER_TIME 100 + +// Instantiate a Bounce object with default values +Bounce debouncer(BUTTON_PIN); + +// Or set your own values when instantiating Bounce object +// Bounce debouncer(BUTTON_PIN,DEBOUNCE_TIME, RETRIGGER_TIME, HOLD_TIME); -int buttonState; unsigned long buttonPressTimeStamp; - -int ledState; +unsigned char ledState = 0; void setup() { - - Serial.begin(57600); - - // Setup the button - pinMode(BUTTON_PIN,INPUT); - // Activate internal pull-up - digitalWrite(BUTTON_PIN,HIGH); - - // After setting up the button, setup debouncer - debouncer.attach(BUTTON_PIN); - debouncer.interval(5); - - //Setup the LED - pinMode(LED_PIN,OUTPUT); - digitalWrite(LED_PIN,ledState); - + + Serial.begin(57600); + + // Setup the button + pinMode(BUTTON_PIN,INPUT); + + //Setup the LED + pinMode(LED_PIN,OUTPUT); + digitalWrite(LED_PIN,LOW); + } void loop() { - // Update the debouncer and get the changed state - boolean changed = debouncer.update(); + // Update the debouncer + debouncer.update(); + if(debouncer.rose()) + { + ledState = 1; + digitalWrite(LED_PIN, ledState); + buttonPressTimeStamp = millis(); + Serial.print(buttonPressTimeStamp); + Serial.println(" Button rose!"); + } + // toggle led everytime it retriggers + if (debouncer.retrigger()) + { + buttonPressTimeStamp = millis(); + Serial.print(buttonPressTimeStamp); + Serial.println(" Button retriggered!"); + + ledState = !ledState; + digitalWrite(LED_PIN, ledState); + } + + if(debouncer.fell()) + { + ledState = 0; + digitalWrite(LED_PIN, ledState); + buttonPressTimeStamp = millis(); + Serial.print(buttonPressTimeStamp); + Serial.println(" Button fell!"); + } - - if ( changed ) { - // Get the update value - int value = debouncer.read(); - if ( value == HIGH) { - ledState = LOW; - digitalWrite(LED_PIN, ledState ); - - buttonState = 0; - Serial.println("Button released (state 0)"); - - } else { - ledState = HIGH; - digitalWrite(LED_PIN, ledState ); - - buttonState = 1; - Serial.println("Button pressed (state 1)"); - buttonPressTimeStamp = millis(); - - } - } - - if ( buttonState == 1 ) { - if ( millis() - buttonPressTimeStamp >= 500 ) { - buttonPressTimeStamp = millis(); - if ( ledState == HIGH ) ledState = LOW; - else if ( ledState == LOW ) ledState = HIGH; - digitalWrite(LED_PIN, ledState ); - Serial.println("Retriggering button"); - } - } - - + } -- 2.54.0 From de9fb2d9c028cf6b56ef68014d1d794092ea56ee Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Fri, 27 Mar 2015 10:16:17 -0700 Subject: [PATCH 2/7] update() calls attach() on first run attach() is now called the first time update() is run so the pin setup is before attach(). I also removed previous_millis_retrigger to save 4 bytes. Also updated retrigger.ino example with a few more comments. --- .gitignore | 3 +- Bounce2.cpp | 139 ++++++++++++++++--------------- Bounce2.h | 3 +- examples/retrigger/retrigger.ino | 10 +-- 4 files changed, 81 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index da48d73..07a5bf6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ examples/retrigger/Debug/Makefile *.atsuo examples/retrigger/retrigger.cppproj *.xml -examples/retrigger/Visual Micro/.retrigger.vsarduino.h \ No newline at end of file +examples/retrigger/Visual Micro/.retrigger.vsarduino.h +*.a \ No newline at end of file diff --git a/Bounce2.cpp b/Bounce2.cpp index 4e00a56..7fe2d7f 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -11,15 +11,17 @@ #define UNSTABLE_STATE 1 #define STATE_CHANGED 3 #define STATE_HELD_ON 4 +#define FIRSTUPDATE 5 Bounce::Bounce(int pin, uint16_t interval_millis, uint16_t interval_retrigger, uint16_t interval_hold) { - attach(pin); + this->pin = pin; interval(interval_millis); holdinterval(interval_hold); retriggerinterval(interval_retrigger); previous_millis_retrigger = 0; + state = _BV(FIRSTUPDATE); } void Bounce::attach(int pin) { @@ -53,77 +55,82 @@ void Bounce::retriggerinterval(uint16_t interval_retrigger) bool Bounce::update() { - #ifdef BOUNCE_LOCK_OUT - state &= ~_BV(STATE_CHANGED); - // Ignore everything if we are locked out - if (millis() - previous_millis >= interval_millis) { + // Attaches the pin the first time update is called + if(state & _BV(FIRSTUPDATE)) + { + attach(pin); + state &= _BV(FIRSTUPDATE); + } + else + { + #ifdef BOUNCE_LOCK_OUT + state &= ~_BV(STATE_CHANGED); + // Ignore everything if we are locked out + if (millis() - previous_millis >= interval_millis) { + bool currentState = digitalRead(pin); + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } + } + return state & _BV(STATE_CHANGED); + #else + // Read the state of the switch in a temporary variable. bool currentState = digitalRead(pin); - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { - previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); - } - } - return state & _BV(STATE_CHANGED); - #else - // Read the state of the switch in a temporary variable. - bool currentState = digitalRead(pin); - state &= ~_BV(STATE_CHANGED); + state &= ~_BV(STATE_CHANGED); - // If the reading is different from last reading, reset the debounce counter - if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) { - previous_millis = millis(); - state ^= _BV(UNSTABLE_STATE); - } else - if ( millis() - previous_millis >= interval_millis ) { - // We have passed the threshold time, so the input is now stable - // If it is different from last state, set the STATE_CHANGED flag - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + // If the reading is different from last reading, reset the debounce counter + if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) { previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); + state ^= _BV(UNSTABLE_STATE); + } else + if ( millis() - previous_millis >= interval_millis ) { + // We have passed the threshold time, so the input is now stable + // If it is different from last state, set the STATE_CHANGED flag + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } } - } - - #endif - - //code for checking if state has been held on >= 500ms or interval_hold - - // If state has changed and has not been held on previously - if(state & _BV(STATE_CHANGED) && !(state & _BV(STATE_HELD_ON))) - { - //If state has been changed before 500ms since last change, reset state change timer - if(millis() - previous_millis_state_changed < interval_hold) - { - previous_millis_state_changed = 0; - } - else - { - previous_millis_state_changed = millis(); - } - } - // If state has changed and has been held on previously - else if(state & _BV(STATE_CHANGED) && (state & _BV(STATE_HELD_ON))) - { - // Immediately turn off held on state - state &= ~_BV(STATE_HELD_ON); - previous_millis_state_changed = 0; - } - else if(previous_millis_state_changed != 0) - { - //if button has been at a debounced state for >= 500ms - if(millis() - previous_millis_state_changed >= interval_hold) + #endif + + //code for checking if state has been held on >= 500ms or interval_hold + + // If state has changed and has not been held on previously + if(state & _BV(STATE_CHANGED) && !(state & _BV(STATE_HELD_ON))) { - state |= _BV(STATE_HELD_ON); + //If state has been changed before 500ms since last change, reset state change timer + if(millis() - previous_millis_state_changed < interval_hold) + { + previous_millis_state_changed = 0; + } + else + { + previous_millis_state_changed = millis(); + } + } + // If state has changed and has been held on previously + else if(state & _BV(STATE_CHANGED) && (state & _BV(STATE_HELD_ON))) + { + // Immediately turn off held on state + state &= ~_BV(STATE_HELD_ON); previous_millis_state_changed = 0; } + else if(previous_millis_state_changed != 0) + { + //if button has been at a debounced state for >= 500ms + if(millis() - previous_millis_state_changed >= interval_hold) + { + state |= _BV(STATE_HELD_ON); + previous_millis_state_changed = 0; + } + } + return state & _BV(STATE_CHANGED); } - - - return state & _BV(STATE_CHANGED); - } bool Bounce::retrigger() @@ -131,19 +138,19 @@ bool Bounce::retrigger() // if button is being held if(this->held()) { - if(previous_millis_retrigger != 0) + if(previous_millis_state_changed != 0) { // Checks if it has been longer that retrigger time - if (millis() - previous_millis_retrigger >= interval_retrigger) + if (millis() - previous_millis_state_changed >= interval_retrigger) { - previous_millis_retrigger = millis(); + previous_millis_state_changed = millis(); return true; } } else { // sets the millis when first going into function - previous_millis_retrigger = millis(); + previous_millis_state_changed = millis(); return false; } diff --git a/Bounce2.h b/Bounce2.h index 8c1059b..a21e926 100644 --- a/Bounce2.h +++ b/Bounce2.h @@ -75,13 +75,12 @@ class Bounce // Returns 0 if not bool held(); - // After the button has been held down, retriggers every interval_repeat(default is 50 ms) + // After the button has been held down, returns a 1 every interval_repeat(default is 50 ms) bool retrigger(); protected: unsigned long previous_millis; unsigned long previous_millis_state_changed; - unsigned long previous_millis_retrigger; uint16_t interval_hold; uint16_t interval_retrigger; uint16_t interval_millis; diff --git a/examples/retrigger/retrigger.ino b/examples/retrigger/retrigger.ino index 4604b74..527bd31 100644 --- a/examples/retrigger/retrigger.ino +++ b/examples/retrigger/retrigger.ino @@ -31,7 +31,6 @@ unsigned long buttonPressTimeStamp; unsigned char ledState = 0; void setup() { - Serial.begin(57600); // Setup the button @@ -47,15 +46,16 @@ void loop() { // Update the debouncer debouncer.update(); + // If button had a rising transition, turn on led. if(debouncer.rose()) { ledState = 1; digitalWrite(LED_PIN, ledState); - buttonPressTimeStamp = millis(); + buttonPressTimeStamp = millis(); Serial.print(buttonPressTimeStamp); Serial.println(" Button rose!"); } - // toggle led everytime it retriggers + // After holding down the button, retrigger returns a 1 every interval_hold (default = 50 ms) if (debouncer.retrigger()) { buttonPressTimeStamp = millis(); @@ -65,12 +65,12 @@ void loop() { ledState = !ledState; digitalWrite(LED_PIN, ledState); } - + // When button has a falling transition, turn off led. if(debouncer.fell()) { ledState = 0; digitalWrite(LED_PIN, ledState); - buttonPressTimeStamp = millis(); + buttonPressTimeStamp = millis(); Serial.print(buttonPressTimeStamp); Serial.println(" Button fell!"); } -- 2.54.0 From fc35d18bd2e927ecd1155a2a93917d17d7c79020 Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Fri, 27 Mar 2015 10:17:49 -0700 Subject: [PATCH 3/7] updated gitignore --- .gitignore | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 07a5bf6..ce68319 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,11 @@ examples/retrigger/Debug/Makefile examples/retrigger/retrigger.cppproj *.xml examples/retrigger/Visual Micro/.retrigger.vsarduino.h -*.a \ No newline at end of file +*.a +*.eep +examples/held/Debug/held.elf +*.hex +examples/held/held.cppproj +examples/retrigger/Debug/retrigger.elf +*.atsln +examples/held/Visual Micro/.held.vsarduino.h \ No newline at end of file -- 2.54.0 From 06e5982b3cce25de42caa58b49d1b523c6a6fe8a Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Fri, 27 Mar 2015 16:23:22 -0700 Subject: [PATCH 4/7] Small change I forgot --- Bounce2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bounce2.cpp b/Bounce2.cpp index 7fe2d7f..f362e56 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -20,7 +20,7 @@ Bounce::Bounce(int pin, uint16_t interval_millis, uint16_t interval_retrigger, u interval(interval_millis); holdinterval(interval_hold); retriggerinterval(interval_retrigger); - previous_millis_retrigger = 0; + previous_millis_state_changed = 0; state = _BV(FIRSTUPDATE); } -- 2.54.0 From 05f2d4257726fc768f20862ba3239096c4d5dceb Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Fri, 27 Mar 2015 19:02:14 -0700 Subject: [PATCH 5/7] Fixed retrigger() Function wasn't properly returning a false when held() was low. --- Bounce2.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Bounce2.cpp b/Bounce2.cpp index f362e56..d75ddc6 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -155,6 +155,7 @@ bool Bounce::retrigger() } } + return false; } -- 2.54.0 From c5e009cc0db459677028df54b9fa6bb040c7a56f Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Sat, 28 Mar 2015 15:01:22 -0700 Subject: [PATCH 6/7] Removed unnecessary return --- Bounce2.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/Bounce2.cpp b/Bounce2.cpp index d75ddc6..b07fc65 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -151,8 +151,6 @@ bool Bounce::retrigger() { // sets the millis when first going into function previous_millis_state_changed = millis(); - return false; - } } return false; -- 2.54.0 From 2977f97058d3d6d62d9eee904cdc732d7b50f6a3 Mon Sep 17 00:00:00 2001 From: Albert Phan Date: Sat, 7 Jan 2017 13:58:41 -0800 Subject: [PATCH 7/7] Fixed bug with update constantly calling attach(pin) This caused people to use retrigger() to have it constantly return a 1 when using active low buttons. --- Bounce2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bounce2.cpp b/Bounce2.cpp index b07fc65..66eb38a 100644 --- a/Bounce2.cpp +++ b/Bounce2.cpp @@ -59,7 +59,7 @@ bool Bounce::update() if(state & _BV(FIRSTUPDATE)) { attach(pin); - state &= _BV(FIRSTUPDATE); + state &= ~_BV(FIRSTUPDATE); } else { -- 2.54.0