Hello everyone, I need help with a sound level detector that I was trying to build.
The plan of the project was the following. I wanted a blue LED to light up when there is a low sound level, 3 green LED to light up when the sound level is medium, 4 yellow LED to light up when the sound level is high and 5 red LED to blink fast when the sound level is very high.
I am using LEDs, 220 ohm resistors to pull down and an Adafruit MAX4466 microphone with an Arduino Uno. Here is the schematic of the project.
My LED all light up and work fine but I can not get to pair the sound level with the colors in a good way.
I tried to do a code where I split the analog readout and that would open the blue LED from a range of 0 to 600. Then the green LED would light up from 600 to 800. Yellow LED from 800 to 900 and then red LED blinking over 900. Is the analog scale of 0 to 1023 for sound linear? It doesn't seem so to me.
Unfortunately it does not seem to work right. Whenever I make a loud noise and the red LED lights up, they do but the blue LED stays on while I would want it to turn off. I don't want to have many colors on at the same time, but rather only 1 color per range.
Here is the code I tried to do. If someone could help and tell my noob ass what I am doing wrong it would be appreciated
#define REDLED 13
#define REDLED2 12
#define REDLED3 11
#define REDLED4 10
#define REDLED5 9
#define YELLOWLED 8
#define YELLOWLED2 7
#define YELLOWLED3 6
#define YELLOWLED4 5
#define GREENLED 4
#define GREENLED2 3
#define GREENLED3 2
#define BLUELED 1
void setup()
{
pinMode(REDLED,OUTPUT);
pinMode(REDLED2,OUTPUT);
pinMode(REDLED3,OUTPUT);
pinMode(REDLED4,OUTPUT);
pinMode(REDLED5,OUTPUT);
pinMode(YELLOWLED,OUTPUT);
pinMode(YELLOWLED2,OUTPUT);
pinMode(YELLOWLED3,OUTPUT);
pinMode(YELLOWLED4,OUTPUT);
pinMode(GREENLED,OUTPUT);
pinMode(GREENLED2,OUTPUT);
pinMode(GREENLED3,OUTPUT);
pinMode(BLUELED,OUTPUT);
pinMode(A0,INPUT);
}
void loop()
{
int volume = analogRead(A0);
digitalWrite(BLUELED,LOW);
digitalWrite(GREENLED,LOW);
digitalWrite(GREENLED2,LOW);
digitalWrite(GREENLED3,LOW);
digitalWrite(YELLOWLED,LOW);
digitalWrite(YELLOWLED2,LOW);
digitalWrite(YELLOWLED3,LOW);
digitalWrite(YELLOWLED4,LOW);
digitalWrite(REDLED,LOW);
digitalWrite(REDLED2,LOW);
digitalWrite(REDLED3,LOW);
digitalWrite(REDLED4,LOW);
digitalWrite(REDLED5,LOW);
if (volume>=1 && volume<600){
digitalWrite(BLUELED,HIGH);
}
if (volume>=600 && volume<800){
digitalWrite(GREENLED,HIGH);
digitalWrite(GREENLED2,HIGH);
digitalWrite(GREENLED3,HIGH);
}
if (volume>=800 && volume<900){
digitalWrite(YELLOWLED,HIGH);
digitalWrite(YELLOWLED2,HIGH);
digitalWrite(YELLOWLED3,HIGH);
digitalWrite(YELLOWLED4,HIGH);
}
if (volume>=900)
{
digitalWrite(REDLED,HIGH);
digitalWrite(REDLED2,HIGH);
digitalWrite(REDLED3,HIGH);
digitalWrite(REDLED4,HIGH);
digitalWrite(REDLED5,HIGH);
delay(100);
digitalWrite(REDLED,LOW);
digitalWrite(REDLED2,LOW);
digitalWrite(REDLED3,LOW);
digitalWrite(REDLED4,LOW);
digitalWrite(REDLED5,LOW);
delay(100);
}
}