const int lightSensorPin = A0; // LDR connected to A0
const int soundSensorPin = A1; // Sound sensor connected to A1
const int ledPin = 7; // LED connected to pin 7
int lightSensorValue = 0; // Variable to store the LDR value
int soundSensorValue = 0; // Variable to store the sound sensor value
int lightThreshold = 300; // Threshold for LDR to detect darkness
int soundThreshold = 500; // Threshold value to simulate sound detection
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as an output
digitalWrite(ledPin, LOW); // Turn off the LED initially
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
lightSensorValue = analogRead(lightSensorPin); // Read the LDR value
soundSensorValue = analogRead(soundSensorPin); // Read the sound sensor value
// Display the light and sound values on the Serial Monitor for debugging
Serial.print("Light: ");
Serial.print(lightSensorValue); // Print light sensor value
Serial.print(" | Sound: ");
Serial.println(soundSensorValue); // Print sound sensor value
// Check if sound is detected at any time
if (soundSensorValue > soundThreshold) {
Serial.println("Don't Disturb!"); // Display message in serial
}
// Check if the room is dark and sound is not detected
else if (lightSensorValue > lightThreshold || soundSensorValue > soundThreshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED
}
// If no conditions are met (well-lit room and no sound)
else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(500); // Small delay for stability
}