Arduino library · Input

Bounce2: an example that runs

Yes. You can use the Bounce2 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Pushbutton). The example below, "Counting presses on a bouncing button", runs on the Arduino Uno R3.

Built in on Mokxi: #include <Bounce2.h> and it builds, with nothing to install. Free, and no account needed.

What Bounce2 does

Debounced buttons: Bounce2::Button, update(), pressed(), fell() and rose(). That is what Bounce2, by Thomas O Fredericks, covers. The sketch below uses it on the Arduino Uno: “Counting presses on a bouncing button”.

The Bounce2 debouncing library's API, on any board.

A switch does not close once: its contacts bounce for a millisecond or two, and a sketch that counts edges counts every bounce. Bounce2 reads the pin on every update() and only lets a change through once the pin has read the same way for interval milliseconds (10 unless interval() says otherwise).

The calls follow release 2.71.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameBounce2
AuthorThomas O Fredericks
Include#include <Bounce2.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesPushbutton

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <Bounce2.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Bounce2" by Thomas O Fredericks from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Bounce2 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.71.0, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

Button button

Makes button, the Button object the calls below are made on.

button.attach(2, INPUT_PULLUP)

Attaches it.

button.setPressedState(LOW)

Sets the pressed state to the value you pass in. Arguments: state.

button.update()

Updates it and returns true or false.

Example: Counting presses on a bouncing button

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Bounce2.h>

Bounce2::Button button = Bounce2::Button();
int presses = 0;

void setup() {
  Serial.begin(9600);
  button.attach(2, INPUT_PULLUP);   // to ground, pull-up on
  button.interval(5);                    // ms the contacts must settle for
  button.setPressedState(LOW);
  Serial.println("ready");
}

void loop() {
  button.update();
  if (button.pressed()) {
    presses++;
    Serial.print("pressed ");
    Serial.println(presses);
  }
  if (button.released()) {
    Serial.print("held for ");
    Serial.print(button.previousDuration());
    Serial.println(" ms");
  }
}

Wiring

  • Arduino Uno R3 2 to Pushbutton 1a
  • Arduino Uno R3 GND to Pushbutton 2a

Open it in the editor, wired and ready to run

Try Bounce2 without installing anything

Open the example, change the sketch, and keep your version in a free account.