AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
DebouncableCallback.ino
Go to the documentation of this file.
1/**
2 * @file DebouncableCallback.ino Example of toggling an LED using callback
3 * functions instead of a clicker. When the button is released (completing a
4 * click), the on-released function is called to toggle the LED.
5 *
6 * @copyright Copyright (c) 2022 John Scott
7 */
8#include <AbleButtons.h>
9
10// Identify which buttons you are using...
11using Button = AblePullupCallbackButton; ///< Using callback pull-up button.
12using ButtonList = AblePullupCallbackButtonList; ///< Using callback pull-up button list.
13
14// Forward declaration of callback function.
16
17#define BUTTON_PIN 2 ///< Connect button between this pin and ground.
18Button btn(BUTTON_PIN, clickedCallback); ///< The button to check.
19bool led = false; ///< On/off state of the LED.
20
21/**
22 * Setup the ClickedBtn example. Called once to initialise everything.
23 */
24void setup() {
25 btn.begin();
26 pinMode(LED_BUILTIN, OUTPUT);
27}
28
29/**
30 * Control the ClickedBtn example. Called repeatedly in a loop.
31 */
32void loop() {
33 btn.handle();
34}
35
36/**
37 * Callback function for button released.
38 *
39 * @param event The event that has occured.
40 * @param id The identifier of the button generating the callback (ignored in this example).
41 */
42void clickedCallback(Button::CALLBACK_EVENT event, uint8_t id) {
43 (void)id; // id is unused.
44
45 if(event == Button::RELEASED_EVENT) {
46 led = !led;
47 digitalWrite(LED_BUILTIN, led);
48 }
49}
The main include file for the Arduino Button library Extension (ABLE).
able::ButtonList< AblePullupCallbackButton > AblePullupCallbackButtonList
AblePullupCallbackButtonList allows an array of AblePullupCallbackButton objects to be managed togeth...
Definition: AbleButtons.h:254
able::CallbackButton< able::Button< able::PullupResistorCircuit, able::DebouncedPin > > AblePullupCallbackButton
Shorthand for a callback button using a pulldown resistor.
Definition: AbleButtons.h:185
void setup()
Setup the ClickedBtn example.
Button btn(BUTTON_PIN, clickedCallback)
The button to check.
void clickedCallback(Button::CALLBACK_EVENT, uint8_t)
Callback function for button released.
#define BUTTON_PIN
Connect button between this pin and ground.
bool led
On/off state of the LED.
void loop()
Control the ClickedBtn example.
Core Button class.
Definition: Button.h:22
void begin()
Initialise the button.
Definition: Button.h:60
void handle()
Handle the button.
Definition: Button.h:67
Template for a list of buttons of the same type.
Definition: ButtonList.h:20
CALLBACK_EVENT
Button event codes.
@ RELEASED_EVENT
The button has been released.