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 name | Bounce2 |
|---|---|
| Author | Thomas O Fredericks |
| Include | #include <Bounce2.h> |
| Boards | every board Mokxi compiles for |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Pushbutton |
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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- ATtiny85
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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
Try Bounce2 without installing anything
Open the example, change the sketch, and keep your version in a free account.