Home

Awesome

<div align = center>

EasyButton

Lightweight Arduino library for handling push buttons just connected between ground and INT0 and / or INT1 or any other PCINT pin.

Badge License: GPLv3     Badge Version     Badge Commits since latest     Badge Build Status     Badge Hit Counter <br/> <br/> Stand With Ukraine

Available as Arduino library "EasyButtonAtInt01".

Button Install     Button Changelog

</div>

If you find this library useful, please give it a star.

🌎 Google Translate

<br/>

Features

Table of available pins for the 2 buttons

CPUButton 0Button 1 using INT1Button 1 using PCINT, if INT1_PIN is defined != 3
ATmega328*D2D3Pin 0 to 2, 4 to 13, A0 to A5
ATtiny5xPB2PB0 - PB5
ATtiny167PB6PA3PA0 to PA2, PA4 to PA7

To use the PCINT buttons instead of the default one, just define INT1_PIN before including EasyButtonAtInt01.hpp.<br/> E.g. #define INT1_PIN 7. See TwoButtons.cpp.

Usage

To use a single button, it needs only:

#define USE_BUTTON_0 // Enable code for button at INT0 (pin2)
#include "EasyButtonAtInt01.hpp"
EasyButton Button0AtPin2;

void setup() {}
void loop() {
...
    digitalWrite(LED_BUILTIN, Button0AtPin2.ButtonToggleState); // The value at the first call after first press is true
...
}

To use 2 buttons, it needs only:

#define USE_BUTTON_0 // Enable code for button at INT0 (pin2)
#define USE_BUTTON_1 // Enable code for button at INT1 (pin3) or PCINT[0:7]
#include "EasyButtonAtInt01.hpp"
EasyButton Button0AtPin2();  // no parameter -> Button is connected to INT0 (pin2)
EasyButton Button1AtPin3(BUTTON_AT_INT1_OR_PCINT); // Button is connected to INT1 (pin3)

void setup() {
    Serial.print(F("Button0 pin="));
    Serial.print(INT0_PIN);
    Serial.print(F(" Button1 pin="));
    Serial.println(INT1_PIN);
}
void loop() {
...
    digitalWrite(LED_BUILTIN, Button0AtPin2.ButtonToggleState);
    delay(100);
    digitalWrite(LED_BUILTIN, Button1AtPin3.ButtonToggleState);
    delay(200);
...
}

Usage of callback functions

The button press callback function is is called on every button press with ButtonToggleState as parameter.<br/> The value at the first call (after first press) is true.<br/> The button release callback function is called on every button release with the additional parameter ButtonPressDurationMillis.<br/> Both callback functions run in an interrupt service context, which means they should be as short/fast as possible. In this library, interrupts are enabled before the callback function is called. This allows the timer interrupt for millis() to work and therefore delay() and millis() can be used in a callback function.

#define USE_BUTTON_0 // Enable code for button at INT0 (pin2)
#include "EasyButtonAtInt01.hpp"

// Initial value is false, so first call is with true
void handleButtonPress(bool aButtonToggleState) {
    digitalWrite(LED_BUILTIN, aButtonToggleState);
}
EasyButton Button0AtPin2(&handleButtonPress); // Button is connected to INT0 (pin2)

void setup() {}
void loop() {}

Long press detection

The easiest way is to use the button release handler. Keep in mind, that you will get a press callback at the start of the long press.

#define USE_BUTTON_0 // Enable code for button at INT0 (pin2)
#include "EasyButtonAtInt01.hpp"

void handleButtonRelease(bool aButtonToggleState, uint16_t aButtonPressDurationMillis);
EasyButton Button0AtPin2(NULL, &handleButtonRelease); // Button is connected to INT0 (pin2)

handleButtonRelease(bool aButtonToggleState, uint16_t aButtonPressDurationMillis) {
    if (aButtonPressDurationMillis >= EASY_BUTTON_LONG_PRESS_DEFAULT_MILLIS) { // 400 ms
        Serial.print(F("Long press "));
        Serial.print(aButtonPressDurationMillis);
        Serial.println(F(" ms detected"));
    }
}

void setup() {}
void loop() {}

Or check in loop, this enables to react with feedback as soon as long press duration is reached.

#define USE_BUTTON_0 // Enable code for button at INT0 (pin2)
#include "EasyButtonAtInt01.hpp"

bool sLongPressMessageSent = false;
void handleButtonPress(bool aButtonToggleState, uint16_t aButtonPressDurationMillis) {sLongPressMessageSent = false};
EasyButton Button0AtPin2(&handleButtonPress); // Button is connected to INT0 (pin2)

