davidwang34

Untitled

Nov 17th, 2013
6,973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. /* David Wang
  2. * Code that takes audio input from a 3.5mm cable
  3. * and flashes an LED strip based on the frequency
  4. * of the music.
  5. *
  6. * HUGE thanks to the arduino community
  7. * If you see your code here, I owe you my gratitude
  8. *
  9. */
  10.  
  11. int analogPin = 0; // MSGEQ7 OUT
  12. int strobePin = 2; // MSGEQ7 STROBE
  13. int resetPin = 4; // MSGEQ7 RESET
  14. int spectrumValue[7];
  15.  
  16. // MSGEQ7 OUT pin produces values around 50-80
  17. // when there is no input, so use this value to
  18. // filter out a lot of the chaff.
  19. int filterValue = 80;
  20.  
  21. // LED pins connected to the PWM pins on the Arduino
  22.  
  23. int ledPinR = 9;
  24. int ledPinG = 10;
  25. int ledPinB = 11;
  26.  
  27. void setup()
  28. {
  29. Serial.begin(9600);
  30. // Read from MSGEQ7 OUT
  31. pinMode(analogPin, INPUT);
  32. // Write to MSGEQ7 STROBE and RESET
  33. pinMode(strobePin, OUTPUT);
  34. pinMode(resetPin, OUTPUT);
  35.  
  36. // Set analogPin's reference voltage
  37. analogReference(DEFAULT); // 5V
  38.  
  39. // Set startup values for pins
  40. digitalWrite(resetPin, LOW);
  41. digitalWrite(strobePin, HIGH);
  42. }
  43.  
  44. void loop()
  45. {
  46. // Set reset pin low to enable strobe
  47. digitalWrite(resetPin, HIGH);
  48. digitalWrite(resetPin, LOW);
  49.  
  50. // Get all 7 spectrum values from the MSGEQ7
  51. for (int i = 0; i < 7; i++)
  52. {
  53. digitalWrite(strobePin, LOW);
  54. delayMicroseconds(30); // Allow output to settle
  55.  
  56. spectrumValue[i] = analogRead(analogPin);
  57.  
  58. // Constrain any value above 1023 or below filterValue
  59. spectrumValue[i] = constrain(spectrumValue[i], filterValue, 1023);
  60.  
  61.  
  62. // Remap the value to a number between 0 and 255
  63. spectrumValue[i] = map(spectrumValue[i], filterValue, 1023, 0, 255);
  64.  
  65. // Remove serial stuff after debugging
  66. Serial.print(spectrumValue[i]);
  67. Serial.print(" ");
  68. digitalWrite(strobePin, HIGH);
  69. }
  70.  
  71. Serial.println();
  72.  
  73. // Write the PWM values to the LEDs
  74. // I find that with three LEDs, these three spectrum values work the best
  75. analogWrite(ledPinR, spectrumValue[1]);
  76. analogWrite(ledPinG, spectrumValue[4]);
  77. analogWrite(ledPinB, spectrumValue[6]);
  78. }
Add Comment
Please, Sign In to add comment