view history edit print login register

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

Bounce is the new version of the Debounce library

Current version

(2010/01/01) 1.3: Fixed a bug that was confusing a local variable and an argument

(2009/11/01) 1.2: Added a duration method

(2009/09/07) 1.1: Removed a badly formated comment that would halt compiling

(2009/08/23) 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 Sketch Folder\libraries\".

In the Arduino IDE, select "menubar->Sketch->Import Library->Bounce" to import the library to your sketch. An "#inlcude 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.

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
 int value = bouncer.read();

 // Turn on or off the LED
 if ( value == HIGH) {
   digitalWrite(LED, HIGH );
 } else {
    digitalWrite(LED, LOW );
 }

}