-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-mamiso-fake-alarm.ino
372 lines (312 loc) · 9.87 KB
/
arduino-mamiso-fake-alarm.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "FastLED.h"
// ---------- Constants
#define DEBUG false
#define TIME_MILLISEC 1.0
#define TIME_SEC 1000.0 * TIME_MILLISEC
#define TIME_MINUTE 60.0 * TIME_SEC
#define TIME_HOUR 60.0 * TIME_MINUTE
#define TIME_DAY 24.0 * TIME_HOUR
#define ALARM_STATE_OFFLINE 0
#define ALARM_STATE_WAITING 1
#define ALARM_STATE_ONLINE 2
#define NUM_LEDS 4
#define BRIGHTNESS 100
#define DATA_PIN 3
#define PHOTO_PIN A0
#define LED_TYPE WS2811
#define NUMBER_OF_PATTERNS 7
#define NIGHT_THRESHOLD 580.0
// All patterns are based on this time delay
#define MAIN_DELAY 900.0 * TIME_MILLISEC
// Pattern stays the same for this amount of seconds
#define PATTERN_KEEP_FOR 2.0 * TIME_HOUR
// We check sunlight every after..
#define CHECK_SUNLIGHT_EVERY 1.0 * TIME_MINUTE
// After sunset, we wait the following amount of time before alarm starts
// As we do not know when everybody goes to sleep, we assume a lot of time when "alarm" is online
#define WAIT_AFTER_SUNSET 3.0 * TIME_HOUR
// Reset system after sunrise
// Always keeps system fresh
#define REBOOT_SYSTEM_AFTER_SUNRISE 3.0 * TIME_HOUR
// ---------- Variables
// Number of leds
CRGB leds[NUM_LEDS];
// Current pattern
int pattern = 0;
unsigned int cycles = 0;
// Time to keep track how much time has passed for current pattern
unsigned long pattern_start_millis = 0;
// Alarm state
int alarm_state;
unsigned long alarm_state_started_waiting;
unsigned long alarm_state_last_sunlight_check;
unsigned long alarm_state_sun_went_up;
// Other
double light_history[5];
void setup() {
// To protect led strip
delay(3.0 * TIME_SEC);
// Setup LED strip
FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
// Init time for the first pattern
resetPatternSequence();
alarm_state = ALARM_STATE_OFFLINE;
alarm_state_last_sunlight_check = 0;
light_history[0] = analogRead(A0);
light_history[1] = light_history[0];
light_history[2] = light_history[0];
light_history[3] = light_history[0];
light_history[4] = light_history[0];
#if DEBUG
// Some basic status printing
Serial.begin(9600);
#endif
}
/*
* Reboot system
*/
void(* reboot) (void) = 0;
/*
* Main loop where magic happens
*/
void loop() {
if (millis() - alarm_state_last_sunlight_check > CHECK_SUNLIGHT_EVERY) {
double lastLightValue = getLightValue();
#if DEBUG
Serial.print("We will check sunlight: ");
Serial.println(lastLightValue);
#endif
alarm_state_last_sunlight_check = millis();
// We have sun
if (lastLightValue > NIGHT_THRESHOLD) {
#if DEBUG
Serial.println("..Still sun");
#endif
// This is the first time, let's show a signal
if (alarm_state != ALARM_STATE_OFFLINE) {
sendSignal(2);
alarm_state_sun_went_up = millis();
}
// If sun is up after having at least a cycle, reset the system to keep fresh
if (alarm_state == ALARM_STATE_OFFLINE && millis() - alarm_state_sun_went_up > REBOOT_SYSTEM_AFTER_SUNRISE && cycles > 0)
return reboot();
// Everything goes black
// Sun is up, no need to wait less
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::Black, CHECK_SUNLIGHT_EVERY);
alarm_state = ALARM_STATE_OFFLINE;
return;
}
// It is night
if (alarm_state == ALARM_STATE_OFFLINE) {
#if DEBUG
Serial.println("Sun went down, let's start waiting");
#endif
sendSignal(3);
alarm_state = ALARM_STATE_WAITING;
alarm_state_started_waiting = millis();
delay(CHECK_SUNLIGHT_EVERY);
return;
}
// It is night, are we still waiting?
if (alarm_state == ALARM_STATE_WAITING) {
// Delay after sunset?
if (millis() - alarm_state_started_waiting < WAIT_AFTER_SUNSET) {
#if DEBUG
Serial.println("Sun still down, waiting..");
#endif
delay(CHECK_SUNLIGHT_EVERY);
return;
}
#if DEBUG
Serial.println("Start messing with the led");
#endif
// We do not wait any more
alarm_state = ALARM_STATE_ONLINE;
goToTheNextPattern();
resetPatternTime();
}
}
if (alarm_state == ALARM_STATE_ONLINE) {
executeCurrentPattern();
if (hasPatternExceededTime()) {
goToTheNextPattern();
resetPatternTime();
}
}
}
/*
* Checks if pattern time has exceeded
*/
bool hasPatternExceededTime() {
return millis() - pattern_start_millis > PATTERN_KEEP_FOR;
}
/*
* Gets to the next pattern
*/
void goToTheNextPattern() {
// Deterministic
// pattern = (pattern + 1) % NUMBER_OF_PATTERNS;
// "random"
pattern = (millis() + (int)getLightValue()) % NUMBER_OF_PATTERNS;
#if DEBUG
Serial.print("Pattern ");
Serial.print(pattern + 1);
Serial.println(" starts.");
#endif
}
/*
* Signal
*/
void sendSignal(unsigned int times) {
while(times > 0) {
times -= 1;
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, TIME_SEC / 15);
setView(CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, TIME_SEC / 15);
setView(CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, TIME_SEC / 15);
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, TIME_SEC / 15);
setView(CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, TIME_SEC / 15);
setView(CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, TIME_SEC / 15);
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, TIME_SEC / 15);
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::Black, TIME_SEC / 15);
}
}
/*
* Reset pattern sequence
*/
void resetPatternSequence() {
// Visual indicator sequence has been reset
sendSignal(1);
resetPatternTime();
pattern = 0;
}
/*
* Resets pattern time
*/
void resetPatternTime() {
cycles = 0;
pattern_start_millis = millis();
}
/*
* Executes current pattern
*/
void executeCurrentPattern() {
cycles += 1;
if (pattern == 0)
pattern_01();
else if (pattern == 1)
pattern_02();
else if (pattern == 2)
pattern_03();
else if (pattern == 3)
pattern_04();
else if (pattern == 4)
pattern_05();
else if (pattern == 5)
pattern_06();
else if (pattern == 6)
pattern_07();
// Something wierd happened, let's restart
else {
#if DEBUG
Serial.println("Do no know how to handle pattern, restarting");
#endif
resetPatternSequence();
}
}
/*
* Prints a pattern
*/
void setView(CRGB l1, CRGB l2, CRGB l3, CRGB l4, unsigned long del) {
leds[0] = l1;
leds[1] = l2;
leds[2] = l3;
leds[3] = l4;
FastLED.show();
delay(del);
}
// XXX --- --- ---
// --- XXX --- ---
// --- --- XXX ---
// --- --- --- XXX
void pattern_01() {
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, MAIN_DELAY);
}
// XXX --- --- ---
// --- XXX --- ---
// --- --- XXX ---
// --- --- --- XXX
// --- --- XXX ---
// --- XXX --- ---
void pattern_02() {
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, MAIN_DELAY);
}
// XXX XXX --- ---
// --- --- XXX XXX
void pattern_03() {
setView(CRGB::White, CRGB::White, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::White, CRGB::White, MAIN_DELAY);
}
// XXX --- --- ---
// XXX --- --- XXX
void pattern_04() {
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::White, MAIN_DELAY);
}
// XXX --- --- ---
// --- --- --- XXX
void pattern_05() {
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, MAIN_DELAY);
}
// XXX --- --- ---
// XXX XXX --- ---
// XXX XXX XXX ---
// XXX XXX XXX XXX
// --- --- --- ---
void pattern_06() {
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::White, CRGB::White, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::White, CRGB::White, CRGB::White, CRGB::Black, MAIN_DELAY);
setView(CRGB::White, CRGB::White, CRGB::White, CRGB::White, MAIN_DELAY);
setView(CRGB::Black, CRGB::Black, CRGB::Black, CRGB::Black, MAIN_DELAY);
}
// XXX --- --- XXX
// XXX --- XXX ---
// XXX XXX --- ---
// XXX --- XXX ---
void pattern_07() {
setView(CRGB::White, CRGB::Black, CRGB::Black, CRGB::White, MAIN_DELAY);
setView(CRGB::White, CRGB::Black, CRGB::White, CRGB::Black, MAIN_DELAY);
setView(CRGB::White, CRGB::White, CRGB::Black, CRGB::Black, MAIN_DELAY);
setView(CRGB::White, CRGB::Black, CRGB::White, CRGB::Black, MAIN_DELAY);
}
/*
* Returns light value
*/
double getLightValue() {
light_history[4] = light_history[3];
light_history[3] = light_history[2];
light_history[2] = light_history[1];
light_history[1] = light_history[0];
int val = 0;
val += analogRead(A0);
val += analogRead(A0);
val += analogRead(A0);
val += analogRead(A0);
light_history[0] =
(val / 4.0) * 0.4 +
light_history[1] * 0.2 +
light_history[2] * 0.2 +
light_history[3] * 0.1 +
light_history[4] * 0.1;
return light_history[0];
}