int Buzzer = 6; // Pin for the Buzzer
int ledPin = 5; // Pin for the LED
int inputPin = 2; // Pin for PIR sensor
int val = 0; // Variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // Declare LED as output
pinMode(Buzzer, OUTPUT); // Declare Buzzer as output
pinMode(inputPin, INPUT); // Declare sensor as input
Serial.begin(9600); // Initialize serial communication
}
void loop() {
val = digitalRead(inputPin); // Read input value
int value_ldr = analogRead(A0); // Read LDR value
Serial.println("PIR VALUE:" + String(value_ldr));
delay(500);
if ((value_ldr <= 250) && (val == HIGH)) {
digitalWrite(ledPin, HIGH); // Turn LED ON
digitalWrite(Buzzer, HIGH); // Turn Buzzer ON
Serial.println("Motion detected!");
} else if ((value_ldr >= 250) && (val == HIGH)){
digitalWrite(ledPin, LOW); // Turn LED OFF
digitalWrite(Buzzer, HIGH); // Buzzer is ON
Serial.println("Motion Detected!");
}
else {
digitalWrite(ledPin, LOW);
digitalWrite(Buzzer, LOW);
Serial.println("Motion Ended.");
}
}