Added fell and rose methods

This commit is contained in:
thomasfredericks
2014-09-30 11:16:11 -04:00
parent a50a0f98aa
commit 8516aeac01
7 changed files with 120 additions and 107 deletions
+10
View File
@@ -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));
}
+4
View File
@@ -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:
+29 -33
View File
@@ -1,52 +1,48 @@
#include <Bounce2.h>
/*
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 <Bounce2.h>
#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 );
}
}
}
+20 -22
View File
@@ -1,9 +1,10 @@
#include <Bounce2.h>
// 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 <Bounce2.h>
#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);
}
}
+22 -47
View File
@@ -1,78 +1,53 @@
#include <Bounce2.h>
/*
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 <Bounce2.h>
#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)");
}
}
}
+6 -5
View File
@@ -1,17 +1,18 @@
#include <Bounce2.h>
/*
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 <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
+29
View File
@@ -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)
#######################################