Facebook
From matrix0606, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 92
  1. //7 BAND SPECTRUM ANALYZER MSGE07
  2. //DESIGN PLATINUM 2019 www.youtube.com/c/PLATINUMKIT
  3. //FACEBOOK   https://www.facebook.com/LegendMasters/
  4. //YOUTUBE VIDEO DIY ACRYLIC PART.1   https://youtu.be/xDKbpwumgF8
  5. //YOUTUBE VIDEO DIY ACRYLIC PART.2
  6. //YOUTUBE VIDEO DIY INFINITY MIRROR  https://youtu.be/j2-0d9xBDJk
  7. //THIS IS NOT FOR SALE ONLY FOR HOBBIES.
  8. //WE SHARE TO ADD A KNOWLEDGE SCIENCE.
  9.  
  10. #include <Arduino.h>
  11. #include <math.h>
  12. #include <Adafruit_NeoPixel.h>
  13. #include "MSGEQ7.h"
  14. Timer t;
  15. #define Acolor A0
  16. #define Scolor A6
  17. #define Pcolor A7
  18. #define MAX_BRIGHTNESS 255
  19. #define MIN_BRIGHTNESS 20
  20. const int brightnessInPin = A2;
  21. int led = 5;
  22. unsigned long x;
  23. unsigned long y = 0;
  24. const byte analogPin = A1;
  25. const byte strobePin = 3;
  26. const byte resetPin = 2;
  27. const byte dataPin = 4;
  28. const byte numBand = 20; //number of LEDs
  29. const byte numTop = 0;
  30. const int noise[] = {20, 20, 20, 20, 20, 20, 20};
  31. const float gain[] = {2.05, 2.05, 2.05, 2.05, 2.05, 2.05, 2.05}; //Frequency GAIN
  32.                                                                  //Frequency =  63Hz, 160Hz, 400Hz, 1kHz, 2.5kHz, 6.25kHz, 16kHz
  33. const unsigned long dlay = 30;
  34.  
  35. void CReSet();
  36. void readBand(byte band);
  37. void anyBand(byte band);
  38. void peakLower();
  39.  
  40. Adafruit_NeoPixel strip = Adafruit_NeoPixel(numBand * 7, dataPin, NEO_GRB + NEO_KHZ800);
  41. enum audio
  42. {
  43.   MONO,
  44.   RIGHT,
  45.   LEFT
  46. };
  47. int spectrumReadR;
  48. int spectrumReadL;
  49. int audio = MONO;
  50. int mag = 0;
  51. int numON = 0;
  52. float fl_mag = 0.0;
  53. int k;
  54. int a;
  55. int s;
  56. int peakArray[7];
  57. byte
  58.     peak = 0,
  59.     dotCount = 0;
  60.  
  61. void setup()
  62. {
  63.   Serial.begin(9600);
  64.   pinMode(resetPin, OUTPUT);
  65.   pinMode(strobePin, OUTPUT);
  66.   pinMode(dataPin, OUTPUT);
  67.   strip.begin();
  68.   strip.show();
  69.   digitalWrite(resetPin, HIGH);
  70.   delayMicroseconds(5);
  71.   digitalWrite(strobePin, HIGH);
  72.   delayMicroseconds(50);
  73.   digitalWrite(strobePin, LOW);
  74.   delayMicroseconds(50);
  75.   digitalWrite(resetPin, LOW);
  76.   delayMicroseconds(5);
  77.   digitalWrite(strobePin, HIGH);
  78.   delayMicroseconds(100);
  79.   t.every(80, peakLower);
  80. }
  81. void loop()
  82. {
  83.   CReSet();
  84.   x = millis();
  85. }
  86. int dimH = 0;
  87. int p = 0;
  88. const long interval = 1;
  89.  
  90. void CReSet()
  91. {
  92.   if (x - y >= interval)
  93.   {
  94.     y = x;
  95.     dimH = map(p, 0, 4000, 0, 255);
  96.     analogWrite(led, dimH);
  97.     if (p >= 4000)
  98.     {
  99.       p = 0;
  100.     }
  101.     else
  102.     {
  103.       p = p + 1;
  104.     }
  105.   }
  106.   int mappedValue = map(analogRead(brightnessInPin), 0, 1023, 0, 255);
  107.   strip.setBrightness(constrain(mappedValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS));
  108.   a = analogRead(Acolor);
  109.   a = map(a, 0, 1023, 0, 400);
  110.   s = analogRead(Scolor);
  111.   s = map(s, 0, 1023, 35, 1500);
  112.   k = analogRead(Pcolor);
  113.   k = map(k, 0, 1023, 0, 750);
  114.   for (byte band = 1; band <= 7; band++)
  115.   {
  116.     digitalWrite(strobePin, LOW);
  117.     delayMicroseconds(40);
  118.     spectrumReadL = analogRead(analogPin);
  119.     digitalWrite(strobePin, HIGH);
  120.     mag = (spectrumReadR + spectrumReadL) / 2;
  121.     mag = max(0, (mag - noise[band - 1]));
  122.     fl_mag = gain[band - 1] * float(mag);
  123.     numON = map(fl_mag, 0, 1024, 1, numBand + 1);
  124.     anyBand(band);
  125.     if (peakArray[band - 1] == 0)
  126.       strip.setPixelColor(peakArray[band - 1] + numBand * (band - 1), strip.Color(0, 0, 0));
  127.     else
  128.       strip.setPixelColor(peakArray[band - 1] + numBand * (band - 1), Wheel(map(10, 0, numBand - 0, k, 255)));
  129.     t.update();
  130.   }
  131.   strip.show();
  132.   delay(dlay);
  133. }
  134.  
  135. void readBand(byte band)
  136. {
  137.   for (byte band = 1; band <= 7; band++)
  138.   {
  139.     digitalWrite(strobePin, LOW);
  140.     delayMicroseconds(40);
  141.     spectrumReadL = analogRead(analogPin);
  142.     digitalWrite(strobePin, HIGH);
  143.     mag = (spectrumReadR + spectrumReadL) / 2;
  144.     mag = max(0, (mag - noise[band - 1]));
  145.     fl_mag = gain[band - 1] * float(mag);
  146.     numON = map(fl_mag, 0, 1024, 1, numBand + 1);
  147.     anyBand(band);
  148.   }
  149. }
  150.  
  151. void anyBand(byte band)
  152. {
  153.   for (byte i = 0; i < numBand; i++)
  154.   {
  155.     if (i < (numON - numTop - 1))
  156.     {
  157.       strip.setPixelColor(i + numBand * (band - 1), Wheel(map(i, 0, numBand - s, a, 255)));
  158.     }
  159.     else if (i >= numON)
  160.     {
  161.       strip.setPixelColor(i + numBand * (band - 1), strip.Color(0, 0, 0));
  162.     }
  163.     else
  164.     {
  165.       if (i > peakArray[band - 1])
  166.         peakArray[band - 1] = i;
  167.     }
  168.   }
  169. }
  170.  
  171. void peakLower()
  172. {
  173.   for (byte i = 0; i < 7; i++)
  174.   {
  175.     if (peakArray[i] > 1)
  176.       peakArray[i]--;
  177.     else
  178.       continue;
  179.   }
  180. }
  181.  
  182. uint32_t Wheel(byte WheelPos)
  183. {
  184.   WheelPos = 255 - WheelPos;
  185.   if (WheelPos < 85)
  186.   {
  187.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  188.   }
  189.   if (WheelPos < 170)
  190.   {
  191.     WheelPos -= 85;
  192.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  193.   }
  194.   WheelPos -= 170;
  195.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  196. }