-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuchtVerfrisser.ino
191 lines (165 loc) · 4.46 KB
/
LuchtVerfrisser.ino
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <EEPROM.h>
#define freshener 13
//for testing purposes
#define doorDistance 75
#define cleaningDelay 30000
#define notInUseDelay 5000
#define maxType1Distance 90
#define type1Delay 5000
#define maxType2Distance 50
#define type2Delay 25000
#define triggeredDelay 1000
uint16_t sprayDelay = 3000; // delay in ms
#define freshenerOnTime 1000
#define spraysRemainingAddress 0
#define emptyEepromValue 65535
#define startSpraysRemaining 2400
uint16_t spraysRemaining;
unsigned long stateTransitionAt;
unsigned long sprayInterval;
uint8_t spraying;
// number of sprays imminent
uint8_t nSprays;
// Number of times to spray for number 1 or number 2
uint8_t nSpraysUse1 = 1;
uint8_t nSpraysUse2 = 2;
uint8_t nSpraysOverride = 1;
unsigned long enteredType1Distance;
unsigned long enteredType2Distance;
uint8_t doorOpenSinceStateChange;
enum State {
notInUse,
useUnknown,
type1Use,
type2Use,
cleaning,
triggered,
menu
};
enum State state = notInUse;
bool stateChanged;
void setup() {
Serial.begin(9600);
setupLEDs();
setupLCD();
setupSensors();
setupButtons();
pinMode(freshener, OUTPUT);
stateChanged = true;
getTemperature();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
EEPROM.get(spraysRemainingAddress, spraysRemaining);
if(spraysRemaining == emptyEepromValue){
resetSpraysRemaining();
}
}
void loop() {
if (stateChanged) { // handle a state change
doStateTransition();
stateChanged = false;
}
changeLEDcolor();
// i don't understand why, but these can't be in the cases below
unsigned long lastMotionDetected = getLastMotionDetected();
uint8_t magnetReading = readMagnet();
getDistance();
if (getDistance() > maxType1Distance) {
enteredType1Distance = millis();
}
if (getDistance() > maxType2Distance) {
enteredType2Distance = millis();
}
switch (state) {
case notInUse:
printTemperature();
if (readMotionDetector() == HIGH) {
setNewState(useUnknown);
}
break;
case useUnknown:
if (millis() - lastMotionDetected > notInUseDelay && getDistance() > doorDistance) {
setNewState(notInUse);
} else if (millis() - enteredType1Distance > type1Delay && magnetReading == HIGH) {
setNewState(type1Use);
} else if (millis() - stateTransitionAt > cleaningDelay && magnetReading == LOW) {
setNewState(cleaning);
}
break;
case type1Use:
if (millis() - enteredType2Distance > type2Delay && magnetReading == HIGH) {
setNewState(type2Use);
}
case type2Use:
if (magnetReading == LOW) {
doorOpenSinceStateChange = true;
}
if (doorOpenSinceStateChange //door has been opened
&& readMagnet() == HIGH // door is now closed again
&& millis() - getDoorCloseTime() > triggeredDelay // was closed 1 sec ago
&& millis() - lastMotionDetected > triggeredDelay) { // and no motion detected for 1 sec.
trigger(state == type1Use ? nSpraysUse1 : nSpraysUse2);
}
break;
case cleaning:
if (millis() - lastMotionDetected > notInUseDelay && getDistance() > doorDistance) {
setNewState(notInUse);
}
break;
case menu:
checkButtons();
break;
case triggered:
printTriggerCountDown();
if (nSprays <= 0) {
Serial.println("Done!");
setNewState(notInUse);
}
else if (millis() - stateTransitionAt >= sprayDelay) {
spray();
}
break;
}
}
void trigger(int _nSprays) {
nSprays = _nSprays;
setNewState(triggered);
doorOpenSinceStateChange = false;
}
void setNewState(State newState) {
state = newState;
stateChanged = true;
stateTransitionAt = millis();
}
void doStateTransition() {
setLEDColor();
setLCD();
}
void spray() {
clockWatch(freshenerOnTime, &sprayInterval, []() {
if (!spraying) {
spraying = true;
digitalWrite(freshener, HIGH);
}
else {
digitalWrite(freshener, LOW);
spraying = false;
nSprays -= 1;
setSpraysRemaining(spraysRemaining - 1);
}
});
}
void resetSpraysRemaining() {
setSpraysRemaining(startSpraysRemaining);
}
void setSpraysRemaining(uint16_t newSpraysRemaining){
spraysRemaining = newSpraysRemaining;
EEPROM.put(spraysRemainingAddress, spraysRemaining);
}
void clockWatch(int frequency, unsigned long * lastRunMillis, void (*f)()) {
if (millis() - *lastRunMillis >= frequency) {
*lastRunMillis = millis();
(*f)();
}
}