-
Notifications
You must be signed in to change notification settings - Fork 2
/
eztoomuchrandombig2.ino
1386 lines (1179 loc) · 38.1 KB
/
eztoomuchrandombig2.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
//This compiles and runs on Particle Build
// Updated 2.11.2015 EZU for const correctness and reference parameter passing.
// Avoids memory allocation and deallocation each time an object is passed as a parameter - performance gain.
#include <math.h>
#include "application.h"
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(AUTOMATIC); //don't connect to the internet on boot
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D0
#define totalPIXEL 4096
#define stripPIXEL totalPIXEL/8 //THERE IS FOUR PINS TO DRIVE EACH STRIPS
#define PIXEL_TYPE WS2812B
#define SIZE 16
#define PI 3.14159
#define DEMO_ROUTINES 1
#define FIREWORKS 7
#define FASTPLASMA 9
#define ROMAN_CANDLE 2
#define FFT_JOY 3
#define CIRCLES 4
#define PLASMA 5
#define DISPLAY_TEST 6
#define BLINK 8
#define TMR 0
/**********************************
* flip variables *
* ********************************/
//accelerometer pinout
#define X A0
#define Y A1
#define Z A6
#define NUM_SAMPLES 100
int accelerometer[3];
unsigned long totals[3];
int runningAverage[3];
boolean whack[3];
boolean whacked=false;
#define WHACK_X 20
#define WHACK_Y 20
#define WHACK_Z 20
bool autoCycle=true; //start on autocycle by default
/*******************************
* fade variables *
* ****************************/
bool fading=false;
int fadeValue=255;
int fadeSpeed=2;
/* datatype definitions
*/
/** An RGB color. */
struct Color
{
unsigned char red, green, blue;
Color(int r, int g, int b) : red(r), green(g), blue(b) {}
Color() : red(0), green(0), blue(0) {}
};
/********************************
* zplasma variables *
* *****************************/
float phase = 0.0;
long fastPhase=0;
float phaseIncrement = 0.035; // Controls the speed of the moving points. Higher == faster
int fastPhaseIncrement = 1; // Controls the speed of the moving points. Higher == faster
int fastColorStretch = 4; // will divide rather than multiply
float colorStretch = 0.23; // Higher numbers will produce tighter color bands
float plasmaBrightness = 0.2;
Color plasmaColor;
/** A point in 3D space. */
struct Point
{
float x;
float y;
float z;
Point() : x(0), y(0), z(0) {}
Point(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
};
/** An integer point in 3D space. */
struct Point_i
{
int x;
int y;
int z;
Point_i() : x(0), y(0), z(0) {}
Point_i(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
};
struct Rocket
{
float x,y,z;
float xVel, yVel, zVel;
float gravity;
Color col;
Rocket():x((SIZE-1)/2), y(0), z((SIZE-1)/2), col(Color(255,0,0)){}
};
#define NUM_ROCKETS 50
Rocket rockets[NUM_ROCKETS];
float offset=0;
int y=0;
int yinc=1;
int maxBrightness=50;
int demo=FIREWORKS;
#define MICROPHONE A7
#define GAIN_CONTROL D5
/*********************************
* FFTJoy variables *
* *******************************/
#define M 5
float real[(int)pow(2,M)];
float imaginary[(int)pow(2,M)];
float maxValue=0;
float sample;
/******************************
* fireworks variables *
* ****************************/
int centerX, centerY, centerZ;
int launchX, launchZ;
int red, green, blue;
float radius=0;
float speed;
bool showRocket;
bool exploded;
bool dead;
float xInc, yInc, zInc;
float rocketX, rocketY, rocketZ;
float launchTime;
int maxSize;
int fireworkRes;
Color rocketColor, fireworkColor;
uint8_t PIXEL_RGB[totalPIXEL*3]; //EACH PIXELS HAS 3 BYTES DATA
// function prototypes
int displayTestIteration=0;
void displayTest();
void initFireworks();
void initCircles();
void initRockets();
void initCloudButton();
void initAccelerometer();
void setVoxel(int x, int y, int z, const Color& col);
void setVoxel(const Point& p, const Color& col);
Color getVoxel(int x, int y, int z);
Color getVoxel(const Point& p);
void line(const Point& p1, const Point& p2, const Color& col);
void line(int x1, int y1, int z1, int x2, int y2, int z2, const Color& col);
void sphere(const Point& center, float radius, const Color& col);
void sphere(const Point& center, float radius, const Color& col, int res);
void sphere(float x, float y, float z, float radius, const Color& col);
void sphere(float x, float y, float z, float radius, const Color& col, int res);
void background(const Color& col);
Color colorMap(float val, float minVal, float maxVal);
Color lerpColor(const Color& a, const Color& b, int val, int minVal, int maxVal);
Color color = Color(random(256), random(256), random(256));
Point add(const Point& a, const Point& b);
void fft_joy();
short FFT(short int dir,int m,float *x,float *y);
void fade();
void mirror();
void updateFireworks();
void circles();
void show();
void setFadeSpeed();
void prepRocket();
float distance(float x, float y, float z, float x1, float y1, float z1);
int frame;
Point poly1[4];
float poly1Angles[4];
float poly1AnglesInc[4];
Point poly2[4];
float poly2Angles[4];
float poly2AnglesInc[4];
Point poly3[4];
float poly3Angles[4];
float poly3AnglesInc[4];
float poly1Color, poly2Color, poly3Color;
float poly1ColorInc=0.001, poly2ColorInc=0.0005, poly3ColorInc=0.0007;
Point center;
unsigned int lastDemo=0;
#define DEMO_TIME 30000
int timeout=0;
bool onlinePressed=false;
bool lastOnline=true;
#define BUTTON D6 //press this button to connect to the internet
#define MODE D4
void setup()
{
randomSeed(analogRead(A0));
Serial.begin(115200);
pinMode(GAIN_CONTROL, OUTPUT);
digitalWrite(GAIN_CONTROL, LOW);
initFireworks();
initCircles();
initRockets();
initCloudButton();
initAccelerometer();
pinMode(D0,OUTPUT); //PB7
pinMode(D1,OUTPUT); //PB6
pinMode(D2,OUTPUT); //BP5
pinMode(D3,OUTPUT); //PB4
pinMode(A2,OUTPUT); //PA4
pinMode(A3,OUTPUT); //PA5
pinMode(A4,OUTPUT); //BA6
pinMode(A5,OUTPUT); //PA7
pinMode(D7, OUTPUT);
digitalWrite(D7, HIGH);
}
//initializes the running average values for the accelerometer
//I just set them to the first reading of the accelerometer on boot -- this is an imperfect method,
//but it gets the rolling average very close it its eventual value
//I chose to base it off of each cube's individual ADC reading, rather than hardcode the values from my sample cube
void initAccelerometer()
{
runningAverage[0]=analogRead(X);
runningAverage[1]=analogRead(Y);
runningAverage[2]=analogRead(Z);
}
//sets up the online/offline switch
void initCloudButton()
{
//set the input mode for the 'connect to cloud' button
pinMode(BUTTON, INPUT_PULLUP);
pinMode(MODE, INPUT_PULLUP);
if(!digitalRead(MODE)) //if the wifi button is held down on boot, do a hard reset. At any other time, keep the firmware, but try to add new wifi creds
{
WiFi.on();
WiFi.clearCredentials();
System.factoryReset();
}
//a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline'
onlinePressed=!digitalRead(BUTTON);
if(onlinePressed)
WiFi.connect();
}
//checks to see if the 'online/offline' switch is switched
void checkCloudButton()
{
//if the 'connect to cloud' button is pressed, try to connect to wifi.
//otherwise, run the program
//note -- how does this behave when there are no wifi credentials loaded on the spark?
//onlinePressed is HIGH when the switch is _not_ connected and LOW when the switch is connected
//a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline'
onlinePressed=!digitalRead(BUTTON);
if((onlinePressed)&&(!lastOnline)) //marked as 'online'
{
lastOnline=onlinePressed;
WiFi.connect();
}
else if((!onlinePressed)&&(lastOnline)) //marked as 'offline'
{
lastOnline=onlinePressed;
WiFi.disconnect();
}
lastOnline=onlinePressed;
if(!digitalRead(MODE))
WiFi.listen();
}
/********************************
* zplasma functions *
* *****************************/
void zPlasma()
{
phase += phaseIncrement;
// The two points move along Lissajious curves, see: http://en.wikipedia.org/wiki/Lissajous_curve
// We want values that fit the LED grid: x values between 0..15, y values between 0..15, z values between 0..15
// The sin() function returns values in the range of -1.0..1.0, so scale these to our desired ranges.
// The phase value is multiplied by various constants; I chose these semi-randomly, to produce a nice motion.
Point p1 = { (sin(phase*1.000)+1.0) * 8.0, (sin(phase*1.310)+1.0) * 8.0, (sin(phase*1.380)+1.0) * 8.0};
Point p2 = { (sin(phase*1.770)+1.0) * 8.0, (sin(phase*2.865)+1.0) * 8.0, (sin(phase*1.410)+1.0) * 8.0};
Point p3 = { (sin(phase*0.250)+1.0) * 8.0, (sin(phase*0.750)+1.0) * 8.0, (sin(phase*0.380)+1.0) * 8.0};
byte row, col, dep;
// For each row
for(row=0; row<16; row++) {
float row_f = float(row); // Optimization: Keep a floating point value of the row number, instead of recasting it repeatedly.
// For each column
for(col=0; col<16; col++) {
float col_f = float(col); // Optimization.
// For each depth
for(dep=0; dep<16; dep++) {
float dep_f = float(dep); // Optimization.
// Calculate the distance between this LED, and p1.
Point dist1 = { col_f - p1.x, row_f - p1.y, dep_f - p1.z }; // The vector from p1 to this LED.
float distance1 = sqrt( dist1.x*dist1.x + dist1.y*dist1.y + dist1.z*dist1.z);
// Calculate the distance between this LED, and p2.
Point dist2 = { col_f - p2.x, row_f - p2.y, dep_f - p2.z}; // The vector from p2 to this LED.
float distance2 = sqrt( dist2.x*dist2.x + dist2.y*dist2.y + dist2.z*dist2.z);
// Calculate the distance between this LED, and p3.
Point dist3 = { col_f - p3.x, row_f - p3.y, dep_f - p3.z}; // The vector from p3 to this LED.
float distance3 = sqrt( dist3.x*dist3.x + dist3.y*dist3.y + dist3.z*dist3.z);
// Warp the distance with a sin() function. As the distance value increases, the LEDs will get light,dark,light,dark,etc...
// You can use a cos() for slightly different shading, or experiment with other functions.
float color_1 = distance1; // range: 0.0...1.0
float color_2 = distance2;
float color_3 = distance3;
float color_4 = (sin( distance1 * distance2 * colorStretch )) + 2.0 * 0.5;
// Square the color_f value to weight it towards 0. The image will be darker and have higher contrast.
color_1 *= color_1 * color_4;
color_2 *= color_2 * color_4;
color_3 *= color_3 * color_4;
color_4 *= color_4;
// Scale the color up to 0..7 . Max brightness is 7.
//strip.setPixelColor(col + (8 * row), strip.Color(color_4, 0, 0) );
plasmaColor.red=color_1*plasmaBrightness;
plasmaColor.green=color_2*plasmaBrightness;
plasmaColor.blue=color_3*plasmaBrightness;
setVoxel(row,col,dep,plasmaColor);
}
}
}
}
// all inputs need to be multiplied by 572
inline int ifsin( int x )
{
// convert float to integer (32768 units times 2*pi / 360 degrees )
//int x= angle * 572;
static const int qN = 13, qA= 12, qP= 15, qR= 2*qN-qP, qS= qN+qP+1-qA;
//x *= 166886; // -- conversion to radians
x= x<<(30-qN); // 32768 steps per circle (360degrees, 2pi radians)
// shift to full s32 range (Q13->Q30)
if( (x^(x<<1)) < 0) // test for quadrant 1 or 2
x= (1<<31) - x;
x= x>>(30-qN);
// convert result to -1024..+1024
return (x * ( (3<<qP) - (x*x>>qR) ) >> qS) >> 2;
}
/********************************
* fastPlasma functions *
* *****************************/
inline unsigned int isqrt(unsigned long a) {
unsigned int rem = 0;
int root = 0;
int i;
for (i = 0; i < 16; i++) {
root <<= 1;
rem <<= 2;
rem += a >> 30;
a <<= 2;
if (root < rem) {
root++;
rem -= root;
root++;
}
}
return (unsigned short) (root >> 1);
}
void fastPlasma()
{
fastPhase += fastPhaseIncrement;
// The two points move along Lissajious curves, see: http://en.wikipedia.org/wiki/Lissajous_curve
// We want values that fit the LED grid: x values between 0..15, y values between 0..15, z values between 0..15
// The sin() function returns values in the range of -1.0..1.0, so scale these to our desired ranges.
// The phase value is multiplied by various constants; I chose these semi-randomly, to produce a nice motion.
Point_i p1 = { (ifsin(fastPhase*572)+1024)/128, (ifsin(fastPhase*749)+1024) /128, (ifsin(fastPhase*789)+1024) /128};
Point_i p2 = { (ifsin(fastPhase*1144)+1024)/128, (ifsin(fastPhase*1639)+1024) /128, (ifsin(fastPhase*807)+1024) /128};
Point_i p3 = { (ifsin(fastPhase*143)+1024)/128, (ifsin(fastPhase*429)+1024) /128, (ifsin(fastPhase*217)+1024) /128};
byte row, col, dep;
for(row=0; row<16; row++) {
for(col=0; col<16; col++) {
for(dep=0; dep<16; dep++) {
// Calculate the distance between this LED, and p1.
Point_i dist1 = { col - p1.x, row - p1.y, dep - p1.z }; // The vector from p1 to this LED.
int distance1 = isqrt( dist1.x*dist1.x + dist1.y*dist1.y + dist1.z*dist1.z);
// Calculate the distance between this LED, and p2.
Point_i dist2 = { col - p2.x, row - p2.y, dep - p2.z}; // The vector from p2 to this LED.
int distance2 = isqrt( dist2.x*dist2.x + dist2.y*dist2.y + dist2.z*dist2.z);
// Calculate the distance between this LED, and p3.
Point_i dist3 = { col - p3.x, row - p3.y, dep - p3.z}; // The vector from p3 to this LED.
int distance3 = isqrt( dist3.x*dist3.x + dist3.y*dist3.y + dist3.z*dist3.z);
// Warp the distance with a sin() function. As the distance value increases, the LEDs will get light,dark,light,dark,etc...
// You can use a cos() for slightly different shading, or experiment with other functions.
int color_1 = distance1; // range: 0...16
int color_2 = distance2;
int color_3 = distance3;
int color_4 = (ifsin( distance1 * distance2 * 143 ) + 1024) /128;
// Square the color_f value to weigh it towards 0. The image will be darker and have higher contrast.
color_1 *= color_1 * color_4;
color_2 *= color_2 * color_4;
color_3 *= color_3 * color_4;
// Scale the color up to 0..7 . Max brightness is 7.
//strip.setPixelColor(col + (8 * row), strip.Color(color_4, 0, 0) );
plasmaColor.red=color_1/290;
plasmaColor.green=color_2/290;
plasmaColor.blue=color_3/290;
setVoxel(row,col,dep,plasmaColor);
}
}
}
}
void displayTest()
{
displayTestIteration++;
Color testColor;
testColor.red=displayTestIteration%7;
testColor.green=(displayTestIteration+2)%7;
testColor.blue=(displayTestIteration+4)%7;
for(int x=0; x<16; x++)
for(int y=0; y<16; y++)
for(int z=0; z<16; z++)
setVoxel(x,y,z,testColor);
delay(50);
}
void updateAccelerometer()
{
accelerometer[0]=analogRead(X);
accelerometer[1]=analogRead(Y);
accelerometer[2]=analogRead(Z);
for(int i=0;i<3;i++)
{
totals[i]+=accelerometer[i];
//sweet running average algorithm: average[i+1]=average[i]+(sample[i]-average[i])/NUM_SAMPLES
//I average over 100 samples, or ~2.5 seconds
runningAverage[i]=runningAverage[i]+((accelerometer[i]-runningAverage[i])/NUM_SAMPLES);
whack[i]=false;
}
if(abs(accelerometer[0]-runningAverage[0])>WHACK_X)
whack[0]=true;
if(abs(accelerometer[1]-runningAverage[1])>WHACK_Y)
whack[1]=true;
if(abs(accelerometer[2]-runningAverage[2])>WHACK_Z)
whack[2]=true;
whacked=whack[0] | whack[1] | whack[2];
}
void initRockets()
{
for(int i=0;i<NUM_ROCKETS;i++)
{
rockets[i].gravity=-0.01;
rockets[i].y=0;
rockets[i].x=center.x;
rockets[i].z=center.z;
rockets[i].col=Color(255,0,0);
rockets[i].xVel=-.5;//(random(10)/10)-0.5;
rockets[i].yVel=0.25;//random(10)/10;
rockets[i].zVel=0.25;//(random(10)/10)-0.5;
}
}
void initCircles()
{
center=Point((SIZE-1)/2,(SIZE-1)/2,(SIZE-1)/2);
for(int i=0;i<4;i++)
{
poly1Angles[i]=random(6.28);
poly2Angles[i]=random(6.28);
poly3Angles[i]=random(6.28);
}
poly1AnglesInc[0]=random(10)/100 - 0.05;
poly1AnglesInc[1]=random(20)/100 - 0.1;
poly1AnglesInc[2]=random(10)/100 - 0.05;
poly1AnglesInc[3]=random(20)/100 - 0.1;
poly2AnglesInc[0]=random(20)/100 - 0.1;
poly2AnglesInc[1]=random(20)/100 - 0.1;
poly2AnglesInc[2]=random(20)/100 - 0.1;
poly2AnglesInc[3]=random(20)/100 - 0.1;
poly3AnglesInc[0]=random(20)/100 - 0.1;
poly3AnglesInc[1]=random(20)/100 - 0.1;
poly3AnglesInc[2]=random(20)/100 - 0.1;
poly3AnglesInc[3]=random(20)/100 - 0.1;
}
void romanCandle()
{
background(Color(0,0,0));
for(int i=0;i<pow(2,M);i++)
{
real[i]=analogRead(MICROPHONE)-993; //adapted for the 0.8v bias point of the big cube
delayMicroseconds(212);
imaginary[i]=0;
}
FFT(1, M, real, imaginary);
for(int i=0;i<pow(2,M);i++)
{
imaginary[i]=sqrt(pow(imaginary[i],2)+pow(real[i],2));
if(imaginary[i]>maxValue)
maxValue=imaginary[i];
}
if(maxValue>100)
maxValue--;
int rocketsToFire=0;
for(int i=0;i<pow(2,M)/2;i++)
{
imaginary[i]=SIZE*imaginary[i]/maxValue;
if(imaginary[i]>SIZE/2)
rocketsToFire++;
}
for(int i=0;i<NUM_ROCKETS;i++)
{
rockets[i].yVel+=rockets[i].gravity;
rockets[i].x+=rockets[i].xVel;
rockets[i].y+=rockets[i].yVel;
rockets[i].z+=rockets[i].zVel;
if(rockets[i].col.green>5)
rockets[i].col.green-=5;
if(rockets[i].col.blue>5)
rockets[i].col.blue-=5;
/*
Serial.print(rockets[i].col.red);
Serial.print(" ");
Serial.print(rockets[i].col.green);
Serial.print(" ");
Serial.println(rockets[i].col.blue);
setVoxel(rockets[i].x,rockets[i].y,rockets[i].z,rockets[i].col);
*/
if(rocketsToFire>0)
if(rockets[i].y<0)
{
rocketsToFire--;
rockets[i].gravity=-0.01;
rockets[i].y=0;
rockets[i].x=center.x;
rockets[i].z=center.z;
rockets[i].col=Color(random(100),random(100),random(100));
rockets[i].xVel=(float)random(10)/10;//((float)random(10)/10)-0.5;
rockets[i].yVel=(float)random(10)/10;
rockets[i].zVel=((float)random(5)/10);
}
}
for(int i=0;i<(NUM_ROCKETS%2==0?NUM_ROCKETS:NUM_ROCKETS-1);i+=2)
line(rockets[i].x*cos(offset), rockets[i].y, rockets[i].z*sin(offset),rockets[i+1].x, rockets[i+1].y, rockets[i+1].z,rockets[i].col);
offset+=0.1;
mirror();
}
void mirror()
{
for(int x=center.x;x<SIZE;x++)
for(int y=0;y<SIZE;y++)
for(int z=center.z;z<SIZE;z++)
{
setVoxel(center.x-(x-center.x),y,z, getVoxel(x,y,z));
setVoxel(center.x-(x-center.x),y,center.z-(z-center.z), getVoxel(x,y,z));
setVoxel(x,y,center.z-(z-center.z), getVoxel(x,y,z));
}
}
void blink()
{
background(Color(0,0,0));
show();
delay(500);
setVoxel(5,5,5,Color(50,50,50));
setVoxel(7,7,7,Color(50,0,0));
setVoxel(0,0,0,Color(20,20,50));
setVoxel(15,15,15,Color(0,0,50));
setVoxel(14,14,14,Color(25,0,25));
setVoxel(1,1,1,Color(50,50,0));
show();
delay(500);
}
void randomColor()
{
color = Color(random(256), random(256), random(256));
//color = Color(255,255,255); uncomment this to make white strobe effect
}
void tmr()
{
background(Color(0,0,0));
randomColor();
for(int i = 0; i < random(10); i++)
{
setVoxel(random(16),random(16),random(16),color);
}
}
void loop()
{
//if the 'connect to cloud' button is pressed, try to connect to wifi.
//otherwise, run the program
checkCloudButton();
if(fading)
fade();
else
{
switch(demo%DEMO_ROUTINES)
{
case(TMR):
tmr();
break;
case(FIREWORKS):
updateFireworks();
break;
case(CIRCLES):
circles();
break;
case(ROMAN_CANDLE):
romanCandle();
break;
case(FFT_JOY):
fft_joy();
break;
case(PLASMA):
zPlasma();
break;
case(FASTPLASMA):
fastPlasma();
break;
case(DISPLAY_TEST):
displayTest();
break;
default:
break;
}
frame++;
}
show();
if(autoCycle)
if(millis()-lastDemo>DEMO_TIME)
{
fading=true;
demo++;
lastDemo=millis();
}
if(fading)
{
fadeValue-=fadeSpeed;
//if we're done fading)
if(fadeValue<=0)
{
fading=false;
fadeValue=255;
}
else
fade();
}
//uncomment to enable whack-to-change
updateAccelerometer();
if((whacked)&&((millis()-timeout)>250))
{
autoCycle=false;
fading=true;
demo++;
timeout=millis();
}
}
void fade()
{
setFadeSpeed();
Color voxelColor;
for(int x=0;x<SIZE;x++)
for(int y=0;y<SIZE;y++)
for(int z=0;z<SIZE;z++)
{
voxelColor=getVoxel(x,y,z);
if(voxelColor.red>0)
voxelColor.red--;
if(voxelColor.green>0)
voxelColor.green--;
if(voxelColor.blue>0)
voxelColor.blue--;
setVoxel(x,y,z, voxelColor);
}
}
/********************************************
* FFT JOY functions
* *****************************************/
void fft_joy(){
for(int i=0;i<pow(2,M);i++)
{
real[i]=analogRead(MICROPHONE)-993; //adapted for the 0.8v bias point of the big cube
delayMicroseconds(212);
imaginary[i]=0;
}
FFT(1, M, real, imaginary);
for(int i=0;i<pow(2,M);i++)
{
imaginary[i]=sqrt(pow(imaginary[i],2)+pow(real[i],2));
if(imaginary[i]>maxValue)
maxValue=imaginary[i];
}
if(maxValue>100)
maxValue--;
for(int i=0;i<pow(2,M)/2;i++)
{
imaginary[i]=SIZE*imaginary[i]/maxValue;
int y;
for(y=0;y<=imaginary[i];y++)
setVoxel(i,y,SIZE-1,colorMap(y,0,SIZE));
for(;y<SIZE;y++)
setVoxel(i,y,SIZE-1,Color(0,0,0));
}
for(int z=0;z<SIZE-1;z++)
for(int x=0;x<SIZE;x++)
for(int y=0;y<SIZE;y++)
{
Color col=getVoxel(x,y,z+1);
setVoxel(x,y,z,col);
}
sample++;
if(sample>=pow(2,M))
sample-=pow(2,M);
}
short FFT(short int dir,int m,float *x,float *y)
{
int n,i,i1,j,k,i2,l,l1,l2;
float c1,c2,tx,ty,t1,t2,u1,u2,z;
/* Calculate the number of points */
n = 1;
for (i=0;i<m;i++)
n *= 2;
/* Do the bit reversal */
i2 = n >> 1;
j = 0;
for (i=0;i<n-1;i++) {
if (i < j) {
tx = x[i];
ty = y[i];
x[i] = x[j];
y[i] = y[j];
x[j] = tx;
y[j] = ty;
}
k = i2;
while (k <= j) {
j -= k;
k >>= 1;
}
j += k;
}
/* Compute the FFT */
c1 = -1.0;
c2 = 0.0;
l2 = 1;
for (l=0;l<m;l++) {
l1 = l2;
l2 <<= 1;
u1 = 1.0;
u2 = 0.0;
for (j=0;j<l1;j++) {
for (i=j;i<n;i+=l2) {
i1 = i + l1;
t1 = u1 * x[i1] - u2 * y[i1];
t2 = u1 * y[i1] + u2 * x[i1];
x[i1] = x[i] - t1;
y[i1] = y[i] - t2;
x[i] += t1;
y[i] += t2;
}
z = u1 * c1 - u2 * c2;
u2 = u1 * c2 + u2 * c1;
u1 = z;
}
c2 = sqrt((1.0 - c1) / 2.0);
if (dir == 1)
c2 = -c2;
c1 = sqrt((1.0 + c1) / 2.0);
}
/* Scaling for forward transform */
if (dir == 1) {
for (i=0;i<n;i++) {
x[i] /= n;
y[i] /= n;
}
}
return(0);
}
void circles()
{
background(Color(0,0,0));
poly1[0]=Point(center.x+(SIZE/2)*cos(poly1Angles[0]),0,center.z+(SIZE/2)*sin(poly1Angles[0]));
poly1[1]=Point(0,center.y+(SIZE/2)*cos(poly1Angles[1]),center.z+(SIZE/2)*sin(poly1Angles[1]));
poly1[2]=Point(center.x+(SIZE/2)*cos(poly1Angles[2]),SIZE-1,center.z+(SIZE/2)*sin(poly1Angles[2]));
poly1[3]=Point(SIZE-1,center.y+(SIZE/2)*cos(poly1Angles[3]),center.z+(SIZE/2)*sin(poly1Angles[3]));
poly2[0]=Point(center.x+(SIZE/2)*cos(poly2Angles[0]),center.y+(SIZE/2)*sin(poly2Angles[0]),0);
poly2[1]=Point(SIZE-1,center.y+(SIZE/2)*cos(poly2Angles[1]),center.z+(SIZE/2)*sin(poly2Angles[1]));
poly2[2]=Point(center.x+(SIZE/2)*cos(poly2Angles[2]),center.y+(SIZE/2)*sin(poly2Angles[2]),SIZE-1);
poly2[3]=Point(0,center.y+(SIZE/2)*cos(poly2Angles[3]),center.z+(SIZE/2)*sin(poly2Angles[3]));
poly3[0]=Point(center.x+(SIZE/2)*cos(poly3Angles[0]),0, center.z+(SIZE/2)*sin(poly3Angles[0]));
poly3[1]=Point(center.x+(SIZE/2)*cos(poly3Angles[1]),center.y+(SIZE/2)*cos(poly3Angles[1]),0);
poly3[2]=Point(center.x+(SIZE/2)*cos(poly3Angles[2]),SIZE-1, center.z+(SIZE/2)*sin(poly3Angles[2]));
poly3[3]=Point(center.x+(SIZE/2)*cos(poly3Angles[3]),center.y+(SIZE/2)*cos(poly3Angles[3]),SIZE-1);
for(int i=0;i<4;i++)
{
poly1Angles[i]+=poly1AnglesInc[i];
poly2Angles[i]+=poly2AnglesInc[i];
poly3Angles[i]+=poly3AnglesInc[i];
float sin1=(float)255*sin(poly1Color);
float sin2=(float)255*sin(poly2Color);
float sin3=(float)255*sin(poly3Color);
line(poly1[i], poly1[(i+1)%4], Color(abs(sin1),0,0));
line(poly2[i], poly2[(i+1)%4], Color(0,0,abs(sin2)));
line(poly3[i], poly3[(i+1)%4], Color(0,abs(sin3),0));
}
poly1Color+=poly1ColorInc;
poly2Color+=poly2ColorInc;
poly3Color+=poly3ColorInc;
}
void background(const Color& col)
{
for(int x=0;x<SIZE;x++)
for(int y=0;y<SIZE;y++)
for(int z=0;z<SIZE;z++)
setVoxel(x,y,z,col);
}
void setVoxel(int x, int y, int z, const Color& col)
{
if((x>=0)&&(x<SIZE))
if((y>=0)&&(y<SIZE))
if((z>=0)&&(z<SIZE))
{
int index=z*256+x*16+y;
PIXEL_RGB[index*3]=col.green;
PIXEL_RGB[index*3+1]=col.red;
PIXEL_RGB[index*3+2]=col.blue;
}
}
void setVoxel(const Point& p, const Color& col)
{
setVoxel(p.x, p.y, p.z, col);
}
void line(const Point& p1, const Point& p2, const Color& col)
{
line(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, col);
}
void line(int x1, int y1, int z1, int x2, int y2, int z2, const Color& col)
{
Point currentPoint;
currentPoint.x=x1;
currentPoint.y=y1;
currentPoint.z=z1;
int dx = x2 - x1;
int dy = y2 - y1;
int dz = z2 - z1;
int x_inc = (dx < 0) ? -1 : 1;
int l = abs(dx);
int y_inc = (dy < 0) ? -1 : 1;
int m = abs(dy);
int z_inc = (dz < 0) ? -1 : 1;
int n = abs(dz);
int dx2 = l << 1;
int dy2 = m << 1;
int dz2 = n << 1;
if((l >= m) && (l >= n)) {
int err_1 = dy2 - l;
int err_2 = dz2 - l;
for(int i = 0; i < l; i++) {
setVoxel(currentPoint, col);
if(err_1 > 0) {
currentPoint.y += y_inc;
err_1 -= dx2;
}
if(err_2 > 0) {
currentPoint.z += z_inc;
err_2 -= dx2;
}
err_1 += dy2;
err_2 += dz2;
currentPoint.x += x_inc;
}
} else if((m >= l) && (m >= n)) {
int err_1 = dx2 - m;
int err_2 = dz2 - m;
for(int i = 0; i < m; i++) {
setVoxel(currentPoint, col);
if(err_1 > 0) {
currentPoint.x += x_inc;
err_1 -= dy2;
}