-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightAndChill.ino
More file actions
402 lines (329 loc) · 6.16 KB
/
LightAndChill.ino
File metadata and controls
402 lines (329 loc) · 6.16 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
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
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
// Pins
#define pin_LED_Ready 13
#define pin_LED_Blue 9
#define pin_LED_Red 10
#define pin_LED_Green 11
// Colors loop
#define BLUE_PLUS 0
#define RED_MINUS 1
#define GREEN_PLUS 2
#define BLUE_MINUS 3
#define RED_PLUS 4
#define GREEN_MINUS 5
// Modes
#define MUSIC1 1
#define MUSIC2 2
#define RAINBOW 5
#define SCENARIO 6
#define COLOR 7
#define OFF 10
#define DEBUG false
/////////////// Globales variables ///////////////
// Colors
int red = 255;
int green = 0;
int blue = 0;
char state = BLUE_PLUS;
int LED = LOW;
int mode = RAINBOW;
// Vars
int limit = 20;
int step = 1;
int interval = 2;
int value = 0;
BridgeServer server;
/////////////// BASE ///////////////
void setup()
{
pinMode(pin_LED_Blue, OUTPUT);
pinMode(pin_LED_Red, OUTPUT);
pinMode(pin_LED_Green, OUTPUT);
pinMode(pin_LED_Ready, OUTPUT);
Serial.begin(9600);
Bridge.begin();
server.listenOnLocalhost();
server.begin();
digitalWrite(pin_LED_Ready, HIGH);
}
// ----------------------------------
void loop()
{
getLimit();
processCommand();
processMode();
delay(interval);
}
/////////////// PROCESS ///////////////
void processCommand()
{
BridgeClient client = server.accept();
if (!client)
{
return;
}
String command = client.readStringUntil('/');
command.trim();
if (command == "color")
{
red = client.readStringUntil('/').toInt();
green = client.readStringUntil('/').toInt();
blue = client.readStringUntil('/').toInt();
mode = COLOR;
}
if (command == "music1")
{
mode = MUSIC1;
}
if (command == "music2")
{
mode = MUSIC2;
}
if (command == "rainbow")
{
mode = RAINBOW;
}
if (command == "scenario")
{
mode = SCENARIO;
}
if (command == "off")
{
mode = OFF;
}
client.stop();
}
// ----------------------------------
void processMode()
{
switch (mode)
{
case COLOR:
color();
break;
case MUSIC1:
music1();
break;
case MUSIC2:
music2();
break;
case RAINBOW:
rainbow();
break;
case SCENARIO:
scenario();
break;
case OFF:
default:
off();
break;
}
}
/////////////// MODES ///////////////
void color()
{
setColor(red, green, blue);
}
void music1()
{
getMic();
//getSerial();
calculColor();
if (LED == HIGH)
{
setColor(red, green, blue);
}
else
{
off();
}
}
// ----------------------------------
void music2()
{
interval = 50;
if (Serial.available() > 0)
{
step = Serial.read() + 1;
//state = map(value, 0, 255, 0, 5);
}
calculColor();
setColor(red, green, blue);
}
// ----------------------------------
void rainbow()
{
interval = 2;
calculColor();
setColor(red, green, blue);
}
// ----------------------------------
int scenarioIndex = 0;
//String scenarioData = "2#255,0,0,200|0,0,255,200"; // Police
String scenarioData = "3#0,0,255,500|255,255,255,500|255,0,0,500"; // France
void scenario()
{
int count = getValue(scenarioData, '#', 0).toInt();
String data = getValue(scenarioData, '#', 1);
String scene = getValue(data, '|', scenarioIndex);
red = getValue(scene, ',', 0).toInt();
green = getValue(scene, ',', 1).toInt();
blue = getValue(scene, ',', 2).toInt();
interval = getValue(scene, ',', 3).toInt();
setColor(red, green, blue);
scenarioIndex++;
if (scenarioIndex >= count) scenarioIndex = 0;
}
// ----------------------------------
void off()
{
setColor(0, 0, 0);
}
/////////////// DATAS ///////////////
void getMic()
{
int value = analogRead(A0);
//Serial.print(" A0: ");
//Serial.println(value);
if (value > limit)
{
LED = HIGH;
}
else
{
LED = LOW;
}
}
// ----------------------------------
void getSerial()
{
if (Serial.available() > 0)
{
value = Serial.read();
}
if (value > limit)
{
LED = HIGH;
}
else
{
LED = LOW;
}
}
// ----------------------------------
void getLimit()
{
//limit = analogRead(A1) * 0.04;
//limit = analogRead(A1) / 4;
//limit = analogRead(A1);
limit = map(analogRead(A1), 0, 1023, -10, 255);
//Serial.print("A1: ");
//Serial.print(limit);
}
/////////////// UTILS ///////////////
void setColor(int _red, int _green, int _blue)
{
if (_red > 255) _red = 255;
if (_green > 255) _green = 255;
if (_blue > 255) _blue = 255;
if (_red < 0) _red = 0;
if (_green < 0) _green = 0;
if (_blue < 0) _blue = 0;
analogWrite(pin_LED_Red, _red);
analogWrite(pin_LED_Green, _green);
analogWrite(pin_LED_Blue, _blue);
if (DEBUG)
{
Serial.print((int)_red);
Serial.print(" ");
Serial.print((int)_green);
Serial.print(" ");
Serial.println((int)_blue);
}
}
// ----------------------------------
void calculColor()
{
switch (state)
{
case BLUE_PLUS:
{
blue += step;
if (blue >= 255)
{
blue = 255;
state = RED_MINUS;
}
break;
}
case RED_MINUS:
{
red -= step;
if (red <= 0)
{
red = 0;
state = GREEN_PLUS;
}
break;
}
case GREEN_PLUS:
{
green += step;
if (green >= 255)
{
green = 255;
state = BLUE_MINUS;
}
break;
}
case BLUE_MINUS:
{
blue -= step;
if (blue <= 0)
{
blue = 0;
state = RED_PLUS;
}
break;
}
case RED_PLUS:
{
red += step;
if (red >= 255)
{
red = 255;
state = GREEN_MINUS;
}
break;
}
case GREEN_MINUS:
{
green -= step;
if (green <= 0)
{
green = 0;
state = BLUE_PLUS;
}
break;
}
}
}
// ----------------------------------
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++)
{
if (data.charAt(i) == separator || i == maxIndex)
{
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}