Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
3.MultiPurposeButton.ino

3.MultiPurposeButton

This examples shows how to use the MultiPurposeButton class to detect different kinds of push button events.

Boards: 🛈
AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32

Connections

The internal pull-up resistor will be enabled.

Behavior

Written by PieterP, 2022-05-07
https://github.com/tttapa/Arduino-Helpers

#include <Arduino_Helpers.h> // https://github.com/tttapa/Arduino-Helpers
// Create a Button object that reads a push button connected to pin 2:
void setup() {
Serial.begin(115200);
btn.setLongPressDelay(1000);
btn.begin();
}
void loop() {
auto longPressTime = btn.getLongPressedTime();
switch (btn.update()) {
case btn.None: break;
case btn.PressStart: Serial << "Pressed" << endl; break;
Serial << "Released after short press" << endl;
break;
case btn.LongPress: Serial << "Long press" << endl; break;
case btn.LongPressRelease:
Serial << "Released after long press (" << longPressTime << " ms)"
<< endl;
break;
case btn.MultiPress:
Serial << "Consecutive presses: " << btn.getCurrentMultiPressCount() + 1
<< endl;
break;
case btn.MultiPressDone:
Serial << "Counted " << btn.getMultiPressCount() + 1
<< " consecutive presses" << endl;
break;
}
}
Dummy header file for Arduino builder.
Class for detecting short/long button presses and double clicks.
Event update()
Read the button state and return the Event (if any).
unsigned long getLongPressedTime() const
Get the number of milliseconds the button has been pressed for.
@ PressStart
The button was just pressed.
@ LongPressRelease
The button was released after a long press.
@ ShortPressRelease
The button was released after a short press.
@ MultiPressDone
The button has been released for long enough to rule out another MultiPress.
@ LongPress
The button has been pressed for some time.
@ MultiPress
The button was pressed in quick succession of the previous release.
void setLongPressDelay(unsigned long longPressDelay)
Set the number of milliseconds after which a press is considered a long press.
uint8_t getMultiPressCount() const
Get the number of times the button was pressed in quick succession (after MultiPressDone),...
uint8_t getCurrentMultiPressCount() const
Get the number of times the button was pressed in quick succession, while the button is being pressed...
void setMultiPressDelay(unsigned long multiPressDelay)
Set the number of milliseconds between multipresses.
Print & endl(Print &printer)