Advertisement
baldengineer

MSGEQ7 Simple Spectrum Analyzer

Jan 7th, 2012
6,660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #define msg7RESET 11
  2. #define msg7Strobe 12
  3. #define msg7DCout 0
  4. const int LEDpins[7] = {3,5,5,6,9,9,10};    // there are 5 LEDs and 7 freq bands.  So, repeat LEDs
  5.  
  6. #define pushButton 2
  7.  
  8. void setup() {                
  9.   // initialize the digital pin as an output.
  10.   // Pin 13 has an LED connected on most Arduino boards:
  11.   for (int x=0; x<7; x++) {
  12.       pinMode(LEDpins[x], OUTPUT);
  13.   }
  14.   pinMode(msg7RESET, OUTPUT);
  15.   pinMode(msg7Strobe, OUTPUT);
  16.  
  17.   pinMode(pushButton, INPUT);       // never actually used in this example.
  18.   digitalWrite(pushButton, HIGH);  // Enable internal pull-up
  19. }
  20.  
  21. void loop() {
  22.     digitalWrite(msg7RESET, HIGH);          // reset the MSGEQ7's counter
  23.     delay(5);
  24.     digitalWrite(msg7RESET, LOW);
  25.  
  26.     for (int x = 0; x < 7; x++){
  27.         digitalWrite(msg7Strobe, LOW);      // output each DC value for each freq band
  28.         delayMicroseconds(35); // to allow the output to settle
  29.         int spectrumRead = analogRead(msg7DCout);
  30.  
  31.         int PWMvalue = map(spectrumRead, 0, 1024, 0, 255);  // scale analogRead's value to Write's 255 max
  32.         if (PWMvalue < 50)
  33.             PWMvalue = PWMvalue / 2;        // bit of a noise filter, so the LEDs turn off at low levels
  34.  
  35.         analogWrite(LEDpins[x], PWMvalue);
  36.         digitalWrite(msg7Strobe, HIGH);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement