// Electric Safe Lock System
// Components: 16x2 LCD, Servo Motor, 4x4 Keypad, Green LED, Red LED
// Functionality:
// 1. Enter 4-digit code; if correct -> "Success" + green LED + unlock (servo to 90° for 5s)
// if incorrect -> "Incorrect" + red LED
// 2. Press 'C' to change code: enter old code, then new code
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
// ----- PIN DEFINITIONS -----
// LCD pins: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);
// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 9, 10, 11, 12 }; // Connect to the row pinouts of the keypad
byte colPins[COLS] = { 13, A0, A1, A2 }; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Servo setup
Servo lockServo;
const int servoPin = 6;
// LED pins
const int greenLedPin = A3;
const int redLedPin = A4;
// Code variables
String code = "1234"; // Default 4-digit code
String inputCode = "";
const int codeLength = 4;
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Enter Code:");
// Initialize LEDs
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, LOW);
// Initialize Servo
lockServo.attach(servoPin);
lockServo.write(0); // Locked position set the angle as 0
}
void loop() {
char key = keypad.getKey();
if (key) {
// If user wants to change code press 'C'
if (key == 'C') {
changeCode();
return;
}
if (isDigit(key)) { // If numeric key, add to input
inputCode += key;
lcd.setCursor(inputCode.length() - 1, 1);
lcd.print('*'); // Mask input
}
if (inputCode.length() >= codeLength) { // If input length reached, check code
checkCode();
}
}
}
// Function to check entered code
void checkCode() {
lcd.clear();
if (inputCode == code) {
// Correct code
lcd.print("Success");
digitalWrite(greenLedPin, HIGH);
// Unlock
lockServo.write(90);
delay(5000); // Keep unlocked for 5 seconds
lockServo.write(0); // Lock back
digitalWrite(greenLedPin, LOW);
} else {
// Incorrect code
lcd.print("Incorrect");
digitalWrite(redLedPin, HIGH);
delay(3000);
digitalWrite(redLedPin, LOW);
}
// Reset for next attempt
inputCode = "";
delay(500);
lcd.clear();
lcd.print("Enter Code:");
}
// Function to change the current code
void changeCode() {
String oldCode = "";
String newCode = "";
lcd.clear();
lcd.print("Enter Old Code:");
oldCode = readCodeEntry();
if (oldCode == code) {
lcd.clear();
lcd.print("Enter New Code:");
newCode = readCodeEntry();
code = newCode; // Update code
lcd.clear();
lcd.print("Code Changed");
delay(2000);
} else {
lcd.clear();
lcd.print("Wrong Old Code");
delay(2000);
}
// Reset display
inputCode = "";
lcd.clear();
lcd.print("Enter Code:");
}
// Helper to read a fixed-length code from keypad
String readCodeEntry() {
String entry = "";
while (entry.length() < codeLength) {
char k = keypad.getKey();
if (k && isDigit(k)) {
entry += k;
lcd.setCursor(entry.length() - 1, 1);
lcd.print('*');
}
}
delay(500);
return entry;
}