#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
#define red 13
#define green 12
// Creating Servo object
Servo servo;
// Defining keypad keys
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Defining pins for the keypad
uint8_t colPins[COLS] = {5, 4, 3, 2};
uint8_t rowPins[ROWS] = {9, 8, 7, 6};
// Creating Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Creating LiquidCrystal_I2C object
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Defining delays for code checking and message display
const int CHECK_DELAY = 500; // Adjust delay based on typing speed
const int MESSAGE_DELAY = 2000; // Adjust delay for message display
// Strings to store entered code and message
String phrase = "";
String message = "";
// Function to unlock the servo
void unlock() {
servo.write(SERVO_LOCK_POS);
}
// Function to lock the servo
void lock() {
servo.write(SERVO_UNLOCK_POS);
}
void setup() {
// Initializing the LCD screen and turning on the backlight
lcd.init();
lcd.backlight();
// Attaching the servo to pin 10
servo.attach(10);
// Initializing serial communication
Serial.begin(115200);
// Configuring pins for LEDs (red and green)
pinMode(red, OUTPUT);
pinMode(green , OUTPUT);
}
void loop() {
// Introducing a delay at the beginning of each loop iteration
delay(50);
// Getting the pressed key on the keypad
char key = keypad.getKey();
// If a key is pressed
if (key != NO_KEY) {
// Adding the key to the phrase string
phrase += key;
// Clearing the LCD screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(phrase);
// If the length of the phrase exceeds the first line of the LCD
if (phrase.length() > LCD_COLUMNS) {
lcd.setCursor(0, 1);
lcd.print(phrase.substring(LCD_COLUMNS, 2 * LCD_COLUMNS));
}
// If the length of the phrase reaches 4 characters
if (phrase.length() == 4) {
// Introducing a delay for code checking
delay(CHECK_DELAY);
// Checking the entered code
if (phrase == "0000") {
// If the code is correct, display a welcome message, unlock the servo, and turn on the green LED
message = "Welcome!";
unlock();
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
} else {
// If the code is incorrect, display an error message, lock the servo, and turn on the red LED
message = "Incorrect code";
lock();
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
// Introducing a delay before clearing the phrase and message
delay(MESSAGE_DELAY);
// Clearing the phrase and message
phrase = "";
message = "";
// Clearing the LCD screen
lcd.clear();
}
}
// If a message is ready to be displayed
if (message.length() > 0) {
// Displaying the message on the second line of the LCD
lcd.setCursor(0, 1);
lcd.print(message);
// Introducing a delay for message display
delay(MESSAGE_DELAY);
// Clearing the phrase and message
phrase = "";
message = "";
// Clearing the LCD screen
lcd.clear();
}
}