-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
36 lines (32 loc) · 894 Bytes
/
Button.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "Button.h"
#include <Arduino.h>
Button::Button(int pin) {
pinMode(pin, INPUT);
_pin = pin;
_state = false;
_lastState = false;
debounceMS = 200;
}
// Returns if the button is currently pressed (no debounce)
bool Button::isOn() {
return !digitalRead(_pin);
}
// returns if the button has been pressed and resets the state
bool Button::wasPressed() { // Only evaluates true once per press
bool value = _state;
_state = true;
return !value;
}
void Button::updateButton() {
int reading = digitalRead(_pin);
if (reading != _lastState) { // if the reading has changed do to pressing or noise
_startDebounce = millis();
_debouncing = true;
}
// Evaluate the state only when enough time has passed without changes
if (_debouncing && millis() - _startDebounce > debounceMS) {
_state = reading;
_debouncing = false;
}
_lastState = reading;
}