#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#include "pitches.h"
const int ROWS = 4; // Number of rows on the keypad
const int COLS = 4; // Number of columns on the keypad
int i = 0;
int buzzerPin = 1;
// Define the keypad pins
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 keypad row pins to these Arduino pins
byte colPins[COLS] = {A3, A2, A1, A0}; // Connect keypad column pins to these Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo myServo; // Create a Servo object to control the servo motor
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // Create an LCD object to display the password input and status
char password[] = "1234"; // Set the password
void setup() {
lcd.begin(16, 2); // Initialize the LCD
myServo.attach(6); // Attach the servo motor to pin 9
pinMode(buzzerPin, OUTPUT);
}
void loop() {
lcd.clear(); // Clear the LCD
lcd.print("Enter Password:"); // Display message on the LCD
delay(500);
int pitch = 1;
String enteredPassword = ""; // Initialize a string to hold the entered password
while (enteredPassword.length() < 4) { // Read input until the password length is 4
char key = keypad.getKey(); // Read a key from the keypad
if (key) { // If a key is pressed
enteredPassword += key;
lcd.setCursor(0, 1); // Move the cursor to the second line of the LCD
lcd.print(enteredPassword); // Print a '*' to indicate a key has been pressed
// Add the pressed key to the entered password
delay(100); // Delay to avoid reading multiple keys
}
}
//delay(500); // Delay to allow the user to release the last key
if (enteredPassword == password) { // If the entered password matches the set password
lcd.clear(); // Clear the LCD
lcd.print("Password Correct!"); // Display message on the LCD
myServo.write(0); // Open the door
delay(2000); // Wait for 2 seconds
myServo.write(90); // Close the door
} else { // If the entered password does not match the set password
lcd.clear(); // Clear the LCD
lcd.print("Password Incorrect!"); // Display message on the LCD
i++;
}
if (i>=3) { // If the entered password matches the set password
lcd.clear(); // Clear the LCD
lcd.print("Password inCorrect!"); // Display message on the LCD
lcd.setCursor(0, 1); // Move the cursor to the second line of the LCD
lcd.print("warning");
digitalWrite(buzzerPin, HIGH); // turn the buzzer on
delay(1000); // wait for 1 second
}
delay(2000); // Delay to allow the user to read the LCD message
}