From 8516aeac010528e6d605c148d48b9f14f508d63c Mon Sep 17 00:00:00 2001 From: thomasfredericks Date: Tue, 30 Sep 2014 11:16:11 -0400 Subject: [PATCH] Added fell and rose methods --- Bounce2/Bounce2.cpp | 10 ++++ Bounce2/Bounce2.h | 4 ++ Bounce2/examples/bounce/bounce.ino | 62 ++++++++++----------- Bounce2/examples/change/change.ino | 42 +++++++-------- Bounce2/examples/duration/duration.ino | 69 ++++++++---------------- Bounce2/examples/retrigger/retrigger.ino | 11 ++-- Bounce2/keywords.txt | 29 ++++++++++ 7 files changed, 120 insertions(+), 107 deletions(-) create mode 100644 Bounce2/keywords.txt diff --git a/Bounce2/Bounce2.cpp b/Bounce2/Bounce2.cpp index 9602aa1..3f15364 100644 --- a/Bounce2/Bounce2.cpp +++ b/Bounce2/Bounce2.cpp @@ -12,6 +12,7 @@ #define UNSTABLE_STATE 1 #define STATE_CHANGED 3 + Bounce::Bounce() : previous_millis(0) , interval_millis(10) @@ -81,3 +82,12 @@ bool Bounce::read() return state & _BV(DEBOUNCED_STATE); } +bool Bounce::rose() +{ + return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); +} + +bool Bounce::fell() +{ + return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); +} diff --git a/Bounce2/Bounce2.h b/Bounce2/Bounce2.h index 4ac03bb..59b5f13 100644 --- a/Bounce2/Bounce2.h +++ b/Bounce2/Bounce2.h @@ -54,6 +54,10 @@ public: bool update(); // Returns the updated pin state bool read(); + // Returns the falling pin state + bool fell(); + // Returns the rising pin state + bool rose(); protected: diff --git a/Bounce2/examples/bounce/bounce.ino b/Bounce2/examples/bounce/bounce.ino index a845848..139620d 100644 --- a/Bounce2/examples/bounce/bounce.ino +++ b/Bounce2/examples/bounce/bounce.ino @@ -1,52 +1,48 @@ -#include /* -DESCRIPTION -==================== -Simple example of the bounce library that switches the debug LED when a button is pressed. - -CIRCUIT -==================== -https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/Bounce/examples/circuit-bounce-change-duration-retrigger.png -*/ - + DESCRIPTION + ==================== + Simple example of the Bounce library that switches the debug LED when a button is pressed. + */ +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce-Arduino-Wiring +#include #define BUTTON_PIN 2 #define LED_PIN 13 - - // Instantiate a Bounce object Bounce debouncer = Bounce(); void setup() { - // Setup the button - pinMode(BUTTON_PIN,INPUT); - // Activate internal pull-up - digitalWrite(BUTTON_PIN,HIGH); - - // After setting up the button, setup debouncer + + // 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 + debouncer.interval(5); // interval in ms + + //Setup the LED : pinMode(LED_PIN,OUTPUT); - + } void loop() { - // Update the debouncer + // Update the Bounce instance : debouncer.update(); - - // Get the update value - int value = debouncer.read(); - - // Turn on or off the LED - if ( value == HIGH) { - digitalWrite(LED_PIN, HIGH ); - } else { + + // Get the updated value : + int value = debouncer.read(); + + // Turn on or off the LED as determined by the state : + if ( value == LOW ) { + digitalWrite(LED_PIN, HIGH ); + } + else { digitalWrite(LED_PIN, LOW ); - } - + } + } + diff --git a/Bounce2/examples/change/change.ino b/Bounce2/examples/change/change.ino index 93f5703..eb5fa26 100644 --- a/Bounce2/examples/change/change.ino +++ b/Bounce2/examples/change/change.ino @@ -1,9 +1,10 @@ -#include // Detect the falling edge -// Circuit : -// https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/Bounce/examples/circuit-bounce-change-duration-retrigger.png +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce-Arduino-Wiring +#include + #define BUTTON_PIN 2 #define LED_PIN 13 @@ -11,39 +12,36 @@ int ledState = LOW; -// Instantiate a Bounce object +// Instantiate a Bounce object : Bounce debouncer = Bounce(); void setup() { - // Setup the button - pinMode(BUTTON_PIN,INPUT); - digitalWrite(BUTTON_PIN,HIGH); - // After setting up the button, setup Bounce object + // 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); - //Setup the LED + // Setup the LED : pinMode(LED_PIN,OUTPUT); digitalWrite(LED_PIN,ledState); + } void loop() { - - boolean stateChanged = debouncer.update(); - int state = debouncer.read(); - - // Detect the falling edge - if ( stateChanged && state == LOW ) { - - if ( ledState == LOW ) { - ledState = HIGH; - } else { - ledState = LOW; - } - digitalWrite(LED_PIN,ledState); + // Update the Bounce instance : + debouncer.update(); + + // Call code if Bounce fell (transition from HIGH to LOW) : + if ( debouncer.fell() ) { + + // Toggle LED state : + ledState = !ledState; + digitalWrite(LED_PIN,ledState); } } diff --git a/Bounce2/examples/duration/duration.ino b/Bounce2/examples/duration/duration.ino index ec2796b..f0abcc5 100644 --- a/Bounce2/examples/duration/duration.ino +++ b/Bounce2/examples/duration/duration.ino @@ -1,78 +1,53 @@ -#include - /* DESCRIPTION ==================== -Simple example of the bounce library that sets three states for a button through the duration of the press. -Open the Serial Monitor (57600 baud) for debug messages. +Reports through serial (57600 baud) the time since +a button press (transition from HIGH to LOW). -CIRCUIT -==================== -https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/Bounce/examples/circuit-bounce-change-duration-retrigger.png */ +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce-Arduino-Wiring +#include + + #define BUTTON_PIN 2 #define LED_PIN 13 -// Instantiate a Bounce object +// Instantiate a Bounce object : Bounce debouncer = Bounce(); -// The current buttonState -// 0 : released -// 1 : pressed less than 2 seconds -// 2 : pressed longer than 2 seconds -int buttonState; unsigned long buttonPressTimeStamp; void setup() { Serial.begin(57600); - // Setup the button - pinMode(BUTTON_PIN,INPUT); - // Activate internal pull-up - digitalWrite(BUTTON_PIN,HIGH); + // Setup the button with an internal pull-up : + pinMode(BUTTON_PIN,INPUT_PULLUP); - // After setting up the button, setup debouncer + // After setting up the button, setup the Bounce instance : debouncer.attach(BUTTON_PIN); debouncer.interval(5); - //Setup the LED + // Setup the LED : pinMode(LED_PIN,OUTPUT); } void loop() { - // Update the debouncer and get the changed state - boolean changed = debouncer.update(); - - - if ( changed ) { - // Get the update value - int value = debouncer.read(); - if ( value == HIGH) { - digitalWrite(LED_PIN, HIGH ); - - buttonState = 0; - Serial.println("Button released (state 0)"); - - } else { - digitalWrite(LED_PIN, LOW ); - buttonState = 1; - Serial.println("Button pressed (state 1)"); - buttonPressTimeStamp = millis(); - - } + // Update the Bounce instance : + debouncer.update(); + + // Call code if Bounce fell (transition from HIGH to LOW) : + if ( debouncer.fell() ) {; + + Serial.println( millis()-buttonPressTimeStamp ); + buttonPressTimeStamp = millis(); + } - if ( buttonState == 1 ) { - if ( millis() - buttonPressTimeStamp >= 2000 ) { - buttonState = 2; - Serial.println("Button held for two seconds (state 2)"); - } - } - - + } diff --git a/Bounce2/examples/retrigger/retrigger.ino b/Bounce2/examples/retrigger/retrigger.ino index f3153db..b1143ee 100644 --- a/Bounce2/examples/retrigger/retrigger.ino +++ b/Bounce2/examples/retrigger/retrigger.ino @@ -1,17 +1,18 @@ -#include /* DESCRIPTION ==================== -Simple example of the bounce library that shows how to retrigger an event when a button is held down. +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. Open the Serial Monitor (57600 baud) for debug messages. -CIRCUIT -==================== -https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/Bounce/examples/circuit-bounce-change-duration-retrigger.png */ +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce-Arduino-Wiring +#include + + #define BUTTON_PIN 2 #define LED_PIN 13 diff --git a/Bounce2/keywords.txt b/Bounce2/keywords.txt new file mode 100644 index 0000000..22285fe --- /dev/null +++ b/Bounce2/keywords.txt @@ -0,0 +1,29 @@ +####################################### +# Syntax Coloring Map For Bounce2 +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Bounce KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +update KEYWORD2 +interval KEYWORD2 +read KEYWORD2 +attach KEYWORD2 +rose KEYWORD2 +fell KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### +