// Constants for Ultrasonic Sensor
const int trigPin = 7;
const int echoPin = 6;
// Constants for PIR Motion Sensor
const int pirPin = 8;
// Constants for Buzzer
const int buzzerPin = 9;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Ultrasonic Sensor
long duration;
float distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// PIR Motion Sensor
int motion = digitalRead(pirPin);
if (motion == HIGH) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
Serial.println("Intruder detected!");
// Add any additional actions here, e.g., sending a notification
} else {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
// Print sensor data to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Motion: ");
Serial.println(motion == HIGH ? "Detected" : "Not Detected");
delay(500); // Adjust delay as needed
}