/* David Wang
* Code that takes audio input from a 3.5mm cable
* and flashes an LED strip based on the frequency
* of the music.
*
* HUGE thanks to the arduino community
* If you see your code here, I owe you my gratitude
*
*Adapted by Max Elfelt to include sensitivity adjuster potentiometer.
*/
int analogPin = 0; // MSGEQ7 OUT int strobePin = 2; // MSGEQ7 STROBE int resetPin = 4; // MSGEQ7 RESET int potPin = 1; //pot for filterValue int val = 0; //variable to store the value from the pot which acts as an audio sensitivity adjustment int spectrumValue[7];
// MSGEQ7 OUT pin produces values around 50-80 // when there is no input, so use this value to // filter out a lot of the chaff. int filterValue =580; //Not sure if this is even necessary
// LED pins connected to the PWM pins on the Arduino
int ledPinB = 9; int ledPinR = 10; int ledPinG = 11;
void setup() { Serial.begin(9600);
// Read from MSGEQ7 OUT pinMode(analogPin, INPUT); // Write to MSGEQ7 STROBE and RESET pinMode(strobePin, OUTPUT); pinMode(resetPin, OUTPUT);
// Set analogPin's reference voltage analogReference(DEFAULT); // 5V
// Set startup values for pins digitalWrite(resetPin, LOW); digitalWrite(strobePin, HIGH); }
void loop() { // Set reset pin low to enable strobe digitalWrite(resetPin, HIGH); digitalWrite(resetPin, LOW);
//Read Pot value val = analogRead(potPin); //read the value from the pot val = constrain(val, 150, 1022); //adjust these values to change the range of the pot Serial.print(val); //use for debugging pot Serial.print(" ");
// Get all 7 spectrum values from the MSGEQ7 for (int i = 0; i < 7; i++) { digitalWrite(strobePin, LOW); delayMicroseconds(50); // Allow output to settle
spectrumValue[i] = analogRead(analogPin);
// Constrain any value above 1023 or below filterValue
spectrumValue[i] = constrain(spectrumValue[i], val, 1023);
// Remap the value to a number between 0 and 255
spectrumValue[i] = map(spectrumValue[i], val, 1023, 0, 250);
// Remove serial stuff after debugging
Serial.print(spectrumValue[i]);
Serial.print(" ");
digitalWrite(strobePin, HIGH);
}
Serial.println();
// Write the PWM values to the LEDs // I find that with three LEDs, these three spectrum values work the best analogWrite(ledPinB, spectrumValue[1]); analogWrite(ledPinG, spectrumValue[3]); analogWrite(ledPinR, spectrumValue[5]); }