-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
408 lines (299 loc) · 11 KB
/
main.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <JsonParserGeneratorRK.h>
#include <neopixel.h>
#define PIXEL_COUNT 24
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B
#define BUTTON D5
#define POT A1
#define HOLD_TIME 3
#define NUM_COLORS 10
#define LIMBO 69
#define TIMER 7200
Adafruit_NeoPixel ring(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); // Create ring object
bool lightOn = false; // Light status
int startPress = 0; // Count down time to close lights
int cantPress = false; // Prevents triggering button hold multiple times
int currColor = -1; // Current color of LEDs
int brightness = 20; // Current brightness of LEDs
int lastOn = 0; // Keep track when light was last turned on or pressed
int red = 0;
int green = 0; // Rememeber the RGB values
int blue = 0;
int shutSec = -1; // Timer to shut off the light
int currSec = -1; // Stores current Time.second() to check if a second has passed
int potValue = 0;
// Color array containing 8 colors represented with 3 values
int colorArray[NUM_COLORS][3] = {{255, 0, 0}, // Red
{255, 60, 0}, // Orange
{255, 191, 0}, // Yellow
{191, 255, 0}, // kinda meh light green?
{ 0, 255, 0}, // Green
{ 0, 255, 255}, // Light blue
{ 0, 0, 255}, // Blue
{140, 0, 255}, // Purple hmm
{255, 0, 191}, // Pink hmmm
{255, 255, 255}}; // White
void handler(const String& event, const String& data);
void setLEDColor(const int& color, const int& delayTime);
void publishEvent(const String& event, const String& message);
void turnOff(const int& delayTime);
int approachValue(const int& value, const int& target);
void spiralLight(const int& color, const int& delayTime);
void rainbowOff(const int& delayTime);
void raving();
void updatePotBrightness();
void setup() {
Serial.begin(9600);
Serial.println("Setup!");
// Setup switch
pinMode(BUTTON, INPUT_PULLUP);
// Start the NeoPixel object
ring.begin();
ring.setBrightness(brightness);
// Subscribe to other light's events
Particle.subscribe("turn_on", handler, MY_DEVICES);
Particle.subscribe("turn_off", handler, MY_DEVICES);
}
void loop() {
// Update global brightness based on pot
if (cantPress != LIMBO) {
updatePotBrightness();
}
// Auto turn off light if time is up
if (lightOn == true) {
if (currSec == -1) { currSec = Time.second(); }
// Counting down the timer using Time.second()
if (currSec != Time.second()) {
currSec = Time.second();
shutSec--;
Serial.printlnf("shutSec = %d ------ lightOn = %d", shutSec, lightOn);
}
// Turn off light once timer is up
if (shutSec <= 0) {
Serial.println("10 seconds up, LIGHTS AUTO TURNING OFF");
rainbowOff(4);
}
}
int buttonState = digitalRead(BUTTON);
// No button pressed
if (buttonState == HIGH) {
// Button was just released
if (cantPress == true || cantPress == LIMBO) {
Serial.println("Button released");
if (cantPress == true) {
// Set all lights to the same random colour + don't allow same color as before
int randColor = random(NUM_COLORS);
while (randColor == currColor) { randColor = random(NUM_COLORS); }
spiralLight(randColor, 15);
shutSec = TIMER;
// Publish TURN ON event
String message = "{\"id\":" + System.deviceID() + ",\"color\":"+ randColor +"}";
publishEvent("turn_on", message);
}
if (cantPress == LIMBO) {
turnOff(10);
}
cantPress = false;
startPress = 0;
}
// Button is being pressed
} else {
// Button is pressed + registered
if (cantPress == false) {
Serial.println("Button pressed DOWN");
cantPress = true;
startPress = Time.second();
// Button is still being held down
} else if (cantPress == true) {
if (Time.second() - startPress >= HOLD_TIME) {
Serial.println("Held for 3 seconds, lights OFF");
// Set all lights to blank
rainbowOff(4);
// Publish TURN OFF event
String message = "{\"id\":" + System.deviceID() + "}";
publishEvent("turn_off", message);
cantPress = LIMBO;
delay(20);
}
// Button must have been clacked...
} else if (cantPress == LIMBO) {
Serial.printlnf("Raving");
raving();
}
}
ring.show();
}
// Handles the events
void handler(const String& event, const String& data) {
// Parse the JSON
JsonParser parser;
parser.addString(data);
parser.parse();
String id;
parser.getOuterValueByKey("id", id);
// Don't receive own messages
if (id != System.deviceID()) {
Serial.println("Received event = " + event);
if (event == "turn_on") {
int color;
parser.getOuterValueByKey("color", color);
// Turn lights on to the corresponding colour
spiralLight(color, 15);
shutSec = TIMER;
} else if (event == "turn_off") {
// Turn lights off
rainbowOff(4);
}
}
}
// Set the lights to flash through the rainbow (cowboy style, doesn't set global variables)
void raving() {
int delayTime;
// Set brightness to 1 (1 because otherwise light closes)
brightness = 1;
ring.setBrightness(brightness);
ring.show();
for (int i = 0; i < NUM_COLORS; i++) {
// Set the delayTime based on pot
potValue = analogRead(POT);
int multiple = 5;
delayTime = ((map(potValue, 0, 4095, 10, 100) + multiple/2) / multiple) * multiple / 10;
if (i == 1 || i == 3 || i == 8) {
continue;
}
Serial.printlnf("Color = %d", i);
// Set ring to a certain colour
Serial.printlnf("Colors [%d, %d, %d]", colorArray[i][0], colorArray[i][1], colorArray[i][2]);
for (int j = 0; j < PIXEL_COUNT; j++) {
ring.setPixelColor(j, colorArray[i][0], colorArray[i][1], colorArray[i][2]);
}
ring.show();
// Delay before flashing in
delay(delayTime*10);
// Flashing in
while (brightness < 100) {
brightness++;
ring.setBrightness(brightness);
ring.show();
delay(delayTime/12);
}
// Delay before flashing out
delay(delayTime*6);
// Flashing out
while (brightness > 1) {
brightness--;
ring.setBrightness(brightness);
ring.show();
delay(delayTime/8);
}
}
}
// Check's the pot and updates the global brightness
void updatePotBrightness() {
potValue = analogRead(POT);
int multiple = 5;
int tempBright = ((map(potValue, 0, 4095, 10, 100) + multiple/2) / multiple) * multiple;
if (tempBright != brightness) {
ring.setBrightness(tempBright);
brightness = tempBright;
Serial.printlnf("Brightness set to %d", brightness);
}
}
// Sets the ring to a specific color
// 0 - 8 = a nice colour
void setLEDColor(const int& color, const int& delayTime) {
Serial.printlnf("SetLEDColor called with color = %d", color);
int r = colorArray[color][0];
int g = colorArray[color][1];
int b = colorArray[color][2];
//Serial.printlnf("Fading goals = [%d, %d, %d]", r, g, b);
//Serial.printlnf("before fade.... red = %d green = %d blue = %d", red, green, blue);
while (red != r || green != g || blue != b) {
red = approachValue(red, r);
green = approachValue(green, g);
blue = approachValue(blue, b);
for (int i = 0; i < PIXEL_COUNT; i++) {
ring.setPixelColor(i, red, green, blue);
}
ring.show();
delay(delayTime);
//Serial.printlnf("iterating... red = %d green = %d blue = %d", red, green, blue);
}
//Serial.printlnf("AFTER fade.... red = %d green = %d blue = %d", red, green, blue);
lightOn = true;
lastOn = Time.second();
//Serial.printlnf("LastOn set to = %d", lastOn);
currColor = color;
}
// Given a current value and a target value, make value approach target value in increments of 10
int approachValue(const int& value, const int& target) {
int skip = 10; // How much to increment
if (value > target) {
if (value >= target + skip) {
return value - skip;
} else {
return target;
}
} else if (value < target) {
if (value <= target - skip) {
return value + skip;
} else {
return target;
}
}
}
// Light up the ring using a spiral effect
void spiralLight(const int& color, const int& delayTime) {
Serial.printlnf("spiralLight called with color = %d", color);
int r = colorArray[color][0];
int g = colorArray[color][1];
int b = colorArray[color][2];
for (int i = 0; i < PIXEL_COUNT; i++) {
ring.setPixelColor(i, r, g, b);
ring.show();
delay(delayTime);
}
Serial.printlnf("set pixel colors to %d %d %d", r, g, b);
// Set global variables
red = r;
green = g;
blue = b;
lightOn = true;
lastOn = Time.second();
currColor = color;
}
// Give a nice rainbow before turning off
void rainbowOff(const int& delayTime) {
Serial.printlnf("Rainbowing off");
for (int i = 0; i < NUM_COLORS; i++) {
spiralLight(i, delayTime);
}
turnOff(2);
}
// Turn off LED's via fading
void turnOff(const int& delayTime) {
Serial.printlnf("Turning off");
while (brightness > 0) {
brightness--;
ring.setBrightness(brightness);
ring.show();
delay(delayTime);
}
red = 0;
green = 0;
blue = 0;
lightOn = false;
currColor = -1;
shutSec = -1;
currSec = -1;
}
// Publishes private event + gives error message if it fails
void publishEvent(const String& event, const String& message) {
bool success;
success = Particle.publish(event, message, PRIVATE);
if (!success) {
Serial.println("Publishing event [" + event + "] FAILEDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD :(");
} else {
Serial.println("Published event [" + event + "] successful!");
}
}