Browse by type
An adjustable, compact, event-driven button library for Arduino platforms.
This library provides classes which accept inputs from a mechanical button connected to a digital input pin on the Arduino. The library should be able to handle momentary buttons, maintained buttons, and switches, but it was designed primarily for momentary (aka push) buttons.
The library is named "AceButton" because:
EventHandler callback functionMost of the features of the library can be accessed through 2 classes, using either a callback function or an interface:
AceButton (class)ButtonConfig (class)EventHandler (typedef for callback function)IEventHandler (interface)The AceButton class contains the logic for debouncing and determining if a
particular event has occurred.
The ButtonConfig class holds various timing parameters, the event handler,
code for reading the button, and code for getting the internal clock.
The EventHandler is a user-defined callback function with a specific signature
which is registered with the ButtonConfig object. When the library detects
interesting events, the callback function is called by the library, allowing the
client code to handle the event.
The IEventHandler is an interface (pure abstract class) that provides an
alternative to the EventHandler. Instead of using a callback function, an
object of type IEventHandler can be used to handle the button events.
The supported events are:
AceButton::kEventPressedAceButton::kEventReleasedAceButton::kEventClickedAceButton::kEventDoubleClickedAceButton::kEventLongPressedAceButton::kEventRepeatPressedAceButton::kEventLongReleased (v1.8)AceButton::kEventHeartBeat (v1.10)The basic ButtonConfig class assumes that each button is connected to a single
digital input pin. In some situations, the number of buttons that we want is
greater than the number of input pins available. This library provides
2 subclasses of ButtonConfig which may be useful:
EncodedButtonConfig2^N - 1 buttons using N
pins (e.g. 7 buttons using 3 digital pins).LadderButtonConfiganalogRead() method is used to read the different
voltage levels corresponding to each button.Both EncodedButtonConfig and LadderButtonConfig support all events listed
above (e.g. kEventClicked and kEventDoubleClicked).
Version: 1.10.1 (2023-05-25)
Changelog: CHANGELOG.md
Here are the high-level features of the AceButton library:
EventHandler callback functionIEventHandler (>= v1.6)kEventPressedkEventReleasedkEventClickedkEventDoubleClickedkEventLongPressedkEventRepeatPressedkEventLongReleasedkEventHeartBeatdigitalRead() button read function can be overriddenmillis() clock function can be overriddenAceButton consumes 17 bytes (8-bit) or 20 bytes (32-bit)ButtonConfig consumes 20 bytes (8-bit) or 24 bytes (32-bit)ButtonConfig instance created automatically by the libraryAceButton::check()Compared to other Arduino button libraries, I think the unique or exceptional features of the AceButton library are:
Here is a simple program (see examples/HelloButton) which controls the builtin LED on the Arduino board using a momentary button connected to PIN 2.
#include <AceButton.h>
using namespace ace_button;
const int BUTTON_PIN = 2;
const int LED_ON = HIGH;
const int LED_OFF = LOW;
AceButton button(BUTTON_PIN);
void handleEvent(AceButton*, uint8_t, uint8_t);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
button.setEventHandler(handleEvent);
}
void loop() {
button.check();
}
void handleEvent(AceButton* /*button*/, uint8_t eventType,
uint8_t /*buttonState*/) {
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(LED_BUILTIN, LED_ON);
break;
case AceButton::kEventReleased:
digitalWrite(LED_BUILTIN, LED_OFF);
break;
}
}
(The button and buttonState parameters are commented out to avoid an unused
parameter warning from the compiler. We can't remove the parameters completely
because the method signature is defined by the EventHandler typedef.)
The latest stable release is available in the Arduino IDE Library Manager. Search for "AceButton". Click install.
The development version can be installed by cloning the
GitHub repository (https://github.com/bxparks/AceButton), checking out the
develop branch, then manually copying over the contents to the ./libraries
directory used by the Arduino IDE. (The result is a directory named
./libraries/AceButton.)
The master branch contains the tagged stable releases.
The core of the library is self-contained and has no external dependencies.
The some programs in examples/ may depend on:
* AceCommon (https://github.com/bxparks/AceCommon)
The unit tests under tests depend on:
* AUnit (https://github.com/bxparks/AUnit)
The source files are organized as follows:
* src/AceButton.h - main header file
* src/ace_button/ - all implementation files
* src/ace_button/testing/ - internal testing files
* tests/ - unit tests which require AUnit
* examples/ - example sketches
Encoded4To2ButtonConfig, Encoded8To3ButtonConfig,
EncodedButtonConfig classesLadderButtonConfig classThe following example sketches are provided:
SingleButton but with an external pull-down resistorSingleButton using an object-based IEventHandlerAceButton:check() with a start/stop/resetkFeatureLongPressThreeButtonsUsingOneButtonConfigFast
(below)ButtonConfig and EventHandler instancesgetId()kFeatureLongPress, kFeatureRepeatPress,
kFeatureSuppressAfterLongPress, and
kFeatureSuppressAfterRepeatPressAceButton and initialize them using
the init() method in a loopIEventHandlerkEventReleased insteadkFeatureSuppressClickBeforeDoubleClick flag at the cost of
increasing the response timekEventHeartBeat feature, and using it
to generate 2 custom events: kCustomEventLongPressed (similar to
kEventLongPressed) and kCustomEventLongReleased (no built-in
equiva$ claude mcp add AceButton \
-- python -m otcore.mcp_server <graph>