Advertisement
burkibo

Arduino Uno LED Matrix

Jul 24th, 2020 (edited)
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 13.57 KB | None | 0 0
  1. #include <FastLED.h>        //https://github.com/FastLED/FastLED
  2. #include <LEDMatrix.h>      //https://github.com/Jorgen-VikingGod/LEDMatrix
  3.  
  4. #define DATA_PIN            6
  5. #define COLOR_ORDER         RGB
  6. #define CHIPSET             WS2811
  7.  
  8. #define MATRIX_WIDTH        -21
  9. #define MATRIX_HEIGHT       11
  10. #define MATRIX_TYPE         3
  11. #define MATRIX_SIZE         (MATRIX_WIDTH*MATRIX_HEIGHT)
  12. #define NUMPIXELS           MATRIX_SIZE
  13.  
  14. #define STROBE 4
  15. #define RESET 5
  16.  
  17. cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;
  18.  
  19. unsigned long int myTimeOffset;
  20.  
  21. int hpoints[21];
  22. int vpoints[11];
  23. int arrAmplitudes[10];
  24. int arrPeaks[10];
  25.  
  26. byte myStep;
  27. byte iBand;
  28.  
  29. bool myInit=false;
  30.  
  31. void setup()
  32. {
  33.   FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds[0], leds.Size()).setCorrection(TypicalSMD5050);
  34.   FastLED.setCorrection(TypicalLEDStrip);
  35.   FastLED.setBrightness(255);
  36.   Serial.begin(9600);
  37.   pinMode(STROBE, OUTPUT);
  38.   pinMode(RESET, OUTPUT);
  39.   pinMode(A0, INPUT);
  40.   pinMode(A1, INPUT);
  41.   digitalWrite(STROBE, false);
  42.   digitalWrite(RESET, false);
  43.   delay(5);
  44. }
  45.  
  46. void Read_Frequencies() {
  47.   digitalWrite(RESET, true);
  48.   delayMicroseconds(200);
  49.   digitalWrite(RESET, false);
  50.   delayMicroseconds(200);
  51.  
  52.   for (iBand = 0; iBand < 7; iBand++)
  53.   {
  54.     digitalWrite(STROBE, true);
  55.     delayMicroseconds(50);
  56.     digitalWrite(STROBE, false);
  57.     delayMicroseconds(50);
  58.     arrAmplitudes[iBand] = (int)((analogRead(A0) + analogRead(A1))/2);
  59.   }
  60.   arrAmplitudes[7]=(arrAmplitudes[0]+arrAmplitudes[1]+arrAmplitudes[2]+arrAmplitudes[3]+arrAmplitudes[4]+arrAmplitudes[5]+arrAmplitudes[6])/7;
  61.   arrAmplitudes[8]=(arrAmplitudes[0]+arrAmplitudes[1]+arrAmplitudes[2]+arrAmplitudes[3])/4;
  62.   arrAmplitudes[9]=(arrAmplitudes[4]+arrAmplitudes[5]+arrAmplitudes[6])/3;
  63. }
  64.  
  65. boolean detectSilence(int myInput) {
  66.   static unsigned long myOffset;
  67.   unsigned long myTime;
  68.   if (myInput>80) myOffset  = millis();
  69.   myTime=millis()-myOffset;
  70.   return (myTime>1500);
  71. }
  72.  
  73. boolean detectLoudness(int myInput) {
  74.   static unsigned long myOffset;
  75.   unsigned long myTime;
  76.   if (myInput<95) myOffset  = millis();
  77.   myTime=millis()-myOffset;
  78.   return (myTime>200);
  79. }
  80.  
  81. void shiftArrayAndInsertValueH(byte count, int newValue){
  82.   int points_mem[21];
  83.   memcpy(points_mem, hpoints, count*sizeof(int));
  84.   for(byte i=0; i<count-1;i++) {
  85.     hpoints[i]=points_mem[i+1];
  86.   }
  87.   hpoints[count-1]=newValue;
  88. }
  89.  
  90. void shiftArrayAndInsertValueV(byte count, int newValue){
  91.   int points_mem[11];
  92.   memcpy(points_mem, vpoints, count*sizeof(int));
  93.   for(byte i=0; i<count-1;i++) {
  94.     vpoints[i]=points_mem[i+1];
  95.   }
  96.   vpoints[count-1]=newValue;
  97. }
  98.  
  99.  
  100. void movingCircles(float rfactor, float tsummand) {
  101.   static float t1=0.00000;
  102.   static float t2=0.33333;
  103.   static float t3=0.66666;
  104.   static float t4=0.99999;
  105.   static float t5=1.33332;
  106.   static float t6=1.66665;
  107.   float r1;
  108.   float r2;
  109.   float r3;
  110.   float r4;
  111.   float r5;
  112.   float r6;
  113.   float rx=9;
  114.   float ry=4;
  115.   byte x1;
  116.   byte x2;
  117.   byte x3;
  118.   byte x4;
  119.   byte x5;
  120.   byte x6;
  121.   byte y1;
  122.   byte y2;
  123.   byte y3;
  124.   byte y4;
  125.   byte y5;
  126.   byte y6;
  127.  
  128.   r1=arrAmplitudes[0]/200;
  129.   r2=arrAmplitudes[1]/200;
  130.   r3=arrAmplitudes[2]/200;
  131.   r4=arrAmplitudes[3]/200;
  132.   r5=arrAmplitudes[4]/200;
  133.   r6=arrAmplitudes[5]/200;
  134.  
  135.   x1=10.5+ rfactor*rx * sin(t1*PI);
  136.   y1= 5.5+ rfactor*ry * cos(t1*PI);
  137.   x2=10.5+ rfactor*rx * sin(t2*PI);
  138.   y2= 5.5+ rfactor*ry * cos(t2*PI);
  139.   x3=10.5+ rfactor*rx * sin(t3*PI);
  140.   y3= 5.5+ rfactor*ry * cos(t3*PI);
  141.   x4=10.5+ rfactor*rx * sin(t4*PI);
  142.   y4= 5.5+ rfactor*ry * cos(t4*PI);
  143.   x5=10.5+ rfactor*rx * sin(t5*PI);
  144.   y5= 5.5+ rfactor*ry * cos(t5*PI);
  145.   x6=10.5+ rfactor*rx * sin(t6*PI);
  146.   y6= 5.5+ rfactor*ry * cos(t6*PI);
  147.  
  148.   t1+=tsummand;
  149.   t2+=tsummand;
  150.   t3+=tsummand;
  151.   t4+=tsummand;
  152.   t5+=tsummand;
  153.   t6+=tsummand;
  154.  
  155.   if (t1>1.99999) t1=0.00000;
  156.   if (t2>2.33331) t2=0.33333;
  157.   if (t3>2.66664) t3=0.66666;
  158.   if (t4>2.99997) t4=0.99999;
  159.   if (t5>3.33333) t5=1.33332;
  160.   if (t6>3.66666) t6=1.66665;
  161.  
  162.   leds.DrawCircle(x1, y1, r1, CRGB::Red);
  163.   leds.DrawCircle(x2, y2, r2, CRGB::Blue);
  164.   leds.DrawCircle(x3, y3, r3, CRGB::Yellow);
  165.   leds.DrawCircle(x4, y4, r4, CRGB::Green);
  166.   leds.DrawCircle(x5, y5, r5, CRGB::Magenta);
  167.   leds.DrawCircle(x6, y6, r6, CRGB::Cyan);
  168. }
  169.  
  170. void spectrumAnalyzerBottom(int iPeakDecay) {
  171.  
  172.   int iAmplitude1=arrAmplitudes[0]/90;
  173.   int iAmplitude2=arrAmplitudes[1]/90;
  174.   int iAmplitude3=arrAmplitudes[2]/90;
  175.   int iAmplitude4=arrAmplitudes[3]/90;
  176.   int iAmplitude5=arrAmplitudes[4]/90;
  177.   int iAmplitude6=arrAmplitudes[5]/90;
  178.   int iAmplitude7=arrAmplitudes[6]/90;
  179.  
  180.   int iPeak1=arrPeaks[0]/90;
  181.   int iPeak2=arrPeaks[1]/90;
  182.   int iPeak3=arrPeaks[2]/90;
  183.   int iPeak4=arrPeaks[3]/90;
  184.   int iPeak5=arrPeaks[4]/90;
  185.   int iPeak6=arrPeaks[5]/90;
  186.   int iPeak7=arrPeaks[6]/90;
  187.  
  188.   byte treshold=2;
  189.  
  190.   if (!myInit) memcpy(arrPeaks, arrAmplitudes, 10*sizeof(int));
  191.   myInit=true;
  192.  
  193.   for(int i=0; i<10; i++) {
  194.     if (arrAmplitudes[i]>=arrPeaks[i]) arrPeaks[i]=arrAmplitudes[i];
  195.     if (arrPeaks[i]>=0) arrPeaks[i]-=iPeakDecay;
  196.   }
  197.  
  198.   if(iAmplitude1>=treshold) for(byte i1=0; i1<=iAmplitude1; i1++) leds.DrawLine( 0,i1, 2,i1,CHSV(i1*15,255,255));
  199.   if(iAmplitude2>=treshold) for(byte i2=0; i2<=iAmplitude2; i2++) leds.DrawLine( 3,i2, 5,i2,CHSV(150+i2*15,255,255));
  200.   if(iAmplitude3>=treshold) for(byte i3=0; i3<=iAmplitude3; i3++) leds.DrawLine( 6,i3, 8,i3,CHSV(i3*15,255,255));
  201.   if(iAmplitude4>=treshold) for(byte i4=0; i4<=iAmplitude4; i4++) leds.DrawLine( 9,i4,11,i4,CHSV(150+i4*15,255,255));
  202.   if(iAmplitude5>=treshold) for(byte i5=0; i5<=iAmplitude5; i5++) leds.DrawLine(12,i5,14,i5,CHSV(i5*15,255,255));
  203.   if(iAmplitude6>=treshold) for(byte i6=0; i6<=iAmplitude6; i6++) leds.DrawLine(15,i6,17,i6,CHSV(150+i6*15,255,255));
  204.   if(iAmplitude7>=treshold) for(byte i7=0; i7<=iAmplitude7; i7++) leds.DrawLine(18,i7,20,i7,CHSV(i7*15,255,255));
  205.  
  206.   if (iPeakDecay>0) {
  207.     leds.DrawLine( 0,iPeak1, 2,iPeak1,CHSV(iPeak1*15,255,255));
  208.     leds.DrawLine( 3,iPeak2, 5,iPeak2,CHSV(150+iPeak2*15,255,255));
  209.     leds.DrawLine( 6,iPeak3, 8,iPeak3,CHSV(iPeak3*15,255,255));
  210.     leds.DrawLine( 9,iPeak4,11,iPeak4,CHSV(150+iPeak4*15,255,255));
  211.     leds.DrawLine(12,iPeak5,14,iPeak5,CHSV(iPeak5*15,255,255));
  212.     leds.DrawLine(15,iPeak6,17,iPeak6,CHSV(150+iPeak6*15,255,255));
  213.     leds.DrawLine(18,iPeak7,20,iPeak7,CHSV(iPeak7*15,255,255));
  214.   }
  215. }
  216.  
  217. void spectrumAnalyzerCenter(int iPeakDecay) {
  218.  
  219.   int iAmplitude1=arrAmplitudes[0]/180;
  220.   int iAmplitude2=arrAmplitudes[1]/180;
  221.   int iAmplitude3=arrAmplitudes[2]/180;
  222.   int iAmplitude4=arrAmplitudes[3]/180;
  223.   int iAmplitude5=arrAmplitudes[4]/180;
  224.   int iAmplitude6=arrAmplitudes[5]/180;
  225.   int iAmplitude7=arrAmplitudes[6]/180;
  226.  
  227.   int iPeak1=arrPeaks[0]/180;
  228.   int iPeak2=arrPeaks[1]/180;
  229.   int iPeak3=arrPeaks[2]/180;
  230.   int iPeak4=arrPeaks[3]/180;
  231.   int iPeak5=arrPeaks[4]/180;
  232.   int iPeak6=arrPeaks[5]/180;
  233.   int iPeak7=arrPeaks[6]/180;
  234.  
  235.   byte treshold=1;
  236.  
  237.   if (!myInit) memcpy(arrPeaks, arrAmplitudes, 10*sizeof(int));
  238.   myInit=true;
  239.  
  240.   for(int i=0; i<10; i++) {
  241.     if (arrAmplitudes[i]>=arrPeaks[i]) arrPeaks[i]=arrAmplitudes[i];
  242.     if (arrPeaks[i]>=0) arrPeaks[i]-=iPeakDecay;
  243.   }
  244.  
  245.   if(iAmplitude1>=treshold) for(byte i1=0; i1<=iAmplitude1; i1++) leds.DrawLine( 0,i1+5, 2,i1+5,CHSV(i1*20,255,255));
  246.   if(iAmplitude2>=treshold) for(byte i2=0; i2<=iAmplitude2; i2++) leds.DrawLine( 3,i2+5, 5,i2+5,CHSV(120+i2*20,255,255));
  247.   if(iAmplitude3>=treshold) for(byte i3=0; i3<=iAmplitude3; i3++) leds.DrawLine( 6,i3+5, 8,i3+5,CHSV(i3*20,250,255));
  248.   if(iAmplitude4>=treshold) for(byte i4=0; i4<=iAmplitude4; i4++) leds.DrawLine( 9,i4+5,11,i4+5,CHSV(120+i4*20,255,255));
  249.   if(iAmplitude5>=treshold) for(byte i5=0; i5<=iAmplitude5; i5++) leds.DrawLine(12,i5+5,14,i5+5,CHSV(i5*20,255,255));
  250.   if(iAmplitude6>=treshold) for(byte i6=0; i6<=iAmplitude6; i6++) leds.DrawLine(15,i6+5,17,i6+5,CHSV(120+i6*20,255,255));
  251.   if(iAmplitude7>=treshold) for(byte i7=0; i7<=iAmplitude7; i7++) leds.DrawLine(18,i7+5,20,i7+5,CHSV(i7*20,255,255));
  252.  
  253.   if(iAmplitude1>=treshold) for(byte i1=0; i1<=iAmplitude1; i1++) leds.DrawLine( 0,5-i1, 2,5-i1,CHSV(i1*20,255,255));
  254.   if(iAmplitude2>=treshold) for(byte i2=0; i2<=iAmplitude2; i2++) leds.DrawLine( 3,5-i2, 5,5-i2,CHSV(120+i2*20,255,255));
  255.   if(iAmplitude3>=treshold) for(byte i3=0; i3<=iAmplitude3; i3++) leds.DrawLine( 6,5-i3, 8,5-i3,CHSV(i3*20,255,255));
  256.   if(iAmplitude4>=treshold) for(byte i4=0; i4<=iAmplitude4; i4++) leds.DrawLine( 9,5-i4,11,5-i4,CHSV(120+i4*20,255,255));
  257.   if(iAmplitude5>=treshold) for(byte i5=0; i5<=iAmplitude5; i5++) leds.DrawLine(12,5-i5,14,5-i5,CHSV(i5*20,255,255));
  258.   if(iAmplitude6>=treshold) for(byte i6=0; i6<=iAmplitude6; i6++) leds.DrawLine(15,5-i6,17,5-i6,CHSV(120+i6*20,255,255));
  259.   if(iAmplitude7>=treshold) for(byte i7=0; i7<=iAmplitude7; i7++) leds.DrawLine(18,5-i7,20,5-i7,CHSV(i7*20,255,255));
  260.  
  261.   if (iPeakDecay>0) {
  262.     leds.DrawLine( 0,iPeak1+5, 2,iPeak1+5,CHSV(iPeak1*20,255,255));
  263.     leds.DrawLine( 3,iPeak2+5, 5,iPeak2+5,CHSV(120+iPeak2*20,255,255));
  264.     leds.DrawLine( 6,iPeak3+5, 8,iPeak3+5,CHSV(iPeak3*20,250,255));
  265.     leds.DrawLine( 9,iPeak4+5,11,iPeak4+5,CHSV(120+iPeak4*20,255,255));
  266.     leds.DrawLine(12,iPeak5+5,14,iPeak5+5,CHSV(iPeak5*20,255,255));
  267.     leds.DrawLine(15,iPeak6+5,17,iPeak6+5,CHSV(120+iPeak6*20,255,255));
  268.     leds.DrawLine(18,iPeak7+5,20,iPeak7+5,CHSV(iPeak7*20,255,255));
  269.  
  270.     leds.DrawLine( 0,5-iPeak1, 2,5-iPeak1,CHSV(iPeak1*20,255,255));
  271.     leds.DrawLine( 3,5-iPeak2, 5,5-iPeak2,CHSV(120+iPeak2*20,255,255));
  272.     leds.DrawLine( 6,5-iPeak3, 8,5-iPeak3,CHSV(iPeak3*20,255,255));
  273.     leds.DrawLine( 9,5-iPeak4,11,5-iPeak4,CHSV(120+iPeak4*20,255,255));
  274.     leds.DrawLine(12,5-iPeak5,14,5-iPeak5,CHSV(iPeak5*20,255,255));
  275.     leds.DrawLine(15,5-iPeak6,17,5-iPeak6,CHSV(120+iPeak6*20,255,255));
  276.     leds.DrawLine(18,5-iPeak7,20,5-iPeak7,CHSV(iPeak7*20,255,255));
  277.   }
  278. }
  279.  
  280. void lissajousFigure(float f1_1, float f2_1, float f1_2, float f2_2) {
  281.  
  282.   static float p1;
  283.   static float p2;
  284.  
  285.   float t1;
  286.   float t2;
  287.   float r1;
  288.   float r2;
  289.  
  290.   int x1;
  291.   int x2;
  292.   int y1;
  293.   int y2;
  294.   static int xOld1;
  295.   static int xOld2;
  296.   static int yOld1;
  297.   static int yOld2;
  298.   int iHue1;
  299.   int iHue2;
  300.  
  301.   r1=(float)arrAmplitudes[8]/150;
  302.   t1=0;
  303.   iHue1=0;
  304.   do {
  305.     x1 = 10.5 + 2*r1 * sin(f1_1*t1*PI + p1);
  306.     y1 = 05.5 + 1*r1 * sin(f2_1*t1*PI);
  307.     if ((t1>0) & (r1<=4) & (r1>=1)) leds.DrawLine(xOld1, yOld1, x1, y1, CRGB::Red );
  308.     if ((t1>0) & (r1> 4)) leds.DrawLine(xOld1, yOld1, x1, y1, CHSV(iHue1,255,255));
  309.     t1+=0.05;
  310.     iHue1+=6;
  311.     xOld1=x1;
  312.     yOld1=y1;
  313.   } while (t1<2.1);
  314.  
  315.   r2=(float)(arrAmplitudes[9])/130;
  316.   t2=0;
  317.   p2=PI/2;
  318.   iHue2=0;
  319.   do {
  320.     x2 = 10.5 + 2*r2 * sin(f1_2*t2*PI + p2);
  321.     y2 = 05.5 + 1*r2 * sin(f2_2*t2*PI);
  322.     if ((t2>0) & (r2<=4) & (r2>=1)) leds.DrawLine(xOld2, yOld2, x2, y2,CRGB::Blue);
  323.     if ((t2>0) & (r2> 4)) leds.DrawLine(xOld2, yOld2, x2, y2,CHSV(iHue2,255,255));
  324.     t2 += 0.05;
  325.     iHue2+=6;
  326.     xOld2=x2;
  327.     yOld2=y2;
  328.   } while (t2<2.1);
  329.  
  330.   p1 +=0.05;
  331.   if (p1==PI) p1=0;
  332. }
  333.  
  334. void waveFormVisualizerHorizontal() {
  335.   shiftArrayAndInsertValueH(21, arrAmplitudes[7]);
  336.   for(int i=0; i<21; i++) {
  337.     leds.DrawPixel(i,4+hpoints[i]/85,CHSV(hpoints[i]/3,255,255));
  338.     leds.DrawPixel(i,6-hpoints[i]/85,CHSV(hpoints[i]/3,255,255));
  339.     leds.DrawLine(i,4+hpoints[i]/85,i,6-hpoints[i]/85,CHSV(hpoints[i]/3,255,255));
  340.   }
  341.   delay(10);
  342. }
  343.  
  344. void waveFormVisualizerVertical() {
  345.   shiftArrayAndInsertValueV(11, arrAmplitudes[7]);
  346.   for(int i=0; i<11; i++) {
  347.     //leds.DrawPixel( 9+vpoints[i]/67,i,CHSV(vpoints[i]/3,255,255));
  348.     //leds.DrawPixel(11-vpoints[i]/67,i,CHSV(vpoints[i]/3,255,255));
  349.     leds.DrawLine(9+vpoints[i]/67,i,11-vpoints[i]/67,i,CHSV(vpoints[i]/3,255,255));
  350.   }
  351.   delay(10);
  352. }
  353.  
  354. void loop()
  355. {
  356.   FastLED.clear();
  357.  
  358.   unsigned long int myTime=millis()-myTimeOffset;
  359.   unsigned long int animationTime=180000;
  360.  
  361.   Read_Frequencies();
  362.  
  363.   switch (myStep) {
  364.   case 0:
  365.     if (detectLoudness(arrAmplitudes[7])) myStep=10;
  366.     myTimeOffset=millis();
  367.     break;
  368.   case 10:
  369.     movingCircles((float)(arrAmplitudes[7])/500, 0.005);
  370.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=20;
  371.     break;
  372.   case 20:
  373.     if (detectLoudness(arrAmplitudes[7])) myStep=30;
  374.     myTimeOffset=millis();
  375.     break;
  376.    case 30:
  377.     spectrumAnalyzerCenter(0);
  378.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=40;
  379.     break;
  380.   case 40:
  381.     if (detectLoudness(arrAmplitudes[7])) myStep=50;
  382.     myTimeOffset=millis();
  383.     break;
  384.   case 50:
  385.     movingCircles(1, (float)arrAmplitudes[0]/50000);
  386.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=60;
  387.     break;
  388.    case 60:
  389.     if (detectLoudness(arrAmplitudes[7])) myStep=70;
  390.     myTimeOffset=millis();
  391.     break;
  392.   case 70:
  393.     waveFormVisualizerHorizontal();
  394.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=80;
  395.     break;
  396.   case 80:
  397.     if (detectLoudness(arrAmplitudes[7])) myStep=90;
  398.     myTimeOffset=millis();
  399.     break;
  400.   case 90:
  401.     lissajousFigure(1,2,1,1);
  402.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=100;
  403.     break;
  404.   case 100:
  405.     if (detectLoudness(arrAmplitudes[7])) myStep=110;
  406.     myTimeOffset=millis();
  407.     break;
  408.   case 110:
  409.     waveFormVisualizerVertical();
  410.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=120;
  411.     break;
  412.   case 120:
  413.     if (detectLoudness(arrAmplitudes[7])) myStep=130;
  414.     myTimeOffset=millis();
  415.     break;
  416.   case 130:
  417.     spectrumAnalyzerBottom(10);
  418.     if (detectSilence(arrAmplitudes[7]) || myTime>animationTime) myStep=0;
  419.     break;
  420.  }
  421.  
  422.   FastLED.show();
  423.  
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement