This is a simple button library for embedded systems. It provides an easy way to handle button presses and releases.
- Debouncing
- Short and long press detection
- Multiple button support
To use this library, include the button.h
file in your project.
Connect the switch between an appropriate MCU pin and ground. An internal pull-up resistor will be used.
#include "button.h"
setup() {
Button btn = Button(2); // Use PIN 2
btn.setShiftRegisterSize(12); // optional
btn.setMinPressDuration(10); // optional
}
loop() {
uint8_t btn_state = btn.read();
switch(btn_state) {
case STATE_SHORTPRESS:
// there was a short press
break;
case STATE_LONGPRESS:
// there was a long press
break;
}
}
Sets the size of the shift register used for debouncing.
- Parameters:
size
: The size of the shift register.
Sets the minimum duration for a press to be considered valid. Setting this to zero effectively disables checks for short presses.
- Parameters:
d
: The minimum press duration in milliseconds.
Sets the minimum press duration of a long press.
- Parameters:
d
: The long press duration in milliseconds.
Reads the current state of the button.
- Returns:
- The current state of the button, which can be
STATE_SHORTPRESS
,STATE_LONGPRESS
,STATE_NOPRESS
.
- The current state of the button, which can be
Gets the duration (ms) of the last press.
- Returns:
- The duration (ms) of the last press.
This project is licensed under the MIT License. See the LICENSE file for details.