Hey everyone. First time posting on here (but not the first time visiting!). Let me preface this post with the statement that I'm a beginner when it comes to both the Arduino and building any kind of circuits. I get most of my information from countless hours googling and combining tutorials together until I make what I want.
That said.. I've been working on a project for a week or so that takes incoming audio signals, attempts to interpret the beat and, and then lights up an analog LED strip accordingly. I attached the circuit design for the audio input to the post. I got it and some code from Jeremy Blum's tutorial which I'll link at the end. So the signal comes into the Arduino from either the left or right channel (I'm only using one or the other right now until I get a digital strip) and it is modified a little to keep the numbers between 0 and 255. It then uses an if-else statement to change the color of the light strip based on the value of the modified input..
Now, the reason why I'm posting on here... While the LED strip lights up to the music (sorta) it's not really all that close to the beat of the music. Even watching the incoming values in the SerialMonitor I noticed that they don't correspond to the music. I get an extremely high concentration of numbers ranging from 40-90 and right now 55 is showing up 60% of the time. So that's the first problem which is probably due to the way I'm interpreting the audio input into a number. The second problem is that if I don't set all the LEDs to 0 at the beginning of the loop I get a strip of white lights that doesn't really change to the beat at all. The downside to turning them off every loop is that if the input value is the same, it just turns back on the same color. Which means there's a lot of strobe effect. Not too bad.. but I'm a perfectionist and find it really annoying. The circuit from the Arduino to the light strip is just a few transistors. I don't think it will be helpful to see/explain it since it's so basic, but I can post a pic if that would help anyone.
Anyways, here's the code. It's in no way pretty, and I'm sure there's a million ways I can make this more efficient, so please let me know what can be improved!
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
//right audio out
int left_channel = A0;
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
//I found that if I don't reset the lights to zero at the beginning of each loop, the strip
//just lights up white..
analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 0);
//Take the incoming signal and limit it to 0-255. I got most of this from a tutorial
//and when I tried it, the constrain function didn't work right. When I did the absolute
//value of the input - 200, I got a decent range, though..
byte hue_right = abs(constrain(map(analogRead(right_channel), 0, 400, 0, 255), 0, 255)-200);
//Serial.println(hue_right);
//This is how I've been controlling the change in color to the LED strip..
if (hue_right != 0){
if (hue_right < 40){
analogWrite(GREENPIN, 200);
analogWrite(REDPIN, 200);
} else if (hue_right < 80){
analogWrite(GREENPIN, 200);
} else if (hue_right < 100){
analogWrite(BLUEPIN , 200);
} else if (hue_right < 140){
analogWrite(REDPIN, 160);
} else if (hue_right < 190) {
analogWrite(GREENPIN, 200);
} else if (hue_right < 220) {
analogWrite(REDPIN, 200);
analogWrite(BLUEPIN, 100);
}else {
analogWrite(REDPIN, 200);
}
}
//delay so it's not a constant strobe..
delay(60);
}
Link to the article by Jeremy Blum: Tutorial 14 for Arduino: Holiday Lights and Sounds – JeremyBlum.com
Holiday Lights_bb.pdf (138 KB)