https://github.com/tfeldmann/arduino-button
https://github.com/tfeldmann/arduino-button
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/tfeldmann/arduino-button
- Owner: tfeldmann
- License: mit
- Created: 2021-11-11T15:19:44.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-02-18T22:33:15.000Z (over 2 years ago)
- Last Synced: 2025-02-01T12:09:19.100Z (over 1 year ago)
- Language: C++
- Size: 16.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Arduino-Button
[](https://registry.platformio.org/libraries/tfeldmann/Button)
## API
```cpp
struct ButtonEvents {
bool pressed;
bool released;
bool changed;
};
class VirtualButton {
VirtualButton(bool invert = false);
struct ButtonEvents update(bool reading);
bool is_pressed();
bool just_pressed();
bool just_released();
bool just_changed();
}
class PinButton : public VirtualButton {
PinButton(int pin, int mode = INPUT_PULLUP, bool invert = false);
};
```
## Button Events Example
```cpp
#include
PinButton btn(12);
void setup() {
Serial.begin(9600);
}
void loop() {
struct ButtonEvents evnt = btn.update();
if (evnt.pressed) Serial.println("Btn pressed");
if (evnt.released) Serial.println("Btn released");
if (evnt.changed) Serial.println("Btn changed");
}
```
## PinButton Example
Toggles a LED on pin 13 when the button is released.
```cpp
#include
PinButton btn(12, INPUT_PULLUP);
bool led_state = LOW;
void setup()
{
pinMode(13, OUTPUT); // LED
digitalWrite(13, led_state);
}
void loop()
{
btn.update();
if (btn.just_released()) {
led_state = !led_state;
digitalWrite(13, led_state);
}
}
```