-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.cpp
executable file
·46 lines (39 loc) · 1.29 KB
/
debounce.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
37
38
39
40
41
42
43
44
45
/*
* Arduino-Cocktail
*
* Pushbutton debouncing helper class
* Copyright 2017 by Thomas Buck <[email protected]>
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return. Thomas Buck
* ----------------------------------------------------------------------------
*/
#include <Arduino.h>
#include "debounce.h"
Debouncer::Debouncer(int p) : pin(p), currentState(0), lastState(0), lastTime(0) { }
int Debouncer::poll() {
int ret = 0;
int state = digitalRead(pin);
// if the pin is still changing...
if (state != lastState) {
// ...wait some more
lastTime = millis();
}
if ((millis() - lastTime) > DEBOUNCE_DELAY) {
// if enough time has passed since the last state-change
if (state != currentState) {
// if the state has changed...
currentState = state;
if (currentState == LOW) {
ret = -1;
} else {
ret = 1;
}
}
}
lastState = state;
return ret;
}