• Stars
    star
    585
  • Rank 76,419 (Top 2 %)
  • Language
    C++
  • License
    MIT License
  • Created almost 11 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Debouncing library for Arduino and Wiring

BOUNCE 2

Debouncing library for Arduino and Wiring by Thomas Ouellet Fredericks with many contributions from the community : https://github.com/thomasfredericks/Bounce2/graphs/contributors

Basically, the mechanical part of buttons and switches vibrate slightly when closed or opened causing multiple undesired false states (similar to noise). This library filters out these undesired state changes. More about debouncing:

See the bottom of this page for a basic usage example and the "examples" folder for more.

The library is composed of three classes:

  • Debouncer : The code that does the actual debouncing. Only advanced users should play with this class.
  • Bounce : This is the general use library. It links the Debouncer to a hardware pin on your board.
  • Button : A special version of Bounce for buttons that are pressed.

INSTALLATION & DOWNLOAD

Install through your software's Library Manager or download the latest version here and put the "Bounce2" folder in your "libraries" folder.

Please note that the original version of this library (Bounce 1) is included in the "extras" folder of the download but not supported anymore.

BASIC USE

INSTANTIATE

#include <Bounce2.h>
Bounce b = Bounce(); // Instantiate a Bounce object

SETUP

b.attach ( <PIN> , <PIN MODE> );
b.interval( <INTERVAL IN MS> );

LOOP

b.update();
if ( b.changed() ) { 
  // THE STATE OF THE INPUT CHANGED
  int deboucedValue = b.read();
  // DO SOMETHING WITH THE VALUE
}

BOUNCE EXAMPLE

// This example toggles the debug LED (pin 13) on or off when a button on pin 2 is pressed.

// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

#define BUTTON_PIN 2
#define LED_PIN 13

int ledState = LOW;


Bounce b = Bounce(); // Instantiate a Bounce object

