Classroom noise traffic light

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.

1 Like

I removed the delay, but both red and green leds light up at the same time

Yes they will do if you remove the delay. Have you changed the code like I said in post #21 ?

1 Like


its probably bc the of the sound sensor

what should I change in this

// 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
  }


}

Try

   digitalWrite(buzzer, LOW); // Buzzer OFF
  } else if (soundLevel  > redThreshold) {
1 Like

none of the leds are lighting up

Can you post our code again with the latest code you are running please.

1 Like
// 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
  }


}

Try this

 // 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
  }

Post#9 shows that you haven't used the breadboard correctly.
All pins of the module and all wiring is shorted together.
What is really going on.
Leo..

there seems to be an error with the code

C:\Users\jyd\Documents\Arduino\classroom_noise_trffclght\classroom_noise_trffclght.ino: In function 'void loop()':
C:\Users\jyd\Documents\Arduino\classroom_noise_trffclght\classroom_noise_trffclght.ino:43:80: error: expected identifier before '{' token
} else if (soundLevel < yellowThreshold && soundLevel > greenThreshold ) && {soundLevel < redThreshold) {
^
C:\Users\jyd\Documents\Arduino\classroom_noise_trffclght\classroom_noise_trffclght.ino:56:3: error: expected '}' at end of input
}
^

exit status 1

Compilation error: expected identifier before '{' token

how do I correct the wiring?

Sorry that line should be:-


  } else if (soundLevel < yellowThreshold && soundLevel > greenThreshold && soundLevel < redThreshold) {
1 Like

im receiving this in my serial monitor

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.

1 Like

Sorry too dark, too blurred and too crowded to see what is going on.

Try better lighting and a few different angles.

1 Like

Wanted to post a link to a breadboard tutorial, but it seems you have already worked it out (post#37). Google that anyway.
Leo..