void setup() {}
void loop() {
if (!sLongPressMessageSent && Button0AtPin2.checkForLongPress(1000) == EASY_BUTTON_LONG_PRESS_DETECTED) {
        Serial.println(F("Long press of 1000 ms just detected"));
        sLongPressMessageSent = true; // Print message only once per long press
}

if (sLCDDisplayPage == POWER_METER_PAGE_INFO && PageButtonAtPin3.checkForLongPress(1000) == EASY_BUTTON_LONG_PRESS_DETECTED) {

Double press detection

Call checkForDoublePress() only from button press callback function. It will not work as expected, if called outside this callback function.

#define USE_BUTTON_0 // Enable code for button at INT0 (pin2)
#include "EasyButtonAtInt01.hpp"

void handleButtonPress(bool aButtonToggleState);
EasyButton Button0AtPin2(&printButtonToggleState);

// Initial value is false, so first call is with true
void handleButtonPress(bool aButtonToggleState) {
     // This function works reliable only if called early in callback function
    if (Button0AtPin2.checkForDoublePress()) {
        Serial.println(F("Button 0 double press (< 400 ms) detected"));
    }
    digitalWrite(LED_BUILTIN, aButtonToggleState);
}

void setup() {}
void loop() {}
<br/>

Handling the multiple definition error

If you get the error multiple definition of __vector_1 (or __vector_2) because another library uses the attachInterrupt() function, then insert the line #define USE_ATTACH_INTERRUPT in your program before the line #include "EasyButtonAtInt01.hpp".

<br/>

Compile options / macros for this library

To customize the library to different requirements, there are some compile options / macros available.<br/> These macros must be defined in your program before the line #include "EasyButtonAtInt01.hpp" to take effect.<br/> Modify them by enabling / disabling them, or change the values if applicable.

NameDefault valueDescription
USE_BUTTON_0disabledEnables code for button at INT0 (pin2 on 328P, PB6 on ATtiny167, PB2 on ATtinyX5). The macro INT0_PIN is set after the include.
USE_BUTTON_1disabledEnables code for button at INT1 (pin3 on 328P, PA3 on ATtiny167, PCINT0 / PCx for ATtinyX5). The macro INT1_PIN is set after the include.
INT1_PIN%It overrides the usage of pin at the processors INT1 pin. Thus, it is the pin number of the pin for button 1 to use with Pin Change Interrupts.
NO_INITIALIZE_IN_CONSTRUCTORdisabledDisables the auto initializing in all constructors without the "aIsButtonAtINT0" parameter. Must be used if buttons must be initialized manually (e.g. later in setup).
BUTTON_IS_ACTIVE_HIGHdisabledEnable this if your buttons are active high.
USE_ATTACH_INTERRUPTdisabledThis forces use of the arduino function attachInterrupt(). It is required if you get the error "multiple definition of `__vector_1'" (or `__vector_2'), because another library uses the attachInterrupt() function.
NO_BUTTON_RELEASE_CALLBACKdisabledDisables the code for release callback. This saves 2 bytes RAM and 64 bytes program memory.
BUTTON_DEBOUNCING_MILLIS50With this you can adapt to the characteristic of your button.
ANALYZE_MAX_BOUNCING_PERIODdisabledAnalyze the buttons actual debounce value.
BUTTON_LED_FEEDBACKdisabledThis activates LED_BUILTIN as long as button is pressed.
BUTTON_LED_FEEDBACK_PINdisabledThe pin to use for button LED feedback.
INTENTIONALLY_USE_PCI0_FOR_BUTTON1disabledActivate it to suppress the warning: "Using PCINT0 interrupt for button 1".
USE_INT2_FOR_BUTTON_0disabledHack, especially for ATmega644 etc, where INT0 and INT1 are occupied by 2. USART.

The exact pin numbers of the buttons used internally are available by the macros INT0_PIN and INT1_PIN, which are set after the include.

Class methods

EasyButton(); // Constructor for button at INT0
EasyButton(void (*aButtonPressCallback)(bool aButtonToggleState)); // Constructor for button at INT0

EasyButton(bool aIsButtonAtINT0); // Constructor
EasyButton(bool aIsButtonAtINT0, void (*aButtonPressCallback)(bool aButtonToggleState));
EasyButton(bool aIsButtonAtINT0, void (*aButtonPressCallback)(bool aButtonToggleState), void (*aButtonReleaseCallback)(bool aButtonToggleState, uint16_t aButtonPressDurationMillis));
void init(); // used by constructors

#define EASY_BUTTON_LONG_PRESS_DEFAULT_MILLIS 400
#define EASY_BUTTON_DOUBLE_PRESS_DEFAULT_MILLIS 400

bool readButtonState();
bool readDebouncedButtonState();
bool updateButtonState();
uint16_t updateButtonPressDuration();
uint8_t checkForLongPress(uint16_t aLongPressThresholdMillis = EASY_BUTTON_LONG_PRESS_DEFAULT_MILLIS);
bool checkForLongPressBlocking(uint16_t aLongPressThresholdMillis = EASY_BUTTON_LONG_PRESS_DEFAULT_MILLIS);
bool checkForDoublePress(uint16_t aDoublePressDelayMillis = EASY_BUTTON_DOUBLE_PRESS_DEFAULT_MILLIS);
bool checkForForButtonNotPressedTime(uint16_t aTimeoutMillis);

Revision History

Version 3.4.1 - work in progress

Version 3.4.0

Version 3.3.1

Version 3.3.0

Version 3.2.0

Version 3.1.0

Version 3.0.0

Version 2.1.0

Version 2.0.0

Version 1.0.0

CI

The library examples are tested with GitHub Actions for the following boards: