#include <Servo.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
// Initialize the servo on pin 6
Servo myservo;
int servoPin = 6;
// Initialize the PIR sensor on pin 13
int pirPin = 13;
int pirState = LOW;
// Initialize the LCD on pins 12, 11, 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {A0, A1, A2, A3}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Set up the servo
myservo.attach(servoPin);
myservo.write(0); // Initial position
// Set up the PIR sensor
pinMode(pirPin, INPUT);
// Set up the LCD
lcd.begin(16, 2);
lcd.print("Enter Code:");
}
void loop() {
pirState = digitalRead(pirPin);
if (pirState == HIGH) {
lcd.clear();
lcd.print("Enter Code:");
char key = keypad.getKey();
if (key) {
lcd.setCursor(0, 1);
lcd.print(key);
// Add your code here to handle the entered key
// For example, check if the entered code is correct
if (key == 'A') { // Example: 'A' is the correct code
myservo.write(90); // Unlock position
delay(500000); // Keep it unlocked for 5 seconds
myservo.write(0); // Lock position
}
}
} else {
lcd.clear();
lcd.print("No Motion");
}
}