-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoquaciousAndLovely2015.ino
250 lines (214 loc) · 7.31 KB
/
LoquaciousAndLovely2015.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
#include <SPI.h>
#include <LPD8806.h> // C
int n_strips = 5;
LPD8806 head;
LPD8806 leftFrontLeg;
LPD8806 leftBackLeg;
LPD8806 rightFrontLeg;
LPD8806 rightBackLeg;
LPD8806 strips[5]; // C
// LPD8806[] strips = new LPD8806[5]; // Java
// Offsets for the front-most LED
int offsets[] = { // C
// int offsets[] = new int[] { // Java
29,25,33,25,20
};
int mode = 0;
int idx = 0;
int n_modes = 12;
int modes[] = { // C
// int modes[] = new int[] { // Java
600, 300, 300, 300, 300, 300,
300, 300, 300, 300, 300, 300
};
uint32_t currentColor; // C
// color currentColor; // Java
int n_colors = 4;
byte colors[] = { // C
// int colors[] = new int[] { // Java
107,18,76, // Pink
32,112,104, // Turquoise
0,17,51, // Royal Blue
119,25,32 // Red
};
uint32_t color(byte r, byte g, byte b) { // C
return ((uint32_t)(g | 0x80) << 16) | // C
((uint32_t)(r | 0x80) << 8) | // C
b | 0x80 ; // C
} // C
// C
byte red(uint32_t color) { // C
return (byte)(color & 0x7F00) >> 8; // C
} // C
// C
byte green(uint32_t color) { // C
return (byte)(color & 0x7F0000)>>16; // C
} // C
// C
byte blue(uint32_t color) { // C
return (byte)(color & 0x7F); // C
} // C
void setup() {
Serial.begin(9600);
Serial.println("Setup strips");
strips[0] = head = LPD8806(100,2,7); // C
strips[1] = leftFrontLeg = LPD8806(80,1,6); // C
strips[2] = leftBackLeg = LPD8806(80,0,5); // C
strips[3] = rightBackLeg = LPD8806(80,3,8); // C
strips[4] = rightFrontLeg = LPD8806(80,4,16); // C
// strips[0] = head = new LPD8806(100); // Java
// strips[1] = leftFrontLeg = new LPD8806(80); // Java
// strips[2] = leftBackLeg = new LPD8806(80); // Java
// strips[3] = rightBackLeg = new LPD8806(80); // Java
// strips[4] = rightFrontLeg = new LPD8806(80); // Java
for (int i=0; i<n_strips; i++) {
strips[i].begin();
}
}
uint32_t stepColor(byte r, byte g, byte b, int step, float divisor) { // C
// color stepColor(int r, int g, int b, int step, float divisor) { // Java
r = int(min(127,step/divisor*r));
g = int(min(127,step/divisor*g));
b = int(min(127,step/divisor*b));
return color(r,g,b);
}
int bound(int n, int max) {
while (n >= max) n -= max;
while (n < 0) n += max;
return n;
}
void gallop(int idx) {
int tailsize = 20;
int x;
int step;
int len;
int hlen;
clear();
for (int i=0; i<n_strips; i++) {
len = strips[i].numPixels();
hlen = ((tailsize+len)/2) + tailsize/2;
step = (idx + i*8) % hlen - tailsize - tailsize/2;
for (int j=0; j<tailsize && step+j<=len/2; j++) {
if (step+j>=0) {
x = bound(offsets[i]+step+j, len);
strips[i].setPixelColor(x,stepColor(127,127,127,j,tailsize));
x = bound(offsets[i]-step-j, len);
strips[i].setPixelColor(x,stepColor(127,127,127,j,tailsize));
}
}
strips[i].show();
}
}
void colorBleed(int idx, int r, int g, int b, float div) {
int x;
for (int i=0; i<n_strips; i++) {
x = strips[i].numPixels()/2;
strips[i].setPixelColor(x,stepColor(r,g,b,idx,div));
for (int j=1; j<idx; j++) {
if (x+j<strips[i].numPixels())
strips[i].setPixelColor(x+j, stepColor(r,g,b,idx-j,div));
if (x-j>=0)
strips[i].setPixelColor(x-j, stepColor(r,g,b,idx-j,div));
}
strips[i].show();
}
}
void colorWipe(int idx, uint32_t col) { // C
// void colorWipe(int idx, color col) { // Java
for (int strip = 0; strip < 5; strip++) {
for (int led = 0; led < strips[strip].numPixels (); led++) {
if ( led <= ((strips[strip].numPixels() / float(modes[mode])) * idx)) {
strips[strip].setPixelColor(led, col);
} else {
// Leave it be
}
}
strips[strip].show();
}
}
void rainbowWarp(int idx) {
if (idx % 10 == 0) {
for (int i = 0; i < 5; i++) {
for (int section = 0; section < 5; section++) {
int led = (strips[i].numPixels() / 5) * section;
int col = int(random(n_colors));
int step = int(random(20));
//strips[i].setPixelColor(led, color(random(50, 255), random(50, 255), random(50, 255)));
strips[i].setPixelColor(led, stepColor(colors[col],colors[col+1],colors[col+2],step,10.0));
}
}
}
for (int strip = 0; strip < 5; strip++) {
for (int led = strips[strip].numPixels () - 1; led > 0; led--) {
strips[strip].setPixelColor(led, strips[strip].getPixelColor(led - 1));
}
strips[strip].show();
}
}
void blinkyBlinky(int idx) {
for (int i = 0; i < 5 ; i++) {
for (int led = 0; led < strips[i].numPixels (); led++) {
if (idx % 6 < 3) {
strips[i].setPixelColor(led, color(255, 255, 255));
} else {
strips[i].setPixelColor(led, color(0, 0, 0));
}
}
strips[i].show();
}
}
void sparklySparkly(int idx) {
if (idx % 4 == 0) {
for (int i = 0; i < 5; i++) {
for (int led = 0; led < strips[i].numPixels(); led++) {
if (random(3) > 2) strips[i].setPixelColor(led, color(127, 127, 127));
else strips[i].setPixelColor(led, color(0, 0, 0));
}
strips[i].show();
}
}
}
void clear() {
for (int i=0; i<n_strips; i++) {
for (int led=0; led<strips[i].numPixels(); led++) {
strips[i].setPixelColor(led, color(0,0,0));
}
}
}
void loop() {
idx++;
if (idx == modes[mode]) {
idx = 0;
mode++;
if (mode == n_modes) mode = 0;
Serial.print("Mode is now ");
Serial.print(mode);
Serial.println();
}
switch(mode) {
case 0:
gallop(idx);
break;
case 1:
case 2:
case 3:
case 4:
colorBleed(idx, colors[(mode-1)*3], colors[(mode-1)*3+1], colors[(mode-1)*3+2], modes[mode] / 4.0);
break;
case 5:
case 6:
case 7:
case 8:
colorWipe(idx, color(colors[(mode-5)*3], colors[(mode-5)*3+1], colors[(mode-5)*3+2]));
break;
case 9:
case 10:
case 11:
rainbowWarp(idx);
break;
case 12:
sparklySparkly(idx);
break;
}
delay(10);
}