From 7e1cb95a9d289fd178b35f43ac9dd7a3b6ad1a5a Mon Sep 17 00:00:00 2001 From: NoobTracker <63962365+NoobTracker@users.noreply.github.com> Date: Fri, 8 May 2020 16:45:05 +0200 Subject: [PATCH] Example for Bounce2Button --- examples/Bounce2Button/Bounce2Button.ino | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/Bounce2Button/Bounce2Button.ino diff --git a/examples/Bounce2Button/Bounce2Button.ino b/examples/Bounce2Button/Bounce2Button.ino new file mode 100644 index 0000000..d5fea68 --- /dev/null +++ b/examples/Bounce2Button/Bounce2Button.ino @@ -0,0 +1,36 @@ + +// 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 + +#define BUTTON_PIN 2 +#define LED_PIN 13 + +int ledState = LOW; + + +BounceButton debouncer = BounceButton(); // Instantiate a Bounce object + +void setup() { + + debouncer.attach(BUTTON_PIN,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode + debouncer.interval(25); // Use a debounce interval of 25 milliseconds + debouncer.setInvert(true); + + pinMode(LED_PIN,OUTPUT); // Setup the LED + digitalWrite(LED_PIN,ledState); + +} + +void loop() { + + debouncer.update(); // Update the Bounce instance + + if ( debouncer.pressed() ) { // Call code if button transitions from HIGH to LOW + ledState = !ledState; // Toggle LED state + digitalWrite(LED_PIN,ledState); // Apply new LED state + } +}