#include <Servo.h>
const int ledPin = 13; // LED connected to digital pin 13
const int buzzerPin = 8; // Buzzer connected to digital pin 8
const int servoPin = 9; // Servo motor connected to digital pin 9
const int motionSensorPin = 2; // Motion sensor connected to digital pin 2
Servo lockServo; // Create a servo object
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(motionSensorPin, INPUT);
lockServo.attach(servoPin);
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Check for motion detection
bool motionDetected = digitalRead(motionSensorPin);
if (motionDetected) {
// Motion detected, trigger security system
digitalWrite(buzzerPin, HIGH);
lockDoor();
Serial.println("Motion Detected!");
blinkLED(3); // Blink LED 3 times
// Add feedback for servo motor movement
moveServo(90); // Move the servo to 90 degrees
delay(1000); // Wait for a second
moveServo(0); // Move the servo back to 0 degrees
buzzerTone(1000); // Generate a buzzer tone for 1 second
} else {
// No motion, deactivate security system
digitalWrite(buzzerPin, LOW);
unlockDoor();
Serial.println("No Motion Detected.");
}
// Add some delay here to avoid rapid checks, adjust as needed
delay(1000);
}
void lockDoor() {
// Rotate the servo motor to lock the door position
lockServo.write(90); // Adjust the angle as needed for your door lock mechanism
}
void unlockDoor() {
// Rotate the servo motor to unlock the door position
lockServo.write(0); // Adjust the angle as needed for your door lock mechanism
}
void blinkLED(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(ledPin, HIGH); // Turn on LED
delay(500); // Wait
digitalWrite(ledPin, LOW); // Turn off LED
delay(500); // Wait
}
}
void moveServo(int angle) {
lockServo.write(angle); // Move the servo to the specified angle
}
void buzzerTone(int toneDuration) {
// Generate a tone with the buzzer
tone(buzzerPin, 1000); // You can adjust the frequency as needed
delay(toneDuration); // Adjust the duration of the tone
noTone(buzzerPin); // Turn off the tone
}