Close
Sign in to view the code!
Sign in to send to cube!
Roman Candle
Created by alex on September 21, 2015
Twitter
Facebook
very symmetric, sound-reactive display
Comments:
www
said
Awesome!!
Guitarmon8
said
This is pretty cool!
Up next
Tree Animation
Update Demo
Sign up, make your own!
#include <math.h> /********************************* * FFTJoy variables * * *******************************/ #define M 4 float real[(int)pow(2,M)]; float imaginary[(int)pow(2,M)]; float maxValue=0; float sample; short FFT(short int dir,int m,float *x,float *y); Cube cube=Cube(); struct Rocket { float x,y,z; float xVel, yVel, zVel; float gravity; Color col; Rocket():x((cube.size-1)/2), y(0), z((cube.size-1)/2), col(Color(255,0,0)){} }; #define NUM_ROCKETS 50 Rocket rockets[NUM_ROCKETS]; float offset=0; void setup() { cube.begin(); initRockets(); Serial.begin(115200); } void loop() { romanCandle(); cube.show(); } void initRockets() { for(int i=0;i<NUM_ROCKETS;i++) { rockets[i].gravity=-0.01; rockets[i].y=0; rockets[i].x=cube.center.x; rockets[i].z=cube.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; } } 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 romanCandle() { cube.background(Color(0,0,0)); for(int i=0;i<pow(2,M);i++) { real[i]=analogRead(MICROPHONE)-2048; //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]=cube.size*imaginary[i]/maxValue; if(imaginary[i]>cube.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=cube.center.x; rockets[i].z=cube.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) cube.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=cube.center.x;x<cube.size;x++) for(int y=0;y<cube.size;y++) for(int z=cube.center.z;z<cube.size;z++) { cube.setVoxel(cube.center.x-(x-cube.center.x),y,z, cube.getVoxel(x,y,z)); cube.setVoxel(cube.center.x-(x-cube.center.x),y,cube.center.z-(z-cube.center.z), cube.getVoxel(x,y,z)); cube.setVoxel(x,y,cube.center.z-(z-cube.center.z), cube.getVoxel(x,y,z)); } }
Awesome!!
This is pretty cool!