#define MIC_PIN A0 // Define the pin for the microphone sensor
#define buzzerPin 7 // Define the pin for the warning buzzer
int threshold = 500; // Set the noise threshold for triggering the buzzer
int duration = 1000; // Set the duration of the sound in milliseconds
int pause = 250; // Set the pause between the sounds in milliseconds
void setup() {
Serial.begin(9600); // Initialize the serial monitor
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
}
void loop() {
float noiseLevel = analogRead(MIC_PIN); // Read the noise level from the microphone sensor
Serial.print("Noise level: ");
Serial.println(noiseLevel);
if (noiseLevel > threshold) { // Check if the noise level exceeds the threshold
digitalWrite(buzzerPin, HIGH); // Trigger the buzzer
Serial.println("Warning: High Noise level!");
// Play the emergency alarm sound
tone(buzzerPin, 880, duration); // 880Hz tone for 0.5 second
delay(pause); // Pause for 0.25 second
tone(buzzerPin, 1760, duration); // 1760Hz tone for 0.5 second
delay(pause); // Pause for 0.25 second
tone(buzzerPin, 880, duration); // 880Hz tone for 0.5 second
delay(pause); // Pause for 0.25 second
tone(buzzerPin, 1760, duration); // 1760Hz tone for 0.5 second
delay(pause); // Pause for 0.25 second
// Wait for one second before playing the sound again
delay(1000); // 1000 milliseconds = 1 second
}
else {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
delay(1000); // Wait for 1 second before taking the next reading
}