Advertisement
Guest User

Untitled

a guest
Mar 12th, 2014
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <math.h>
  2. #include <Adafruit_NeoPixel.h>
  3. #include "Timer.h"
  4.  
  5. Timer t;
  6.  
  7. String colorPick = "GreenYellowRed";
  8.  
  9. const byte analogPinL         = 0;   // - - - - - - - - - - - - - - - - - left channel analog data from shield
  10. const byte analogPinR         = 1;   // - - - - - - - - - - - - - - - - - right channel analog data from shield
  11. const byte strobePin          = 4;   // - - - - - - - - - - - - - - - - - data strobe for shield
  12. const byte resetPin           = 5;   // - - - - - - - - - - - - - - - - - reset strobe for shield
  13. const byte dataPin            = 8;   // - - - - - - - - - - - - - - - - - data pin for Neopixels
  14. const byte numBand            = 10;  // - - - - - - - - - - - - - - - - - number of LEDs for each freq band
  15. const byte numTop             = 0;   // - - - - - - - - - - - - - - - - - number of LEDs to have top color
  16. const int noise[]             = {1, 1, 1, 1, 1, 1, 1}; // - - - - - - - - set this to magnitude of noise from shield
  17. const float gain[]            = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; // - gain for each band
  18. const unsigned long loop_dlay = 35;  // - - - - - - - - - - - - - - - - - loop delay to slow down display update rate
  19.  
  20. Adafruit_NeoPixel strip = Adafruit_NeoPixel(numBand*7, dataPin, NEO_GRB + NEO_KHZ800);
  21.  
  22. enum audio {
  23.   MONO,
  24.   RIGHT,
  25.   LEFT };
  26.  
  27. int spectrumReadR;  //R magnitude from shield
  28. int spectrumReadL;  //L magnitude from shield
  29. int audio = MONO;   //set audio mode to mono, combine R&L channels
  30. int mag = 0;        //the magnitude of a freq band
  31. int numON = 0;      //the number of LEDs on in a freq band
  32. float fl_mag = 0.0; //floating point mag after noise removal and scaling
  33.  
  34. int peakArray[7];
  35.  
  36. byte
  37.   peak = 0,
  38.   dotCount = 0;
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.   pinMode(resetPin, OUTPUT);
  43.   pinMode(strobePin, OUTPUT);
  44.   pinMode(dataPin, OUTPUT);
  45.  
  46.   strip.begin(); //begin strip communication
  47.   strip.show(); //set strip blank
  48.  
  49.   digitalWrite(resetPin,HIGH);
  50.     delayMicroseconds(5);
  51.   digitalWrite(strobePin,HIGH);
  52.     delayMicroseconds(50);              // strobe PW > 18 usec min
  53.   digitalWrite(strobePin,LOW);
  54.     delayMicroseconds(50);              //reset PW > 100 usec min
  55.   digitalWrite(resetPin,LOW);
  56.     delayMicroseconds(5);              
  57.   digitalWrite(strobePin,HIGH);
  58.     delayMicroseconds(100);             // allow reset to strobe falling > 72 usec min
  59.  
  60.   t.every(100,peakLower);
  61. }
  62.  
  63. void loop() {
  64.   for(byte band = 1; band <= 7; band++) {
  65.     digitalWrite(strobePin, LOW); //set analyzer to low to read
  66.     delayMicroseconds(40);
  67.    
  68.     spectrumReadR = analogRead(analogPinR); //read right audio
  69.     spectrumReadL = analogRead(analogPinL); //read left audio
  70.    
  71.     digitalWrite(strobePin, HIGH); //set analyzer back to high
  72.    
  73.     mag = (spectrumReadR + spectrumReadL) / 2;
  74.     mag = max(0, (mag - noise[band-1])); //creates magnitude of frequency
  75.     fl_mag = gain[band-1] * float(mag); //adjusts magnitude for gain
  76.     numON = map(fl_mag, 0, 1024, 0, numBand+1); //maps magnitude to number of active pixels
  77.  
  78.     anyBand(band);
  79.    
  80.     //Serial.print(band);
  81.     //Serial.println(peakArray[band-1]);
  82.     if(peakArray[band-1]==0) strip.setPixelColor(peakArray[band-1] + numBand*(band-1), strip.Color(0,0,0));
  83.     else strip.setPixelColor(peakArray[band-1] + numBand*(band-1), strip.Color(255,0,0));
  84.     t.update();
  85.   }
  86.   strip.setBrightness(40);
  87.   strip.show();
  88.  
  89.   delay(loop_dlay);
  90. }
  91.  
  92. void readBand(byte band) {
  93.   for(byte band = 1; band <= 7; band++) {
  94.     digitalWrite(strobePin, LOW); //set analyzer to low to read
  95.     delayMicroseconds(40);
  96.    
  97.     spectrumReadR = analogRead(analogPinR); //read right audio
  98.     spectrumReadL = analogRead(analogPinL); //read left audio
  99.    
  100.     digitalWrite(strobePin, HIGH); //set analyzer back to high
  101.    
  102.     mag = (spectrumReadR + spectrumReadL) / 2; //average L and R spectrums to make mono
  103.     mag = max(0, (mag - noise[band-1])); //creates magnitude of frequency
  104.     fl_mag = gain[band-1] * float(mag); //adjusts magnitude for gain
  105.     numON = map(fl_mag, 0, 1024, 0, numBand+1); //maps magnitude to number of active pixels
  106.  
  107.     anyBand(band);
  108.   }
  109. }
  110.  
  111. void anyBand(byte band) {
  112.   for(byte i = 0; i < numBand; i++){
  113.     if(i < (numON - numTop - 1)){
  114.       strip.setPixelColor(i + numBand*(band-1), Wheel(map(i,0,numBand-1,20,83))); //main wheel colors
  115.     }
  116.     else if(i >= numON){
  117.       strip.setPixelColor(i + numBand*(band-1), strip.Color(0,0,0)); //unused colors on wheel
  118.     }
  119.     else{
  120.       if(i > peakArray[band-1]) peakArray[band-1] = i; //used for falling peak dot
  121.     }
  122.   }
  123. }
  124.  
  125. void peakLower() {
  126.   for(byte i = 0; i < 7; i++) {
  127.     if(peakArray[i] > 0) peakArray[i]--;
  128.     else continue;
  129.   }
  130. }
  131.  
  132. uint32_t Wheel(byte WheelPos) {
  133.   if(colorPick == "GreenYellowRed") {
  134.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  135.   }
  136.   else if(colorPick == "PinkPurpleBlue") {
  137.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  138.   }
  139.   else if(colorPick == "GreenTealBlue") {
  140.     return strip.Color(0, 255 - WheelPos * 3, WheelPos * 3);
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement