Debounce is outdate please use Bounce instead

Debounce library for Arduino
by Thomas Ouellet Fredericks
contact: mrtoftrash@gmail.com

Current version

1.2 2008/09/25: Added a write function as suggested by Jim Schimpf

History

1.1 2008/09/25: Modified header file so Debounce compiles with Arduino 12
1.0 2008/09/21: Initial Release

Description

Debounce is a library for Arduino (arduino.cc).

It debounces digital inputs.

Download, install and import

Download here: Attach:Debounce.zip

Put the Debounce folder in "hardware\libraries\".

In the Arduino IDE, create a new sketch (or open one) and

select from the menubar "Sketch->Import Library->Debounce".

Once the library is imported, an "#inlcude Debounce.h" line will appear

at the top of your Sketch.

CREATION

Debounce(unsigned long debounceTime, byte pin)

Instanciates a Debounce object with a debounce time and a digital pin number.

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

Methods

int update()

Updates Debounce. 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 update pin state correct for debouncing.

void write(int state)

Writes the state to the pin (in Debounce's memory and with digitalWrite).

Example

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


#include <Debounce.h>
#define SWITCH 5
#define LED 13

// Instantiate a Debounce object with a 20 millisecond debounce time
Debounce debouncer = Debounce( 20 , SWITCH ); 

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

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

 // Get the update value
   digitalWrite(LED, debouncer.read() );

}

FAQ

I found your debounce library on the Arduino Website. It's just what I was looking for. But...Can you use it for more than one digitalInput at a time? When you call the debouncer.read() function it uses the pin number that was set in the Debounce() function?

Debounce is a class. Therefore to debounce multiple digital pins, you must create an instance for each of them. In the example above, a Debounce instance (called debouncer) for pin 5 (defined as SWITCH) is created with the following line:

Debounce debouncer = Debounce( 20 , SWITCH );

To debounce an additional pin (pin 4 for example), you could create the following instance called debounce2:

Debounce debounce2 = Debounce( 20 , 4 );

During the loop() you would have to update debouncer (for pin 5) and debounce2 (for pin 4):

// Update the debouncers
  debouncer.update();
  debounce2.update();

Share