Skip to content

ripred/ButtonGestures

Arduino CI Arduino-lint JSON check code size: Arduino Library Manager License: MIT Stars Forks

Arduino Button Gestures Library

This library allows you to use a single push button to be able to invoke up to 6 different functions without blocking the rest of your sketch while it waits to classify the gesture.

The button gestures include:

* No press
* Single press short (single click)            ---> call func1
* Single press long  (single click and hold)   ---> call func2
* Double press short (double click)            ---> call func3
* Double press long  (double click and hold)   ---> call func4
* Triple press short (triple click)            ---> call func5
* Triple press long  (triple click and hold)   ---> call func6

Easy to use. Includes example sketches for basic polling, callback registration, and long-press mode behavior.

check_button() is the preferred polling API. It is non-blocking, so call it often from loop() and it returns NOT_PRESSED until a gesture has been fully recognized. Press detection is still debounced with KEYDBDELAY, long press detection still uses KEYLONGDELAY, and multi-press detection still uses ALLOWED_MULTIPRESS_DELAY.

Because detection is non-blocking, your sketch must keep polling. Avoid long delay() calls or other blocking work in loop(). If a complete press and release happens between two calls to check_button(), the library cannot see that gesture. check_button_gesture() is the lower-level recognizer used by check_button(); most sketches should call check_button().

Long gestures repeat while the button remains held by default to preserve the original library behavior. If you want each long gesture to report exactly once per physical hold, set single-shot mode:

if (!button.set_long_press_mode(LONG_PRESS_SINGLE_SHOT)) {
    // invalid mode; previous mode was left unchanged
}

To enable repeating long gestures explicitly:

button.set_long_press_mode(LONG_PRESS_REPEAT);
button.set_long_press_repeat_delay(250); // optional repeat interval in ms

Example use:

/*\
|*| Example use of the ButtonGestures library
|*|
|*| This example shows how to check the button for any gestures.
|*| If a gesture has been entered it will be displayed to the
|*| serial port.
|*|
\*/

#include <ButtonGestures.h>

// NOTE: change/define the following pin(s) based on your project/connections
#define   BUTTON_PIN    2

//                     Arduino pin     Active HIGH/LOW         Input Mode
ButtonGestures  button(BUTTON_PIN,           LOW,             INPUT_PULLUP);

//
// This function will report the passed button state along with a label
//
void report_button(const uint8_t state, const char* const label = NULL)  {
    switch (state) {
        case SINGLE_PRESS_SHORT: Serial.print(F("Single click and release"));   break;
        case SINGLE_PRESS_LONG:  Serial.print(F("Single click and hold"));      break;
        case DOUBLE_PRESS_SHORT: Serial.print(F("Double click and release"));   break;
        case DOUBLE_PRESS_LONG:  Serial.print(F("Double click and hold"));      break;
        case TRIPLE_PRESS_SHORT: Serial.print(F("Triple click and release"));   break;
        case TRIPLE_PRESS_LONG:  Serial.print(F("Triple click and hold"));      break;

        case NOT_PRESSED:
        default:
            return;
    }

    if (nullptr != label) {
        Serial.print(F(" on "));
        Serial.print(label);
    }

    Serial.println();
}


void setup(void) {
    Serial.begin(115200);
    uint32_t timer = millis() + 500;
    while (!Serial && millis() < timer);
    Serial.flush();
    Serial.println(F("\n\nArduino Core Library - ButtonGestures Library Test"));

    // Optional: make long gestures report once per hold.
    // button.set_long_press_mode(LONG_PRESS_SINGLE_SHOT);
}


void loop(void) {
    report_button(button.check_button(), "push button 1");
}

About

Use a single push button for up to 6 different functions! Button gesture combinations of single, double, and triple-tap along with a long or short hold on the last press make it easy! Functions can also be pre-registered for callback for the gesture that you want. Even easier!

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors

Languages