-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb_led_cube.ino
2991 lines (2670 loc) · 95.5 KB
/
rgb_led_cube.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SPI.h>
#define latch_pin 4 // BIT of PortD for latch - UNO pin 2, MEGA pin 4
#define blank_pin 5 // BIT of PortD for blank - UNO pin 3, MEGA pin 5
#define data_pin 51 // used by SPI - UNO pin 11, MEGA pin 51 (MOSI)
#define clock_pin 52 // used by SPI - UNO pin 13, MEGA pin 52 (SCK)
#define layer1 4
#define layer2 5
#define layer3 6
#define layer4 7
#define layer5 8
#define layer6 9
#define layer7 10
#define layer8 11
int layerArray[8] = {
layer1,
layer2,
layer3,
layer4,
layer5,
layer6,
layer7,
layer8
};
int lastAnode;
int shift_out;
byte anode[8]; // byte to write to the anode shift register, 8 of them,
// shifting the ON level in each byte in the array
// This is how the brightness for every LED is stored,
// Each LED only needs a 'bit' to know if it should be ON or OFF, so 64 Bytes
// gives you 512 bits= 512 LEDs. Since we are modulating the LEDs, using 4 bit
// resolution, each color has 4 arrays containing 64 bits each. Notice how more
// resolution will eat up more of your precious RAM.
byte red0[64], red1[64], red2[64], red3[64];
byte blue0[64], blue1[64], blue2[64], blue3[64];
byte green0[64], green1[64], green2[64], green3[64];
int level = 0; // Track level we shift data to
int anodeLevel = 0; // Increments through the anode levels
int BAM_Bit, BAM_Counter = 0; // Keeps track of Bit Angle Modulation
unsigned long start; // Millisecond timer to cycle through the animations
const int CUBE_SIZE = 8;
const int TEMPO = 130; // bpm
const float DIVISOR_4 = 0.25;
const float DIVISOR_8 = 0.125;
const float DIVISOR_16 = 0.0625;
const float DIVISOR_32 = 0.03125;
const float DIVISOR_64 = 0.015625;
const float DIVISOR_128 = 0.0078125;
const float DIVISOR_3 = 0.333333333;
const float DIVISOR_6 = 0.166666667;
int getBeat() {
return int(60000 / TEMPO);
}
int getBeatDivision(float multiplier = 1.00) {
return int(getBeat() * multiplier);
}
int getBeatPerLayer() {
return int(getBeatDivision(1.00 / CUBE_SIZE));
}
int getBeatDivisionPerLayer(float multiplier = 1.00) {
return int(getBeatPerLayer() * multiplier);
}
void setup() {
SPI.setBitOrder(MSBFIRST); // Most Significant Bit First
SPI.setDataMode(SPI_MODE0); // Mode 0 Rising edge of data, keep clock low
SPI.setClockDivider(SPI_CLOCK_DIV2); // Run the data in at 16MHz/2 - 8MHz
// Serial.begin(115200); // Start serial if required
noInterrupts(); // kill interrupts until everybody is set up
// We use Timer 1 to refresh the cube
TCCR1A = B00000000; // Register A all 0's since we're not toggling any pins
TCCR1B = B00001011; // bit 3 set to place in CTC mode, will call an interrupt
// on a counter match
// bits 0 and 1 are set to divide the clock by 64, so 16MHz/64=250kHz
TIMSK1 = B00000010; // bit 1 set to call the interrupt on an OCR1A match
OCR1A = 45; // you can play with this, but I set it to 30, which means:
// our clock runs at 250kHz, which is 1/250kHz = 4us
// with OCR1A set to 30, this means the interrupt will be
// called every (30+1)x4us=124us, which gives a multiplex
// frequency of about 8kHz
// Setup anode array, this is written to anode
// shift register, to enable each level
anode[0] = B11111110;
anode[7] = B11111101;
anode[6] = B11111011;
anode[5] = B11110111;
anode[4] = B11101111;
anode[3] = B11011111;
anode[2] = B10111111;
anode[1] = B01111111;
// Set up Outputs
// pinMode(latch_pin, OUTPUT); //Latch
pinMode(2, OUTPUT); // turn off PWM and set PortD bit 4 as output
pinMode(3, OUTPUT); // turn off PWM and set PortD bit 5 as output
pinMode(data_pin, OUTPUT); // MOSI DATA
pinMode(clock_pin, OUTPUT); // SPI Clock
// pinMode(blank_pin, OUTPUT); // Output Enable - Do this last, so LEDs don't flash on boot
pinMode(layer1, OUTPUT);
pinMode(layer2, OUTPUT);
pinMode(layer3, OUTPUT);
pinMode(layer4, OUTPUT);
pinMode(layer5, OUTPUT);
pinMode(layer6, OUTPUT);
pinMode(layer7, OUTPUT);
pinMode(layer8, OUTPUT);
SPI.begin();
interrupts(); // Start multiplexing
}
void setLED(int level, int row, int column, byte red, byte green, byte blue) {
// Args mean:
// setLED(
// level 0-7,
// row 0-7,
// column 0-7,
// red brightness 0-15,
// green brightness 0-15,
// blue brightness 0-15
// );
// Clamp location between 0 or 7
// Clamp brightness between 0 or 15
if (level<0) level=0;
if (level>7) level=7;
if (row<0) row=0;
if (row>7) row=7;
if (column<0) column=0;
if (column>7) column=7;
if (red<0) red=0;
if (red>15) red=15;
if (green<0) green=0;
if (green>15) green=15;
if (blue<0) blue=0;
if (blue>15) blue=15;
// Translate level, row and column 0 to 511
int whichbyte = int(((level * (CUBE_SIZE * CUBE_SIZE)) + (row * CUBE_SIZE) + column) / CUBE_SIZE);
// The first level LEDs are first in the sequence, then 2nd level, then third,
// and so on the (level*64) is what indexes the level's starting place, so
// level 0 are LEDs 0-63, level 1 are LEDs 64-127, and so on.
// The column counts left to right 0-7 and the row is back to front 0-7. This
// means that if you had level 0, row 0, the bottom back row would count from
// 0-7,
// Eg. If you look down on the cube, and only looked at the bottom level
// 00 01 02 03 04 05 06 07
// 08 09 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
// Then, if you incremented the level, the top right of the grid above would
// start at 64. The reason for doing this, is so you don't have to memorize a
// number for each LED, allowing you to use level, row, column.
// Now, what about the divide by 8 in there?
// ...well, we have 8 bits per byte, and we have 64 bytes in memory for all
// 512 bits needed for each LED, so we divide the number we just found by 8,
// and take the integ7er of it, so we know which byte, that bit is located
// confused? that's ok, let's take an example, if we wanted to write to the
// LED to the last LED in the cube, we would write a 7, 7, 7 giving
// (7 * 64) + (7 * 8) = 7 = 511, which is right, but now let's divide it by 8,
// 511 / 8 = 63.875, and take the int of it so, we get 63, this is the last
// byte in the array, which is right since this is the last LED.
// This next variable is the same thing as before, but here we don't divide
// by 8, so we get the LED number 0-511
int wholebyte=(level * (CUBE_SIZE * CUBE_SIZE)) + (row * CUBE_SIZE) + column;
//This is 4 bit color resolution, so each color contains x4 64 byte arrays,
// explanation below:
bitWrite(red0[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(red, 0));
bitWrite(red1[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(red, 1));
bitWrite(red2[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(red, 2));
bitWrite(red3[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(red, 3));
bitWrite(green0[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(green, 0));
bitWrite(green1[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(green, 1));
bitWrite(green2[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(green, 2));
bitWrite(green3[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(green, 3));
bitWrite(blue0[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(blue, 0));
bitWrite(blue1[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(blue, 1));
bitWrite(blue2[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(blue, 2));
bitWrite(blue3[whichbyte], wholebyte - (CUBE_SIZE * whichbyte), bitRead(blue, 3));
// Are you now more confused? You shouldn't be! It's starting to make sense
// now. Notice how each line is a bitWrite, which is, bitWrite(the byte you
// want to write to, the bit of the byte to write, and the 0 or 1 you want to
// write). This means that the 'whichbyte' is the byte from 0-63 in which the
// bit corresponding to the LED from 0-511. Is making sense now why we did
// that? taking a value from 0-511 and converting it to a value from 0-63,
// since each LED represents a bit in an array of 64 bytes.
// Then next line is which bit 'wholebyte-(8*whichbyte)'. This is simply
// taking the LED's value of 0-511 and subracting it from the BYTE its bit
// was located in times 8. Think about it, byte 63 will contain LEDs from
// 504 to 511, so if you took 505-(8*63), you get a 1, meaning that, LED
// number 505 is is located in bit 1 of byte 63 in the array
// is that it? No, you still have to do the bitRead of the brightness 0-15
// you are trying to write, if you wrote a 15 to RED, all 4 arrays for that
// LED would have a 1 for that bit, meaning it will be on 100%. This is why
// the four arrays read 0-4 of the value entered in for RED, GREEN, and BLUE
// hopefully this all makes some sense?
}
ISR(TIMER1_COMPA_vect) {
// This routine is called in the background automatically at frequency set by
// OCR1A. In this code, I set OCR1A to 30, so this is called every 124us,
// giving each level in the cube 124us of ON time. There are 8 levels, so we
// have a maximum brightness of 1/8, since the level must turn off before the
// next level is turned on. The frequency of the multiplexing is then
// 124us*8=992us, or 1/992us= about 1kHz
PORTE |= 1<<blank_pin; // The first thing we do is turn all of the LEDs OFF,
// by writing a 1 to the blank pin
//
// Note, in my bread-boarded version, I was able to move this way down in the
// cube, meaning that the OFF time was minimized due to signal integrity and
// parasitic capcitance, my rise/fall times, required all of the LEDs to first
// turn off, before updating otherwise you get a ghosting effect on the
// previous level
// This is 4 bit 'Bit angle Modulation' or BAM, There are 8 levels, so when a
// '1' is written to the color brightness, each level will have a chance to
// light up for 1 cycle, the BAM bit keeps track of which bit we are
// modulating out of the 4 bits. Bam counter is the cycle count, meaning as we
// light up each level, we increment the BAM_Counter
if (BAM_Counter==8) BAM_Bit++;
else if (BAM_Counter==24) BAM_Bit++;
else if (BAM_Counter==56) BAM_Bit++;
BAM_Counter++; // Increment the BAM counter
// The BAM bit will be a value from 0-3, and only shift out the arrays
// corresponding to that bit, 0-3. Here's how this works, each case is the bit
// in the Bit angle modulation from 0-4. Next, it depends on which level we're
// on, so the byte in the array to be written depends on which level, but
// since each level contains 64 LED, we only shift out 8 bytes for each color
switch (BAM_Bit) {
case 0:
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(red0[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(green0[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(blue0[shift_out]);
}
break;
case 1:
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(red1[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(green1[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(blue1[shift_out]);
}
break;
case 2:
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(red2[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(green2[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(blue2[shift_out]);
}
break;
case 3:
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(red3[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(green3[shift_out]);
}
for (shift_out=level; shift_out<level+8; shift_out++) {
SPI.transfer(blue3[shift_out]);
}
// Here is where the BAM_Counter is reset back to 0, it's only 4 bit, but
// since each cycle takes 8 counts, it goes 0 8 16 32, and when
// BAM_counter hits 64 we reset the BAM
if (BAM_Counter==120) {
BAM_Counter=0;
BAM_Bit=0;
}
break;
}
//SPI.transfer(anode[anodeLevel]); // Send out the anode level byte
// ** This routine selects layer without shift register.
lastAnode = (anodeLevel-1);
if (anodeLevel == 0) { lastAnode = 7; } // if at bottom, last layer was top
digitalWrite(layerArray[lastAnode], HIGH); // turn off the previous layer
digitalWrite(layerArray[anodeLevel], LOW); // turn on the current layer
PORTE |= 1<<latch_pin; // Latch pin HIGH
PORTE &= ~(1<<latch_pin); // Latch pin LOW
PORTE &= ~(1<<blank_pin); // Blank pin LOW to turn on the LEDs with the new
// data. Blank is the same as the OE or ENABLE pin
anodeLevel++; // inrement the anode level
level = level + 8; // increment the level variable by 8, which is used to
// shift out data, since the next level woudl be the next
// 8 bytes in the arrays
if (anodeLevel==8) anodeLevel=0; // go back to 0 if max is reached
if (level==64) level=0; // if you hit 64 on level, this means you just
// sent out all 63 bytes, so go back
pinMode(blank_pin, OUTPUT); // moved down here so outputs are all off until
// the first call of this function
}
// ANIMATIONS ==================================================================
// =============================================================================
// =============================================================================
void fireworks (int iterations, int n, int delayx) {
clean;
int i, f, e, x;
float origin_x = 3;
float origin_y = 3;
float origin_z = 3;
int rand_y, rand_x, rand_z;
float slowrate, gravity;
// Particles and their position, x,y,z and their movement, dx, dy, dz
float particles[n][6];
float lastpart[n][3];
for (i=0; i < iterations; i++) {
origin_x = rand() % 4;
origin_y = rand() % 4;
origin_z = rand() % 2;
origin_z +=5;
origin_x +=2;
origin_y +=2;
// shoot a particle up in the air
for (e=0; e < origin_z; e++) {
setLED(e, origin_x, origin_y, (random(16)), (random(16)), (random(16)));
x = (50 * e);
delay(30);
clean();
}
// Fill particle array
for (f=0; f < n; f++) {
// Position
particles[f][0] = origin_x;
particles[f][1] = origin_y;
particles[f][2] = origin_z;
rand_x = rand() % 200;
rand_y = rand() % 200;
rand_z = rand() % 200;
// Movement
particles[f][3] = 1 - (float)rand_x / 100; // dx
particles[f][4] = 1 - (float)rand_y / 100; // dy
particles[f][5] = 1 - (float)rand_z / 100; // dz
}
// explode
for (e=0; e < 25; e++) {
slowrate = 1 + tan((e + 0.1) / 20) * 10;
gravity = tan((e + 0.1) / 20) / 2;
for (f=0; f < n; f++) {
particles[f][0] += particles[f][3] / slowrate;
particles[f][1] += particles[f][4] / slowrate;
particles[f][2] += particles[f][5] / slowrate;
particles[f][2] -= gravity;
setLED(particles[f][2],particles[f][0],particles[f][1],(random(16)),(random(16)),(random(16)));
lastpart[f][2]=particles[f][2];
lastpart[f][0]=particles[f][0];
lastpart[f][1]=particles[f][1];
}
delay(40);
for (f=0; f < n; f++) {
setLED(lastpart[f][2], lastpart[f][0], lastpart[f][1], 0, 0, 0);
}
}
}
}
void rain(int duration) {
int x[64], y[64], z[64];
int xx[64], yy[64], zz[64];
int xold[64], yold[64], zold[64];
int addr, colowheel, slowdown;
int leds = 64;
int bright = 1;
for (addr=0; addr < 64; addr++) {
x[addr] = random(16);
y[addr] = random(16);
z[addr] = random(16);
xx[addr] = random(16);
yy[addr] = random(16);
zz[addr] = random(16);
}
start = millis();
while (millis() - start < duration) {
for (addr=0; addr < leds; addr++) {
setLED(zold[addr], xold[addr], yold[addr], 0, 0, 0);
if (z[addr] >= 7) setLED(z[addr], x[addr], y[addr], 0, 5, 15);
if (z[addr] == 6) setLED(z[addr], x[addr], y[addr], 0, 1, 9);
if (z[addr] == 5) setLED(z[addr], x[addr], y[addr], 0, 0, 10);
if (z[addr] == 4) setLED(z[addr], x[addr], y[addr], 1, 0, 11);
if (z[addr] == 3) setLED(z[addr], x[addr], y[addr], 3, 0, 12);
if (z[addr] == 2) setLED(z[addr], x[addr], y[addr], 10, 0, 15);
if (z[addr] == 1) setLED(z[addr], x[addr], y[addr], 10, 0, 10);
if (z[addr] <= 0) setLED(z[addr], x[addr], y[addr], 10, 0, 1);
}
for (addr=0; addr < leds; addr++) {
xold[addr] = x[addr];
yold[addr] = y[addr];
zold[addr] = z[addr];
}
for (addr=0; addr < leds; addr++) {
//slowdown = random(2);
//if (bitRead(z[addr],0))
z[addr] = z[addr]-1;
// x[addr] = x[addr]+1;
// y[addr] = y[addr]+1;
if (z[addr] < random(-100, 0)) {
x[addr] = random(8);
y[addr] = random(8);
int select = random(3);
if (select == 0) {
xx[addr] = 0;
zz[addr] = random(16);
yy[addr] = random(16);
//zz[addr] = 0;
}
if (select == 1) {
xx[addr] = random(16);
zz[addr] = 0;
yy[addr] = random(16);
//yy[addr] = 0;
}
if (select == 2) {
xx[addr] = random(16);
zz[addr] = random(16);
yy[addr] = 0;
}
z[addr] = 7;
}
}
}
}
void folder(int duration, float speedMultiplier = 1.0) {
int xx, yy, zz;
int pullback[16], state = 0, backorfront = 7; // backorfront 7 for back 0 for front
int folderaddr[16], LED_Old[16], oldpullback[16];
int ranx = random(16), rany = random(16), ranz = random(16),
ranselect;
int bot = 0, top = 1, right = 0, left = 0, back = 0, front = 0, side = 0,
side_select;
folderaddr[0] = -7;
folderaddr[1] = -6;
folderaddr[2] = -5;
folderaddr[3] = -4;
folderaddr[4] = -3;
folderaddr[5] = -2;
folderaddr[6] = -1;
folderaddr[7] = 0;
for (xx=0; xx < 8; xx++) {
oldpullback[xx] = 0;
pullback[xx] = 0;
}
start = millis();
while (millis() - start < duration) {
if (top == 1) {
if (side == 0) {
//top to left-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(7 - LED_Old[yy], yy - oldpullback[yy], xx, 0, 0, 0);
setLED(7 - folderaddr[yy], yy - pullback[yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 2) {
//top to back-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(7 - LED_Old[yy], xx, yy - oldpullback[yy], 0, 0, 0);
setLED(7 - folderaddr[yy], xx, yy - pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 3) {
//top-side to front-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(7 - LED_Old[7-yy], xx, yy + oldpullback[yy], 0, 0, 0);
setLED(7 - folderaddr[7-yy], xx, yy + pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 1) {
//top-side to right
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(7 - LED_Old[7-yy], yy + oldpullback[yy], xx, 0, 0, 0);
setLED(7 - folderaddr[7-yy], yy + pullback[yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
}
if (right == 1) {
if (side == 4) {
//right-side to top
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy + oldpullback[7-yy], 7 - LED_Old[7-yy], xx, 0, 0, 0);
setLED(yy + pullback[7-yy], 7 - folderaddr[7-yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 3) {
//right-side to front-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, 7 - LED_Old[7-yy], yy + oldpullback[yy], 0, 0, 0);
setLED(xx, 7 - folderaddr[7-yy], yy + pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 2) {
//right-side to back-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, 7 - LED_Old[yy], yy - oldpullback[yy], 0, 0, 0);
setLED(xx, 7 - folderaddr[yy], yy - pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 5) {
//right-side to bottom
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy - oldpullback[yy], 7 - LED_Old[yy], xx, 0, 0, 0);
setLED(yy - pullback[yy], 7 - folderaddr[yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
}
if (left == 1) {
if (side == 4) {
//left-side to top
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy + oldpullback[yy], LED_Old[7-yy], xx, 0, 0, 0);
setLED(yy + pullback[yy], folderaddr[7-yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 3) {
//left-side to front-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, LED_Old[7-yy], yy + oldpullback[yy], 0, 0, 0);
setLED(xx, folderaddr[7-yy], yy + pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 2) {
//left-side to back-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, LED_Old[yy], yy - oldpullback[yy], 0, 0, 0);
setLED(xx, folderaddr[yy], yy - pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 5) {
//left-side to bottom
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy - oldpullback[yy], LED_Old[yy], xx, 0, 0, 0);
setLED(yy - pullback[yy], folderaddr[yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
}
if (back == 1) {
if (side == 1) {
//back-side to right-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, yy + oldpullback[yy], LED_Old[7-yy], 0, 0, 0);
setLED(xx, yy + pullback[yy], folderaddr[7-yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 4) {
// back-side to top-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy + oldpullback[yy], xx, LED_Old[7-yy], 0, 0, 0);
setLED(yy + pullback[yy], xx, folderaddr[7-yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 5) {
// back-side to bottom
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy - oldpullback[yy], xx, LED_Old[yy], 0, 0, 0);
setLED(yy - pullback[yy], xx, folderaddr[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 0) {
//back-side to left-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, yy - oldpullback[yy], LED_Old[yy], 0, 0, 0);
setLED(xx, yy - pullback[yy], folderaddr[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
}
if (bot == 1) {
if (side == 1) {
// bottom-side to right-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(LED_Old[7-yy], yy + oldpullback[yy], xx, 0, 0, 0);
setLED(folderaddr[7-yy], yy + pullback[yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 3) {
//bottom to front-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(LED_Old[7-yy], xx, yy + oldpullback[yy], 0, 0, 0);
setLED(folderaddr[7-yy], xx, yy + pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 2) {
//bottom to back-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(LED_Old[yy], xx, yy - oldpullback[yy], 0, 0, 0);
setLED(folderaddr[yy], xx, yy - pullback[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 0) {
//bottom to left-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(LED_Old[yy], yy - oldpullback[yy], xx, 0, 0, 0);
setLED(folderaddr[yy], yy - pullback[yy], xx, ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
}
if (front == 1) {
if (side == 0) {
//front-side to left-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, yy - oldpullback[yy], 7 - LED_Old[yy], 0, 0, 0);
setLED(xx, yy - pullback[yy], 7 - folderaddr[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 5) {
// front-side to bottom
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy - oldpullback[yy], xx, 7 - LED_Old[yy], 0, 0, 0);
setLED(yy - pullback[yy], xx, 7 - folderaddr[yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 4) {
// front-side to top-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(yy + oldpullback[yy], xx, 7 - LED_Old[7-yy], 0, 0, 0);
setLED(yy + pullback[yy], xx, 7 - folderaddr[7-yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
if (side == 1) {
//front-side to right-side
for (yy=0; yy < 8; yy++) {
for (xx=0; xx < 8; xx++) {
setLED(xx, yy + oldpullback[yy], 7 - LED_Old[7-yy], 0, 0, 0);
setLED(xx, yy + pullback[yy], 7 - folderaddr[7-yy], ranx, rany, ranz);
}
}
delay(getBeatDivisionPerLayer(speedMultiplier));
}
}
for (xx=0; xx < 8; xx++) {
LED_Old[xx] = folderaddr[xx];
oldpullback[xx] = pullback[xx];
}
if (folderaddr[7] == 7) {
for (zz=0; zz < 8; zz++) {
pullback[zz] = pullback[zz]+1;
}
if (pullback[7] == 8) {
// delay(getBeatDivisionPerLayer(speedMultiplier) * 30);
ranselect = random(3);
if (ranselect == 0) {
ranx = 0;
rany = random(1, 16);
ranz = random(1, 16);
}
if (ranselect == 1) {
ranx = random(1, 16);
rany = 0;
ranz = random(1, 16);
}
if (ranselect == 2) {
ranx = random(1, 16);
rany = random(1, 16);
ranz = 0;
}
side_select = random(3);
if (top == 1) { // TOP
top = 0;
if (side == 0) { // top to left
left = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 3;
if (side_select == 2) side = 5;
} else if (side == 1) { // top to right
right = 1;
if (side_select == 0) side = 5;
if (side_select == 1) side = 2;
if (side_select == 2) side = 3;
} else if (side == 2) { // top to back
back = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 1;
if (side_select == 2) side = 5;
} else if (side == 3) { // top to front
front = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 1;
if (side_select == 2) side = 5;
}
} else if (bot == 1) { // BOTTOM
bot = 0;
if (side == 0) { // bot to left
left = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 3;
if (side_select == 2) side = 4;
} else if (side == 1) { // bot to right
right = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 3;
if (side_select == 2) side = 4;
} else if (side == 2) { // bot to back
back = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 1;
if (side_select == 2) side = 4;
} else if (side == 3) { // bot to front
front = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 1;
if (side_select == 2) side = 4;
}
} else if (right == 1) { // RIGHT
right = 0;
if (side == 4) { // right to top
top = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 3;
if (side_select == 2) side = 0;
} else if (side == 5) { // right to bot
bot = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 2;
if (side_select == 2) side = 3;
} else if (side == 2) { // right to back
back = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
} else if (side == 3) { // right to front
front = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
}
} else if (left == 1) { // LEFT
left = 0;
if (side == 4) { // left to top
top = 1;
if (side_select == 0) side = 3;
if (side_select == 1) side = 2;
if (side_select == 2) side = 1;
} else if (side == 5) { // left to bot
bot = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 3;
if (side_select == 2) side = 1;
} else if (side == 2) { // left to back
back = 1;
if (side_select == 0) side = 1;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
} else if (side == 3) { // left to front
front = 1;
if (side_select == 0) side = 1;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
}
} else if (front == 1) { // FRONT
front = 0;
if (side == 4) { // front to top
top=1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 0;
if (side_select == 2) side = 1;
} else if (side == 5) { // front to bot
bot = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 2;
if (side_select == 2) side = 1;
} else if (side == 0) { // front to left
left = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
} else if (side == 1) { // front to right
right = 1;
if (side_select == 0) side = 2;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
}
} else if (back == 1) { // BACK
back = 0;
if (side == 4) { // back to top
top = 1;
if (side_select == 0) side = 3;
if (side_select == 1) side = 0;
if (side_select == 2) side = 1;
} else if (side==5) { // back to bot
bot = 1;
if (side_select == 0) side = 0;
if (side_select == 1) side = 3;
if (side_select == 2) side = 1;
} else if (side ==0) { // back to left
left = 1;
if (side_select == 0) side = 3;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
} else if (side==1) { // back to right
right = 1;
if (side_select == 0) side = 3;
if (side_select == 1) side = 5;
if (side_select == 2) side = 4;
}
}
for (xx=0; xx < 8; xx++) {
oldpullback[xx] = 0;
pullback[xx] = 0;
}
folderaddr[0] = -8;
folderaddr[1] = -7;
folderaddr[2] = -6;
folderaddr[3] = -5;
folderaddr[4] = -4;
folderaddr[5] = -3;
folderaddr[6] = -2;
folderaddr[7] = -1;
}
}
if (folderaddr[7] != 7) {
for (zz=0; zz < 8; zz++) {