AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
HoldableCallback.ino
Go to the documentation of this file.
1/**
2 * @file HoldableCallback.ino Example delay timer control using callbacks.
3 *
4 * The built-in LED lights up when a button connected between pin 2 and
5 * ground is held down for 2 seconds or more. It uses the internal pull-up
6 * resistor within an Arduino for the simplest button connection.
7 *
8 * When the button is released, the LED stays on, waiting until idle after 5
9 * seconds and the LED goes off. Simulates a delay timer.
10 *
11 * @copyright Copyright (c) 2022 John Scott
12 */
13#include <AbleButtons.h>
14
15// Identify which buttons you are using...
16using Button = AblePullupCallbackButton; ///< Using pull-up button with callbacks.
17using ButtonList = AblePullupCallbackButtonList; ///< ButtonList for the same.
18
19// Forward declaration of callback function.
21
22#define BUTTON_PIN 2 ///< Connect button between this pin and ground.
23Button btn(BUTTON_PIN, holdableCallback); ///< The button to check.
24
25/**
26 * Setup the Debouncable example. Called once to initialise everything.
27 */
28void setup() {
29 pinMode(LED_BUILTIN, OUTPUT);
30
31 Button::setHeldTime(2000);
32 Button::setIdleTime(5000);
33
34 btn.begin();
35}
36
37/**
38 * Control the Debouncable example. Called repeatedly in a loop.
39 */
40void loop() {
41 btn.handle();
42}
43
44/**
45 * Callback function for button events.
46 *
47 * @param event The event that has occured.
48 * @param id The identifier of the button generating the callback (ignored in this example).
49 */
51 (void)id; // id is unused.
52
53 if(event == Button::HELD_EVENT) {
54 digitalWrite(LED_BUILTIN, HIGH);
55 } else if(event == Button::IDLE_EVENT) {
56 digitalWrite(LED_BUILTIN, LOW);
57 }
58}
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
Button btn(BUTTON_PIN, holdableCallback)
The button to check.
void setup()
Setup the Debouncable example.
void holdableCallback(Button::CALLBACK_EVENT, uint8_t)
Callback function for button events.
#define BUTTON_PIN
Connect button between this pin and ground.
void loop()
Control the Debouncable 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.
@ IDLE_EVENT
The button has been idle (untouched) for a while.
@ HELD_EVENT
The button has been held down for a while.