-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerTrigger.cpp
More file actions
102 lines (84 loc) · 1.7 KB
/
TimerTrigger.cpp
File metadata and controls
102 lines (84 loc) · 1.7 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <Arduino.h>
#include <EEPROM.h>
#include "TimerTrigger.h"
void TimerTrigger::init (uint8_t pinNumber) {
pin = pinNumber;
onState = false;
timeLeft = OFFTIME;
active = true;
lastMillis = millis();
modified = false;
problem = false;
steps=0;
}
void TimerTrigger::check(void) {
if (active) {
updateTimeLeft();
if (timeLeft <= 0) {
if (!onState) {
onState = true;
timeLeft = OFFTIME;
lastMillis = millis();
analogWrite(pin, 255);
} else {
// It didn't finish turning within the expected time
problem = true;
}
}
}
}
void TimerTrigger::step(void) {
steps++;
if (steps >= MAXSTEPS) {
onState = false;
problem = false;
steps=0;
analogWrite(pin, 0);
}
}
bool TimerTrigger::getOnState(void) {
return onState;
}
void TimerTrigger::activate (bool on) {
if (on != active) {
active = on;
problem = false;
modified = true;
}
if (!active) {
analogWrite(pin, 0);
}
}
void TimerTrigger::save(int address) {
if (modified){
EEPROM.write(address, active);
}
modified = false;
}
void TimerTrigger::restore(int address) {
active = EEPROM.read(address);
modified = false;
}
bool TimerTrigger::getStatus() {
return !problem;
}
bool TimerTrigger::isActive (void){
return active;
}
unsigned long TimerTrigger::getTimeLeft(void) {
return timeLeft;
}
void TimerTrigger::updateTimeLeft(void) {
unsigned long elapsedSeconds;
if (millis() < lastMillis) { // millis rolled over
elapsedSeconds = ((34359737 - lastMillis) + millis())/1000;
}
else {
elapsedSeconds = (millis() - lastMillis)/1000;
}
if (onState) {
timeLeft = MAXONTIME - elapsedSeconds;
} else {
timeLeft = OFFTIME - elapsedSeconds;
}
}