#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
Servo myServo; // Create a servo object
const int servoPin = 11; // Pin connected to the servo signal wire
// Password Length
const int Password_Length = 4;
// Character to hold password input
String Data;
// Password
String Master = "1234";
// degree of the servo on correct password state
int correct_degree = 90;
// degree of the servo on correct password state
int incorrect_degree = 0;
// Counter for character entries
byte data_count = 0;
// Character to hold key input
char customKey;
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Create LCD object
LiquidCrystal lcd(13, 12, A0, A1, A2, A3);
void setup() {
// Setup LCD with begin
lcd.begin(16, 2);
myServo.attach(servoPin); // Attach the servo to the specified pin
// Set lockOutput as an OUTPUT pin (relay)
myServo.write(0); // Move the servo directly to 0 degrees
}
void loop() {
// Initialize LCD and print
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Check for keypress
customKey = customKeypad.getKey();
// Handle keypress if valid
if (customKey && data_count < Password_Length) {
// Enter keypress into array and increment counter
Data += customKey;
lcd.setCursor(data_count, 1);
lcd.print('*'); // Display asterisks for privacy
data_count++;
}
// Check if password entered and "#" pressed
if (customKey == '#' && data_count == Password_Length) {
lcd.clear();
if (Data == Master) {
// Correct Password
lcd.print("Correct: ");
lcd.setCursor(0, 1);
lcd.print(Master); // Display the correct password,
// Turn on relay for 5 seconds
myServo.write(correct_degree); // Move the servo to a specific degrees
delay(5000);
myServo.write(incorrect_degree); // Move the servo to a specific degrees
} else {
// Incorrect Password
lcd.print("Incorrect");
delay(1000);
}
// Clear data and LCD display
lcd.clear();
clearData();
}
}
void clearData() {
// Reset data_count
data_count = 0;
// Reset Data
Data = "";
}