-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpad.cpp
More file actions
31 lines (25 loc) · 831 Bytes
/
Copy pathpad.cpp
File metadata and controls
31 lines (25 loc) · 831 Bytes
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
#include "pad.h"
Pad::Pad(int pin, int debounceInterval) {
_pin = pin;
_pressed = false;
_lastPressed = 0;
_debounceInterval = debounceInterval;
pinMode(pin, INPUT_PULLUP);
}
// Check the status of the press and the timing.
// In timeSinceLastPress, abs() handles the edge case of the Arduino timer
// overflowing and resetting to zero.
bool Pad::pressChanged() {
const bool newPressed = digitalRead(_pin) == LOW;
unsigned long timeSinceLastPress = abs(millis() - _lastPressed);
// Check for status change and debouncing
bool changed = newPressed != _pressed && timeSinceLastPress >= _debounceInterval;
if (changed) {
_pressed = newPressed;
_lastPressed = millis();
}
return changed;
}
bool Pad::isPressed() {
return _pressed;
}