/
Timer.h
50 lines (44 loc) · 1.09 KB
/
Timer.h
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
46
47
48
49
50
#ifndef Timer_h
#define Timer_h
class Timer {
public:
Timer (unsigned long timeout) {
timerLength = timeout;
}; // constructor
void setPeriod (unsigned long timeout) {
timerLength = timeout;
}
void start() {
endTime = millis() + timerLength;
timeLeft = timerLength;
printTimeout = millis() + 1000;
}
boolean expired () {
boolean elapse = false;
unsigned long timeRemaining = (endTime - millis ());
if (endTime != -1) {
if (millis () > endTime) {
Serial.println ( "Timer has expired" );
elapse = true;
endTime = -1;
} else {
if (millis() > printTimeout ) {
Serial.print (((endTime - millis() ) / 1000) + 1 );
Serial.println ( " seconds remaining." );
printTimeout = printTimeout + 1000;
}
timeLeft = timeRemaining;
}
}
return elapse;
}
void reset () {
endTime = -1;
}
private:
unsigned long timerLength = -1;
unsigned long endTime = -1;
unsigned long timeLeft = 0;
unsigned long printTimeout = 0;
};
#endif