That is due to the the code you have written to sort out the levels. Read through it and you will see why.
In a high noise situation the Red LED will be on for the time it takes for the
By the way this line has nothing to do with stability I see this in a lot of beginners it is rubbish. But the delay used here defines the blinking rate that you see.
Because the last section of you code has a simple else and not an else if it is failing to register a low level.
// Pins for LEDs
const int redLED = 7; // Red LED pin
const int yellowLED = 6; // Yellow LED pin
const int greenLED = 2; // Green LED pin
// Pin for buzzer
const int buzzer = 3; // Buzzer pin
// Pin for sound sensor
const int analogPin = A0; // Analog output of the sound sensor
// Noise thresholds (adjust based on your environment)
const int greenThreshold = 5; // Low noise level
const int yellowThreshold = 10; // Moderate noise level
const int redThreshold = 15; // High noise level
void setup() {
// Set LED pins as OUTPUT
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Set buzzer pin as OUTPUT
pinMode(buzzer, OUTPUT);
Serial.begin(9600); // For debugging
}
void loop() {
// Read the sound level from the sound sensor
int soundLevel = analogRead(analogPin);
// Debugging: Print sound level to Serial Monitor
Serial.println(soundLevel);
// Control LEDs and buzzer based on sound level
if (soundLevel < greenThreshold) {
// Low noise: Green LED ON, others OFF
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else if (soundLevel < yellowThreshold) {
// Moderate noise: Yellow LED ON, others OFF
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else {
// High noise: Red LED ON, others OFF, Buzzer ON
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(buzzer, HIGH); // Buzzer ON
}
}
// Pins for LEDs
const int redLED = 7; // Red LED pin
const int yellowLED = 6; // Yellow LED pin
const int greenLED = 2; // Green LED pin
// Pin for buzzer
const int buzzer = 3; // Buzzer pin
// Pin for sound sensor
const int analogPin = A0; // Analog output of the sound sensor
// Noise thresholds (adjust based on your environment)
const int greenThreshold = 5; // Low noise level
const int yellowThreshold = 10; // Moderate noise level
const int redThreshold = 15; // High noise level
void setup() {
// Set LED pins as OUTPUT
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Set buzzer pin as OUTPUT
pinMode(buzzer, OUTPUT);
Serial.begin(9600); // For debugging
}
void loop() {
// Read the sound level from the sound sensor
int soundLevel = analogRead(analogPin);
// Debugging: Print sound level to Serial Monitor
Serial.println(soundLevel);
// Control LEDs and buzzer based on sound level
if (soundLevel < greenThreshold) {
// Low noise: Green LED ON, others OFF
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else if (soundLevel < yellowThreshold) {
// Moderate noise: Yellow LED ON, others OFF
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else if (soundLevel > redThreshold) {
// High noise: Red LED ON, others OFF, Buzzer ON
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(buzzer, HIGH); // Buzzer ON
}
}
// Control LEDs and buzzer based on sound level
if (soundLevel < greenThreshold) {
// Low noise: Green LED ON, others OFF
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else if (soundLevel < yellowThreshold && soundLevel > greenThreshold ) && {soundLevel < redThreshold) {
// Moderate noise: Yellow LED ON, others OFF
Serial.println(" in yellow section"); // see if you are in the yellow region
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else if (soundLevel > redThreshold) {
// High noise: Red LED ON, others OFF, Buzzer ON
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(buzzer, HIGH); // Buzzer ON
}
Looks like you have suddenly got a lot more voltage from your audio sensor. Probably due to an intermittent connection on the solderless bread board. Or something else is wrong with your wiring. Could you please take a much better photograph of your bread board and Arduino so we can see what goes where.
Does this change if you give a louder shout to the microphone?
Also is the red light coming on, it should be with an input of that size.