void setup() {
  
  b.attach(BUTTON_PIN,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  b.interval(25); // Use a debounce interval of 25 milliseconds
  
  
  pinMode(LED_PIN,OUTPUT); // Setup the LED
  digitalWrite(LED_PIN,ledState); // Turn off the LED
 
}

void loop() {

   b.update(); // Update the Bounce instance
   
   if ( b.fell() ) {  // Call code if button transitions from HIGH to LOW
     ledState = !ledState; // Toggle LED state
     digitalWrite(LED_PIN,ledState); // Apply new LED state
   }
}

BUTTON EXAMPLE

/* 
 DESCRIPTION
 ====================
 This is an example of the Bounce2::Button class. 
 When the user presses a physical button, it toggles a LED on or off.
 The Button class matches an electrical state to a physical action. 
 Use .setPressedState(LOW or HIGH) to set the detection state for when the button is pressed.

 INSTRUCTIONS
 ====================
 Set BUTTON_PIN to the pin attached to the button.
 Set LED_PIN to the pin attached to a LED.
 
 */
 
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

// INSTANTIATE A Button OBJECT
Bounce2::Button button = Bounce2::Button();

// WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define BUTTON_PIN 39 

// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
//#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
#define LED_PIN 13

// SET A VARIABLE TO STORE THE LED STATE
bool ledState = LOW;

void setup() {

  // BUTTON SETUP 
  
  // SELECT ONE OF THE FOLLOWING :
  // 1) IF YOUR BUTTON HAS AN INTERNAL PULL-UP
  // button.attach( BUTTON_PIN ,  INPUT_PULLUP ); // USE INTERNAL PULL-UP
  // 2) IF YOUR BUTTON USES AN EXTERNAL PULL-UP
  button.attach( BUTTON_PIN, INPUT ); // USE EXTERNAL PULL-UP

  // DEBOUNCE INTERVAL IN MILLISECONDS
  button.interval(5); 

  // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
  button.setPressedState(LOW); 
  
  // LED SETUP
  pinMode(LED_PIN,OUTPUT);
  digitalWrite(LED_PIN,ledState);

}

void loop() {
  // UPDATE THE BUTTON
  // YOU MUST CALL THIS EVERY LOOP
  button.update();

  if ( button.pressed() ) {
    
    // TOGGLE THE LED STATE : 
    ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
    digitalWrite(LED_PIN,ledState);

  }
}

DOCUMENTATION

The complete class documentation can be found in the "docs" folder or online here.

GITHUB PAGE (SOURCE CODE)

https://github.com/thomasfredericks/Bounce2

HAVE A QUESTION?

Please post your questions here.

ALTERNATE DEBOUNCE ALGORITHMS FOR ADVANCED USERS AND SPECIFIC CASES

STABLE INTERVAL

By default, the Bounce library uses a stable interval to process the debouncing. This is simpler to understand and can cancel unwanted noise.

LOCK-OUT INTERVAL

By defining "#define BOUNCE_LOCK_OUT" in "Bounce.h" you can activate an alternative debouncing method. This method is a lot more responsive, but does not cancel noise.

#define BOUNCE_LOCK_OUT

WITH PROMPT DETECTION

By defining "#define BOUNCE_WITH_PROMPT_DETECTION" in "Bounce.h" you can activate an alternative debouncing method. Button state changes are available immediately so long as the previous state has been stable for the timeout period. Otherwise the state will be updated as soon as the timeout period allows.

  • Able to report acurate switch time normally with no delay.
  • Use when accurate switch transition timing is important.
#define BOUNCE_WITH_PROMPT_DETECTION

More Repositories

1

UnityOSC

Open Sound Control (OSC) for Unity
C#
186
star
2

Metro-Arduino-Wiring

Metro library for Arduino or Wiring
C++
125
star
3

DMX_USB_PRO_MAX

Enttec DMX USB Pro patcher for Cycling '74 Max
Max
19
star
4

MicroOsc

MicroOsc is a minimal Open Sound Control (OSC) library for Arduino
Max
16
star
5

Op-Synth

Open source modular micro analog synthesizer designed to facilitate the understanding of electronics, audio synthesis, audio theory, and filtering.
16
star
6

wemos_matrix_gfx

Library for the WEMOS Mini Matrix LED Shield compatible with Adafruit GFX Library.
Max
11
star
7

Stepper_28BYJ_48

Stepper 28BYJ_48 UL2003 Library for Arduino/Wiring
C++
10
star
8

Tvestroy

Pure Data
7
star
9

p5js-examples

Examples for p5.js
JavaScript
5
star
10

Messenger

Simple ASCII messages for Wiring and Arduino
Max
4
star
11

Slip

Slip protocol for Arduino/Wiring
Max
4
star
12

Mozard

Mozard is a miniature digital synthesizer based on Mozzi and an Arduino Nano ATMEGA328p board
HTML
4
star
13

raspberrypi-power

Scripts and circuits to manage power to the Raspberry Pi 3
4
star
14

EuroESP32

Eurorack version of the ESP32
2
star
15

note2osc-axmd

Max for Live device that converts notes to OSC messages
2
star
16

Anal

An application that analyzes an audio input and that outputs OSC packets. Made with Cyling '74's Max.
Max
2
star
17

rel-vcv-rack

Ressource d'enseignement libre sur VCV Rack
JavaScript
2
star
18

NetworkManager

A meta network connection manager library
C++
2
star
19

MicroLog

Arduino basic logging library
C
2
star
20

Arduino-Nodejs

Scripts for running Arduino and Nodejs
JavaScript
1
star
21

STOP_PING

STOP_PING Captive Portal Camera for ESP8266 and ArduCAM
Objective-C
1
star
22

TinyMidi

A tiny (low on features, but fast and light) MIDI library for Arduino
C++
1
star
23

Max-Serveur_Node_JS-WebSockets

Max
1
star
24

Seize

Seize Library for Arduino and Wiring
Max
1
star
25

SpaceRoarCreator

C#
1
star
26

ByteStream

A stream library for Arduino and Wiring
Max
1
star
27

M5_PbHub

Library for the M5Stack PbHUB v1.1
C++
1
star