-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebouncePin.cpp
More file actions
30 lines (26 loc) · 1.27 KB
/
DebouncePin.cpp
File metadata and controls
30 lines (26 loc) · 1.27 KB
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
#include "DebouncePin.h"
DebouncePin::DebouncePin( byte Pin, byte Mode ) {
DebounceTime = 10;
SwitchPin = Pin;
pinMode( Pin, Mode );
if( Mode == INPUT_PULLUP ) {
OldSwitchState = HIGH;
}
}
byte DebouncePin::State() {
byte SwitchState = digitalRead( SwitchPin ); // see if switch is open or closed
if( SwitchState != OldSwitchState ) { // has it changed since last time?
if( millis()-SwitchPressTime >= DebounceTime ) { // debounce
PreviousPeriod = millis() - SwitchPressTime;
SwitchPressTime = millis(); // when we closed the switch
OldSwitchState = SwitchState; // remember for next time
} // end if debounce time up
} // end of state change
return OldSwitchState;
}
unsigned long DebouncePin::Duration() { // returns the duration of the period between the last two edges in milliseconds.
return( PreviousPeriod );
}
unsigned long DebouncePin::Timer() { // returns the number of milliseconds since the last edge
return( millis() - SwitchPressTime );
}