Hello everyone
I had a question regarding some code for sound reactive LEDs
I am trying to get the audio input of a 3.5mm audio jack using Arduino's A0 analog in and ground to control a strip of RGB LEDs(SMD 5050s)
so far I have been able to control the color function of the led strip alone but I'm not sure how to get it to change with the audio input as I do not know what values to use, I have toyed around with someone else's code that is similar but uses a microphone instead of a 3.5mm jack input and figure id use the same A0 analog input to read the signal coming from the tip or first ring of the audio jack and adjusting the values but have not been able to get the full list of colors(written in the code) to show on the strip, I have specifically been playing with the values in these 2 lines of the code
filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4};
filteredSignal = (0.945 * filteredSignal) + (0.545* sensorSignal);
I have been able to get the color to change between the first 5 colors in the code
in the code, the signal is filtered using a First Order Low pass IIR Digital filter with a cutoff frequency of 900Hz, the filtered signal is compared with several threshold values that are used to determine which color will be displayed based on frequency off the sound that is sensed from the first ring of the audio jack, low frequency sounds (barely audible) are displayed as Blue in the LED strip, all the way to high frequency sounds(highly audible) which are displayed as Red in the strip
Here's the code
#define Rpin 11
#define Gpin 10
#define Bpin 9
#define delayLEDS 3
#define sensorPin A0
float sensorValue = 0, filteredSignal = 0,
filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4};
void setup () {
Serial.begin (9600);
}
void loop () {
MainFunction();
}
void MainFunction() {
sensorValue = (float) analogRead(sensorPin) * (5.0 / 1024.0);
FilterSignal(sensorValue);
Serial.print(sensorValue);
Serial.print(" ");
Serial.println(filteredSignal);
CompareSignalFiltered(filteredSignal);
}
void FilterSignal(float sensorSignal) {
filteredSignal = (0.945 * filteredSignal) + (0.0545* sensorSignal);
}
void CompareSignalFiltered(float filteredSignal) {
if (filteredSignal > filteredSignalValues[0]) {
RGBColor(0, 0, 255);
Serial.println("Blue");
} else if (filteredSignal <= filteredSignalValues[0] && filteredSignal > filteredSignalValues[1]) {
Serial.println("Azure");
RGBColor(0, 255, 255);
} else if (filteredSignal <= filteredSignalValues[1] && filteredSignal > filteredSignalValues[2]) {
RGBColor(0, 127, 255);
Serial.println("Cyan");
} else if (filteredSignal <= filteredSignalValues[2] && filteredSignal > filteredSignalValues[3]) {
RGBColor(0, 255, 127);
Serial.println("Aqua marine");
} else if (filteredSignal <= filteredSignalValues[3] && filteredSignal > filteredSignalValues[4]) {
RGBColor(0, 255, 0);
Serial.println("Green");
} else if (filteredSignal <= filteredSignalValues[4] && filteredSignal > filteredSignalValues[5]) {
RGBColor(255, 255, 0);
Serial.println("Yellow");
} else if (filteredSignal <= filteredSignalValues[5] && filteredSignal > filteredSignalValues[6]) {
RGBColor(255, 0, 255);
Serial.println("Magenta");
} else if (filteredSignal <= filteredSignalValues[6] && filteredSignal > filteredSignalValues[7]) {
RGBColor(255, 0, 127);
Serial.println("Rose");
} else if (filteredSignal <= filteredSignalValues[7] && filteredSignal > filteredSignalValues[8]) {
RGBColor(255, 127, 0);
Serial.println("Orange");
} else if (filteredSignal <= filteredSignalValues[8]) {
RGBColor(255, 0, 0);
Serial.println("Red");
} else {
RGBColor(0, 0, 255);
Serial.println("Default: Blue");
}
}
void RGBColor(int Rcolor, int Gcolor, int Bcolor) {
analogWrite(Rpin, Rcolor);
analogWrite(Gpin, Gcolor);
analogWrite(Bpin, Bcolor);
delay(delayLEDS);
}
I have attached a picture below with the Arduino pin layout and hardware I'm using
**the picture shows PNP transistors but I'm actually using TIP120 transistors in conjunction with 1k ohm resistors in my project
let me know if there is anything else I can include, TIA