Loading...

Bounce library for Arduino
by Thomas Ouellet Fredericks
with contributions from:
Eric Lowry
Jim Schimpf
Tom Harkaway
contact: mrtoftrash@gmail.com

Bounce is the new version of the Debounce library

Version

1.6: Fixed for Arduino 1.0

1.5: Added risingEdge and fallingEdge

1.3: Fixed a bug that was confusing a local variable and an argument

1.2: Added a duration method

1.1: Removed a badly formated comment that would halt compiling

1.0: Initial Release

Description

Bounce is a library for Arduino (arduino.cc). It debounces digital inputs and more.

Download, install and import

Download here: Attach:Bounce.zip

Put the Bounce folder in "your Arduino Sketchbook Location/libraries/". To identify this location open "menubar->File->Preferences".

In the Arduino IDE, select "menubar->Sketch->Import Library->Bounce" to import the library to your sketch. An "#include Bounce.h" line will appear at the top of your Sketch.

You can also find examples under "menubar->File->Sketchbook->libraries->Bounce"

Methods

Bounce(byte pin,unsigned long debounce_interval)

Instantiates a Debounce object for the specified pin with a debounce time.

Because Bounce does not use interrupts, you have to "update" the object before reading its value.

int update()

Updates Bounce. Returns true if the pin state changed (HIGH to LOW or LOW to HIGH). False if not.

void interval(unsigned long interval)

Changes the debounce time in milliseconds.

int read()

Reads the updated pin state.

void write(int state)

Sets the stored pin state

void rebounce(unsigned long interval)

Forces the pin to signal a state change in X milliseconds even if the state does not actually change. Example: A button that repeats every X milliseconds as long as it is held down

unsigned long duration(void)

Returns the number of milliseconds the pin has been in the current state.

bool risingEdge()

// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.

bool fallingEdge();

The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.

Example

// This code turns a led on/off through a debounced switch

#include <Bounce.h>

// This code turns a led on/off through a debounced button
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button

#define BUTTON 2
#define LED 13

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 ); 

void setup() {
  pinMode(BUTTON,INPUT);
  pinMode(LED,OUTPUT);
}

void loop() {
 // Update the debouncer
  bouncer.update ( );

 // Get the update value and turn on or off the LED
digitalWrite(LED, bouncer.read());

}