Arduino Helpers master
Utility library for Arduino
2.Button.ino

2.Button

This examples shows how to use the debounced Button class to toggle an LED.

Boards:
AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32

Connections

The internal pull-up resistor will be enabled.

Behavior

Written by PieterP, 2019-11-22
https://github.com/tttapa/Arduino-Helpers

// Include the library
// Create a Button object that reads a push button connected to pin 2:
Button pushbutton {2};
// The pin with the LED connected:
const pin_t ledPin = LED_BUILTIN;
void setup() {
pinMode(ledPin, OUTPUT);
pushbutton.begin();
// You can invert the input, for use with normally closed (NC) switches:
// pushbutton.invert();
}
void loop() {
static bool ledState = LOW;
// Read the digital input, debounce the signal, and check the state of
// the button:
if (pushbutton.update() == Button::Falling) {
ledState = !ledState; // Invert the state of the LED
// Update the LED with the new state
digitalWrite(ledPin, ledState ? HIGH : LOW);
}
}
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
constexpr PinMode_t OUTPUT
Dummy header file for Arduino builder.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:15
@ Falling
Input went from high to low (1,0)
Definition: Button.hpp:48
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.