-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberkana_v2_2_non_blocking.ino
550 lines (495 loc) · 13.5 KB
/
berkana_v2_2_non_blocking.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/***************************************************************************/
//LED sign for our home using RadioShack Tricolor LED Strip
// By Richard Bailey
//
// Version 2.2.3
// User interface fixed for now, brightness implemented.
// Future updates from this point will most likely to clean up the code and reduce size.
//
// Version 2.2.2
// Start implementation of menu options to adjust brightness and frame speed
// frame delay adjustment implemented, need to tweak user interface to be more
// user friendly.
//
// Version 2.2.1
// move kelley pattern out of main loop into subroutine as prep for menu system.
//
// Version 2.2.0
// physical prototyping board with LED connector, analog color selector and circuits on it built.
// incorporate LCD sample code to begin working on menu system.
// reorganize code to be closer to standard practices.
//
// Version 2.1.1
// complete restucture to stop using delay()
//
// Version 2.1.0
// convert colors from RGB to HSV to allow simple addition of color picker
//
// Version 2.0.4
// June 24, 2014
// implement use of high_intensity variable, move blanking to subroutine, and bug fixes
//
//
// Version 2.0.3
// June 20, 2014
// setup color codes into two dimensional array and a code option to
// have random colors. Currently set to cycle through the preprogrammed colors.
// Need to get the display shield to program the
// interface for stand alone operation as well as a 12vdc power supply
// of at least 2 amps.
//
//
// Version 2.0.2
// June 20, 2014
// converted from using hex color codes to individual RGB colors
// to allow custom color selection with on board controls planned
// for later versions.
// now includes output to serial for debugging use.
//
//
// Version 2.0.1
// complete rebuild using FastLED library instead of the RadioShack code.
/***************************************************************************/
#include <avr/pgmspace.h>
#include <FastLED.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// Variable definitions start here
// How many leds are in the strip?
#define NUM_LEDS 12
// Data pin that led data will be written out over
#define DATA_PIN 11
// start points for frames DO NOT CHANGE, THE CURRENT CODE WILL BREAK.
#define xstart 1
#define ystart 2
// define analog color pins for color selector output
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
// define the delay between frames in milliseconds.
long frame_delay = 50;
// define intensity levels
uint8_t high_intensity = 128;
uint8_t med_intensity = high_intensity * .66;
uint8_t low_intensity = high_intensity * .33;
// This is an array of leds. One item for each led in your strip.
CRGB leds[NUM_LEDS];
// predefined color arrays
unsigned int colors[][2] = {
{
64,255 }
, // yellow
{
0,255 }
, //red
{
160,255 }
, //blue
{
96,255 }
, //green
{
192,255 }
, //purple
{
255,0 }
, //white
};
// define menus
// main menu
char* main_menu[] = {"Kelley Yellow","Kelley Red","Kelley Blue","Kelley Green","Kelley Purple","Kelley White","Kelley dial","Rainbow chase", "Rainbow cycle", "Frame Delay", "Brightness"};
int menu_count = 11;
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
//initialize variables
int x; // variable for first group, counts down and wraps around.
int y; // variable for second group, counts up.
unsigned int led_color[2]; //array that holds the current color codes for the LEDS
int c = 0;
long last_millis;
long current_millis;
int frame_count;
static uint8_t hue;
bool kpattern_selected = true;
bool chase_selected = false;
bool cycle_selected = false;
bool idle = true;
int kelley_menu_selection = 0;
int menu_active=0;
int menu_current = menu_active;
int menu_previous = menu_current;
long last_button;
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// ***********************************************************************************************************
// *
// * Power Up Init.
// *
// *
// ***********************************************************************************************************
void setup() {
// Serial.begin(9600);
// sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(3000);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD. We track how long it takes since
// this library has been optimized a bit and we're proud of it :)
// int time = millis();
lcd.print(F("Berkana"));
// time = millis() - time;
// Serial.print(F("Took "));
// Serial.print(time);
// Serial.println(F(" ms"));
lcd.setBacklight(WHITE);
// Change this line as needed to match the LED string type, control chip and color sequence
FastLED.addLeds<TM1803, DATA_PIN, GBR>(leds, NUM_LEDS); // RadioShack LED String
// turn off all leds
fill_solid ( &(leds[0]), NUM_LEDS, CRGB::Black);
FastLED.show();
delay (200);
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
// Flash the "hello" color sequence: R, G, B, black.
colorBars();
last_millis=millis();
last_button=millis();
frame_count=0;
led_color[0] = colors[c][0];
led_color[1] = colors[c][1];
}
// ***********************************************************************************************************
// *
// * Main Loop
// *
// *
// ***********************************************************************************************************
void loop()
{
// read the analog in value: and display on sample LED
sensorValue = analogRead(analogInPin);
hue = map(sensorValue, 0, 1023, 0, 255);
// Use FastLED automatic HSV->RGB conversion
showAnalogRGB( CHSV( hue, 255, high_intensity) );
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
//lcd.print("Active: ");
lcd.print(main_menu[menu_active]);
// menu_previous = menu_current;
if ((millis()-last_button) > 10000 && menu_active <9){
if (idle == false){
lcd.setCursor(0,0);
lcd.print(" ");
idle = true;
}
lcd.setCursor(0,0);
lcd.print("Berkana");
}
uint8_t buttons = lcd.readButtons();
if ((millis()-last_button) > 333) {
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_UP) {
if (menu_current == 0){
menu_current = menu_count-1;
}
else {
menu_current = menu_current-1;
}
last_button=millis();
idle = false;
//lcd.print("Menu: ");
lcd.print(main_menu[menu_current]);
// lcd.setBacklight(GREEN);
//lcd.print("up");
}
if (buttons & BUTTON_DOWN) {
if (menu_current == menu_count-1) {
menu_current = 0;
}
else {
menu_current=menu_current+1;
}
last_button=millis();
idle = false;
//lcd.print("Menu: ");
lcd.print(main_menu[menu_current]);
}
if (menu_active==9){
//lcd.clear();
lcd.setCursor(0,0);
//lcd.print(frame_delay);
if (buttons & BUTTON_LEFT) {
//lcd.setCursor(0,1);
if (frame_delay > 50) {
frame_delay=(frame_delay - 50);
lcd.print(frame_delay);
}
}
if (buttons & BUTTON_RIGHT) {
//lcd.clear();
//lcd.setCursor(0,0);
//lcd.print(main_menu[menu_current]);
//lcd.setCursor(0,1);
if (frame_delay < 5000) {
frame_delay= (frame_delay + 50);
lcd.print(frame_delay);
}
}
if (buttons & BUTTON_SELECT) {
menu_active=menu_previous;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Berkana");
}
}
if (menu_active==10){
lcd.setCursor(0,0);
if (buttons & BUTTON_LEFT) {
if (high_intensity > 72) {
high_intensity=(high_intensity - 4);
lcd.print(high_intensity);
}
}
if (buttons & BUTTON_RIGHT) {
if (high_intensity < 252) {
high_intensity= (high_intensity + 4);
lcd.print(high_intensity);
}
}
if (buttons & BUTTON_SELECT) {
menu_active=menu_previous;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Berkana");
}
}
if (buttons & BUTTON_SELECT) {
menu_previous=menu_active;
menu_active=menu_current;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Berkana");
//lcd.setCursor(0,0);
//lcd.print("Menu: ");
//lcd.print(main_menu[menu_current]);
}
}
}
choice_update();
current_millis=millis();
if (kpattern_selected)
{ kelley_pattern();
};
if (chase_selected)
{
chase_sub();
};
if (cycle_selected)
{
cycle_sub();
};
}
// ***********************************************************************************************************
// *
// * Subroutines
// *
// *
// ***********************************************************************************************************
void showAnalogRGB( const CRGB& rgb)
{
// showAnalogRGB: this is like FastLED.show(), but outputs on
// analog PWM output pins instead of sending data to an intelligent,
// pixel-addressable LED strip.
//
// This function takes the incoming RGB values and outputs the values
// on three analog PWM output pins to the r, g, and b values respectively.
analogWrite(REDPIN, rgb.r );
analogWrite(GREENPIN, rgb.g );
analogWrite(BLUEPIN, rgb.b );
}
void kelley_pattern() {
// subroutine to display kelley pattern in selected color
if (kelley_menu_selection < 6) {
led_color[0] = colors[kelley_menu_selection][0];
led_color[1] = colors[kelley_menu_selection][1];
};
if (kelley_menu_selection == 6){
// set color to analog picker color
led_color[0]= hue;
led_color[1]= 255;
};
if (current_millis-last_millis > frame_delay) {
delay(0);
last_millis=current_millis;
if (frame_count == 0 ) {
x=xstart;
y=ystart;
frame_count++;
return;
}
if (frame_count == 1 ) {
kelley_frame();
return;
}
if (frame_count == 2){
kelley_frame();
return;
}
if (frame_count == 3) {
kelley_frame();
return;
}
if (frame_count == 4) {
kelley_frame();
return;
}
if (frame_count == 5) {
kelley_frame();
return;
}
if (frame_count == 6) {
kelley_frame();
return;
}
if (frame_count == 7) {
kelley_frame();
return;
}
if (frame_count == 8) {
kelley_frame();
return;
}
if (frame_count == 9) {
x=xstart;
y=ystart;
kelley_blank();
return;
}
if (frame_count == 10){
kelley_blank();
return;
}
if (frame_count == 11){
kelley_blank();
return;
}
if (frame_count == 12){
kelley_blank();
return;
}
if (frame_count == 13){
kelley_blank();
return;
}
if (frame_count == 14){
kelley_blank();
frame_count = 0;
return;
}
}
}
void kelley_frame() {
// generates frames for kelley_pattern
if (frame_count <= (NUM_LEDS/2)){
leds[(x+NUM_LEDS) % NUM_LEDS]=CHSV(led_color[0],led_color[1],low_intensity);
leds[y] = leds[(x+NUM_LEDS) % NUM_LEDS];
}
if (frame_count >1 && frame_count <= (NUM_LEDS/2)+1) {
leds[(1+x+NUM_LEDS) % NUM_LEDS]=CHSV(led_color[0],led_color[1],med_intensity);
leds[y-1] = leds[(1+x+NUM_LEDS) % NUM_LEDS];
}
if (frame_count >2 && frame_count <= (NUM_LEDS/2)+2) {
leds[(2+x+NUM_LEDS) % NUM_LEDS]=CHSV(led_color[0],led_color[1],high_intensity);
leds[y-2] = leds[(2+x+NUM_LEDS) % NUM_LEDS];
}
FastLED.show();
x--;
y++;
frame_count++;
}
void kelley_blank () {
// blanking subroutine for kelley_pattern
leds[(x+NUM_LEDS) % NUM_LEDS]=CHSV(led_color[0],led_color[1],0);
leds[y] = leds[(x+NUM_LEDS) % NUM_LEDS];
FastLED.show();
x--;
y++;
frame_count++;
}
void colorBars()
{
// colorBars: flashes Red, then Green, then Blue, then Black.
// Helpful for diagnosing if you've mis-wired which is which.
showAnalogRGB( CRGB::Red );
fill_solid ( &(leds[0]), NUM_LEDS, CRGB::Red);
FastLED.show();
delay(500);
showAnalogRGB( CRGB::Green );
fill_solid ( &(leds[0]), NUM_LEDS, CRGB::Green);
FastLED.show();
delay(500);
showAnalogRGB( CRGB::Blue );
fill_solid ( &(leds[0]), NUM_LEDS, CRGB::Blue);
FastLED.show();
delay(500);
showAnalogRGB( CRGB::Black );
fill_solid ( &(leds[0]), NUM_LEDS, CRGB::Black);
FastLED.show();
delay(500);
}
void chase_sub() {
if (current_millis-last_millis > frame_delay * .15) {
delay(0);
last_millis=current_millis;
static uint8_t hue = 64;
leds[x % NUM_LEDS]=CHSV(hue--, 255, high_intensity);
FastLED.show();
x++;
}
}
void cycle_sub() {
if (current_millis-last_millis > (frame_delay * .5)) {
delay(0);
last_millis=current_millis;
static uint8_t hue = 64;
FastLED.showColor(CHSV(hue--, 255, high_intensity));
// delay (frame_delay);
}
}
void choice_update()
{
if (menu_active < 7) {
kpattern_selected = true;
chase_selected = false;
cycle_selected = false;
kelley_menu_selection = menu_active;
// Serial.println(kelley_menu_selection);
}
if (menu_active == 7) {
kpattern_selected = false;
chase_selected = true;
cycle_selected = false;
}
if (menu_active == 8) {
kpattern_selected = false;
chase_selected = false;
cycle_selected = true;
}
}