-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrgb_stip_handler.ino
More file actions
372 lines (308 loc) · 7.74 KB
/
Copy pathrgb_stip_handler.ino
File metadata and controls
372 lines (308 loc) · 7.74 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
#include "SimpleTimer.h"
#include <OneWire.h>
#include <DallasTemperature.h>
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 60
#define ONE_WIRE_BUS 2
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define SIMPLE_COLOR 0
#define RAINBOW 1
#define RAINBOW_CYCLE 2
#define KNGIHT_RIDER 3
#define FADE 4
#define BLINK 5
#define GET_TEMP 6
uint8_t RED = 0;
uint8_t GREEN = 0;
uint8_t BLUE = 0;
const byte numChars = 64;
char receivedChars[numChars]; // an array to store the received data
uint8_t command = 0;
boolean newData = false;
SimpleTimer tempTimer;
/* Function prototypes */
void sendTempToPC();
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
pixels.begin(); // This initializes the NeoPixel library.
pixels.show(); // all pixel OFF
sensors.begin();
sendTempToPC();
/* set timers */
tempTimer.setInterval(60000L, sendTempToPC);
}
void loop()
{
tempTimer.run();
recvWithEndMarker();
showNewData();
}
void sendTempToPC()
{
sensors.requestTemperatures();
Serial.print("Temp:");
Serial.println(sensors.getTempCByIndex(0));
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0)
{
rc = Serial.read();
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
ndx++;
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true)
{
parseData();
newData = false;
}
}
// Split the data into its parts
void parseData()
{
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars,",");
command = atoi(strtokIndx);
strtokIndx = strtok(NULL,",");
RED = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
GREEN = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
BLUE = atoi(strtokIndx);
switch(command)
{
case SIMPLE_COLOR:
set_color(RED, GREEN, BLUE);
break;
case RAINBOW:
rainbow(20);
break;
case RAINBOW_CYCLE:
rainbowCycle(20);
break;
case KNGIHT_RIDER:
knightRider(1, 32, 8, pixels.Color(RED, GREEN, BLUE)); // Cycles, Speed, Width, RGB Color (red)
break;
case FADE:
fadeColor(RED, GREEN, BLUE);
break;
case BLINK:
blinkColor(RED, GREEN, BLUE);
case GET_TEMP:
sendTempToPC();
break;
default:
break;
}
}
void blinkColor(int red, int green, int blue)
{
do
{
for(int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
if (Serial.available() > 0)
{
return 0;
}
pixels.show();
delay(500);
for(int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, pixels.Color(red, green, blue));
}
if (Serial.available() > 0)
{
return 0;
}
pixels.show();
delay(500);
}while(!Serial.available() > 0);
}
void fadeColor(int red, int green, int blue)
{
do
{
for(int i = 100; i >= 0; i--)
{
for(int k = 0; k < pixels.numPixels(); k++)
{
if (Serial.available() > 0)
{
return 0;
}
pixels.setPixelColor(k, pixels.Color((red / 100) * i, (green / 100) * i, (blue / 100) * i));
}
pixels.show();
delay(15);
}
delay(45);
for(int i = 0; i <= 100; i++)
{
for(int k = 0; k < pixels.numPixels(); k++)
{
if (Serial.available() > 0)
{
return 0;
}
pixels.setPixelColor(k, pixels.Color((red / 100) * i, (green / 100) * i, (blue / 100) * i));
}
pixels.show();
delay(10);
}
}while(!Serial.available() > 0);
}
void set_color(int red, int green, int blue)
{
for(int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(red,green,blue));
}
pixels.show();
}
// Cycles - one cycle is scanning through all pixels left then right (or right then left)
// Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
// Width - how wide the trail effect is on the fading out LEDs. The original display used
// light bulbs, so they have a persistance when turning off. This creates a trail.
// Effective range is 2 - 8, 4 is default for 16 pixels. Play with this.
// Color - 32-bit packed RGB color value. All pixels will be this color.
// knightRider(cycles, speed, width, color);
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color)
{
uint32_t old_val[NUMPIXELS]; // up to 256 lights!
do
{
// Larson time baby!
for(int i = 0; i < cycles; i++)
{
for (int count = 1; count < NUMPIXELS; count++)
{
pixels.setPixelColor(count, color);
old_val[count] = color;
if (Serial.available() > 0)
{
return 0;
}
for(int x = count; x > 0; x--)
{
old_val[x-1] = dimColor(old_val[x-1], width);
pixels.setPixelColor(x-1, old_val[x-1]);
}
pixels.show();
delay(speed);
}
for (int count = NUMPIXELS -1; count >= 0; count--)
{
pixels.setPixelColor(count, color);
old_val[count] = color;
if (Serial.available() > 0)
{
return 0;
}
for(int x = count; x <= NUMPIXELS ; x++)
{
old_val[x-1] = dimColor(old_val[x-1], width);
pixels.setPixelColor(x+1, old_val[x+1]);
}
pixels.show();
delay(speed);
}
}
}while(!Serial.available() > 0);
}
uint32_t dimColor(uint32_t color, uint8_t width)
{
return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}
void rainbow(uint8_t wait)
{
uint16_t i, j;
do
{
for(j=0; j<256; j++)
{
for(i=0; i < pixels.numPixels(); i++)
{
if (Serial.available() > 0)
{
return 0;
}
pixels.setPixelColor(i, Wheel((i+j) & 255));
}
pixels.show();
delay(wait);
}
}while(!Serial.available() > 0);
}
void rainbowCycle(uint8_t wait)
{
uint16_t i, j;
do
{
for(j=0; j<256*5; j++) // 5 cycles of all colors on wheel
{
for(i=0; i< pixels.numPixels(); i++)
{
if (Serial.available() > 0)
{
return 0;
}
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}while(!Serial.available() > 0);
}
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if(WheelPos < 85)
{
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170)
{
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}