-
Notifications
You must be signed in to change notification settings - Fork 0
/
Powercell.ino
345 lines (309 loc) · 12.1 KB
/
Powercell.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
#include <Adafruit_NeoPixel.h>
#define NEO_POWER 6 // for powercell
Adafruit_NeoPixel powerStick = Adafruit_NeoPixel(50, NEO_POWER, NEO_GRB + NEO_KHZ800);
#define NEO_CYCLO 3 // for cyclotron
Adafruit_NeoPixel cyclotron = Adafruit_NeoPixel(4, NEO_CYCLO, NEO_GRB + NEO_KHZ800);
// Possible Pack states
bool powerBooted = false; // has the pack booted up
// ##############################
// available options
// ##############################
const bool useGameCyclotronEffect = true; // set this to true to get the fading previous cyclotron light in the idle sequence
const bool useCyclotronFadeInEffect = true; // Instead of the yellow alternate flashing on boot this fades the cyclotron in from off to red
// ##############################
// bootup animation speeds
// ##############################
const unsigned long pwr_boot_interval = 40; // How fast to do the powercell drop animation on bootup
const unsigned long cyc_boot_interval = 400; // If useCyclotronFadeInEffect is false this alternates the cycltron lights yellow
const unsigned long cyc_boot_alt_interval = 150; // How fast to fade in the cyclotron lights from black to red on bootup
// ##############################
// idle animation speeds
// ##############################
const unsigned long pwr_interval = 35; // how fast the powercell cycles: One 14 led cycle per 756 ms
const unsigned long cyc_interval = 770; // how fast the cycltron cycles from one cell to the next: One full rotation every 3024 ms
const unsigned long cyc_fade_interval = 1; // if useGameCyclotronEffect is true this is how fast to fade the previous cyclotron to light to nothing
void setup() {
// configure powercell/cyclotron
powerStick.begin();
powerStick.setBrightness(80);
powerStick.show(); // Initialize all pixels to 'off'
cyclotron.begin();
cyclotron.setBrightness(80);
cyclotron.show(); // Initialize all pixels to 'off'
}
void loop() {
// get the current time
unsigned long currentMillis = millis();
if( powerBooted == false ){
powerSequenceBoot(currentMillis);
} else {
powerSequenceOne(currentMillis, pwr_interval, cyc_interval, cyc_fade_interval);
}
delay(1);
}
int cyclotronRunningFadeOut = 255; // we reset this variable every time we change the cyclotron index so the fade effect works
int cyclotronRunningFadeIn = 0; // we reset this to 0 to fade the cyclotron in from nothing
void setCyclotronLightState(int startLed, int endLed, int state ){
switch ( state ) {
case 0: // set all leds to red
for(int i=startLed; i <= endLed; i++) {
cyclotron.setPixelColor(i, powerStick.Color(255, 0, 0));
}
break;
case 1: // set all leds to orange
for(int i=startLed; i <= endLed; i++) {
cyclotron.setPixelColor(i, powerStick.Color(255, 106, 0));
}
break;
case 2: // set all leds off
for(int i=startLed; i <= endLed; i++) {
cyclotron.setPixelColor(i, 0);
}
break;
case 3: // fade all leds from red
for(int i=startLed; i <= endLed; i++) {
if( cyclotronRunningFadeOut >= 0 ){
cyclotron.setPixelColor(i, 255 * cyclotronRunningFadeOut/255, 0, 0);
cyclotronRunningFadeOut--;
}else{
cyclotron.setPixelColor(i, 0);
}
}
break;
case 4: // fade all leds to red
for(int i=startLed; i <= endLed; i++) {
if( cyclotronRunningFadeIn < 255 ){
cyclotron.setPixelColor(i, 255 * cyclotronRunningFadeIn/255, 0, 0);
cyclotronRunningFadeIn++;
}else{
cyclotron.setPixelColor(i, cyclotron.Color(255, 0, 0));
}
}
break;
}
}
/*************** Powercell/Cyclotron Animations *********************/
/* These allow you to use more than one neopixel per cyclotron if you
* want it brighter. If using only one set start and end to the same
* number */
int c1Start = 0;
int c1End = 0;
int c2Start = 1;
int c2End = 1;
int c3Start = 2;
int c3End = 2;
int c4Start = 3;
int c4End = 3;
unsigned long prevPwrBootMillis = 0; // the last time we changed a powercell light in the boot sequence
unsigned long prevCycBootMillis = 0; // the last time we changed a cyclotron light in the boot sequence
unsigned long prevPwrMillis = 0; // last time we changed a powercell light in the idle sequence
unsigned long prevCycMillis = 0; // last time we changed a cyclotron light in the idle sequence
unsigned long prevFadeCycMillis = 0; // last time we changed a fading cyclotron light in the idle sequence
// LED indexes into the neopixel powerstick chain for the cyclotron
const int powercellLedCount = 15; // total number of led's in the animation
int powerSeqNum = 0; // current running powercell sequence leds
// animation level trackers for the boot and shutdown
int currentBootLevel = 0; // current powercell boot level sequence led
int currentLightLevel = powercellLedCount; // current powercell boot light sequence led
// boot animation on the powercell/cyclotron
bool reverseBootCyclotron = false;
void powerSequenceBoot(unsigned long currentMillis) {
bool doPowercellUpdate = false;
if ((unsigned long)(currentMillis - prevPwrBootMillis) >= pwr_boot_interval) {
// save the last time you blinked the LED
prevPwrBootMillis = currentMillis;
// START POWERCELL
if( currentBootLevel != powercellLedCount ){
if( currentBootLevel == currentLightLevel){
if(currentLightLevel+1 <= powercellLedCount){
powerStick.setPixelColor(currentLightLevel+1, 0);
}
powerStick.setPixelColor(currentBootLevel, powerStick.Color(0, 0, 255));
currentLightLevel = powercellLedCount;
currentBootLevel++;
}else{
if(currentLightLevel+1 <= powercellLedCount){
powerStick.setPixelColor(currentLightLevel+1, 0);
}
powerStick.setPixelColor(currentLightLevel, powerStick.Color(0, 0, 255));
currentLightLevel--;
}
doPowercellUpdate = true;
}else{
powerBooted = true;
currentBootLevel = 0;
currentLightLevel = powercellLedCount;
}
// END POWERCELL
}
// if we have changed an led
if( doPowercellUpdate == true ){
powerStick.show(); // commit all of the changes
}
// START CYCLOTRON
bool doCycUpdate = false;
if( useCyclotronFadeInEffect == false ){
if ((unsigned long)(currentMillis - prevCycBootMillis) >= cyc_boot_interval) {
prevCycBootMillis = currentMillis;
if( reverseBootCyclotron == false ){
setCyclotronLightState(c1Start, c1End, 1);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 1);
setCyclotronLightState(c4Start, c4End, 2);
doCycUpdate = true;
reverseBootCyclotron = true;
}else{
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 1);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 1);
doCycUpdate = true;
reverseBootCyclotron = false;
}
}
}else{
if ((unsigned long)(currentMillis - prevCycBootMillis) >= cyc_boot_alt_interval) {
prevCycBootMillis = currentMillis;
setCyclotronLightState(c1Start, c4End, 4);
doCycUpdate = true;
}
}
if( doCycUpdate == true ){
cyclotron.show(); // send to the neopixels
}
// END CYCLOTRON
}
int cycOrder = 0;
int cycFading = -1;
// normal animation on the bar graph
void powerSequenceOne(unsigned long currentMillis, unsigned long anispeed, unsigned long cycspeed, unsigned long cycfadespeed) {
// START POWERCELL
bool doPowercellUpdate = false;
if ((unsigned long)(currentMillis - prevPwrMillis) >= anispeed) {
// save the last time you blinked the LED
prevPwrMillis = currentMillis;
for ( int i = 0; i <= powercellLedCount; i++) {
if ( i <= powerSeqNum ) {
powerStick.setPixelColor(i, powerStick.Color(0, 0, 150));
} else {
powerStick.setPixelColor(i, 0);
}
}
if ( powerSeqNum <= powercellLedCount) {
powerSeqNum++;
} else {
powerSeqNum = 0;
}
// Update the leds
powerStick.show();
}
// END POWERCELL
// START CYCLOTRON
bool doCycUpdate = false;
if( useGameCyclotronEffect == true ){
// figure out main light
if ((unsigned long)(currentMillis - prevCycMillis) >= cycspeed) {
prevCycMillis = currentMillis;
switch ( cycOrder ) {
case 0:
setCyclotronLightState(c1Start, c1End, 0);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 2);
cycFading = 1;
cyclotronRunningFadeOut = 255;
cycOrder = 1;
break;
case 1:
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 0);
cycFading = 0;
cyclotronRunningFadeOut = 255;
cycOrder = 2;
break;
case 2:
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 0);
setCyclotronLightState(c4Start, c4End, 2);
cycFading = 3;
cyclotronRunningFadeOut = 255;
cycOrder = 3;
break;
case 3:
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 0);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 2);
cycFading = 2;
cyclotronRunningFadeOut = 255;
cycOrder = 0;
break;
}
doCycUpdate = true;
}
// now figure out the fading light
if( (unsigned long)(currentMillis - prevFadeCycMillis) >= cycfadespeed ){
prevFadeCycMillis = currentMillis;
if( cycFading != -1 ){
switch ( cycFading ) {
case 0:
setCyclotronLightState(c1Start, c1End, 3);
break;
case 1:
setCyclotronLightState(c2Start, c2End, 3);
break;
case 2:
setCyclotronLightState(c3Start, c3End, 3);
break;
case 3:
setCyclotronLightState(c4Start, c4End, 3);
break;
}
doCycUpdate = true;
}
}
}else{
// figure out main light
if ((unsigned long)(currentMillis - prevCycMillis) >= cycspeed) {
prevCycMillis = currentMillis;
switch ( cycOrder ) {
case 0:
setCyclotronLightState(c1Start, c1End, 0);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 2);
cycOrder = 1;
break;
case 1:
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 0);
cycOrder = 2;
break;
case 2:
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 2);
setCyclotronLightState(c3Start, c3End, 0);
setCyclotronLightState(c4Start, c4End, 2);
cycOrder = 3;
break;
case 3:
setCyclotronLightState(c1Start, c1End, 2);
setCyclotronLightState(c2Start, c2End, 0);
setCyclotronLightState(c3Start, c3End, 2);
setCyclotronLightState(c4Start, c4End, 2);
cycOrder = 0;
break;
}
doCycUpdate = true;
}
}
if( doCycUpdate == true ){
cyclotron.show(); // send to the neopixels
}
// END CYCLOTRON
}