int ldrPin = A0; // LDR connected to analog pin A0
int redLedPin = 12; // Red LED connected to digital pin 12
int greenLedPin = 13; // Green LED connected to digital pin 13
int buzzerPin = 11; // Buzzer connected to digital pin 11 (or any other PWM pin)
void setup() {
pinMode(redLedPin, OUTPUT); // Set the red LED pin as output
pinMode(greenLedPin, OUTPUT); // Set the green LED pin as output
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
pinMode(ldrPin, INPUT); // Set the LDR pin as input
Serial.begin(9600); // Begin serial communication at 9600 baud
}
void loop() {
int sensorValue = analogRead(ldrPin); // Read the value from the LDR
// Print the sensor value for debugging
Serial.print("LDR Value: ");
Serial.println(sensorValue);
if (sensorValue < 500) { // If the light level is low
digitalWrite(redLedPin, HIGH); // Turn on the red LED
digitalWrite(greenLedPin, LOW); // Turn off the green LED
tone(buzzerPin, 1000); // Play a 1kHz tone on the buzzer
Serial.println(" Something Detected.");
} else { // If the light level is high
digitalWrite(redLedPin, LOW); // Turn off the red LED
digitalWrite(greenLedPin, HIGH); // Turn on the green LED
noTone(buzzerPin); // Stop the buzzer sound
Serial.println(" Nothing detected.");
}
delay(1000); // Wait for 1 second before reading the sensor again
}