/*
SECURITY SYSTEM
18 SEP 2025
URVASHIBA GOHIL
RAHUL MAKWANA
*/
#include <Keypad.h>
#include <LiquidCrystal.h>
// lcd pins
LiquidCrystal lcd(12,11,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] = {14,15,16,17};
byte colPins[COLS] = {18,19,20,21};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// leds instead of servo
#define greenLed A0
#define redLed A1
#define buzzer A2 // buzzer pin
// password storage
char password[5] = "1234"; // default pass
char entered[5]; // what user enters
int currentPosition = 0; // keep track of digits
// flag for secure or visible input
bool secureMode = true;
void setup() {
lcd.begin(16, 2);
lcd.print(" ENTER PASSWORD ");
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW); // buzzer off initially
}
void loop() {
char key = keypad.getKey();
if (key) {
// if buzzer was on -> stop buzzer when any key pressed
if (digitalRead(buzzer) == HIGH)
{
digitalWrite(buzzer, LOW);
lcd.clear();
lcd.print(" ENTER PASSWORD ");
currentPosition = 0;
return;
}
// check if * is long pressed -> toggle secure/visible
if (key == '*')
{
unsigned long startTime = millis();
while (keypad.getKey() == '*')
{
if (millis() - startTime > 1000)
{ // 1 sec hold
secureMode = !secureMode;
lcd.clear();
lcd.setCursor(0, 0);
if (secureMode) lcd.print(" Secure Mode ");
else lcd.print(" Visible Mode ");
delay(800);
lcd.clear();
lcd.print(" ENTER PASSWORD ");
currentPosition = 0;
return;
}
}
return; // short press * does nothing
}
// if # pressed -> go change password
if (key == '#')
{
changePassword();
return;
}
// if number pressed
if (key >= '0' && key <= '9')
{
entered[currentPosition] = key;
lcd.setCursor(currentPosition, 1);
if (secureMode) lcd.print('*'); // show * if secure
else lcd.print(key); // else show digit
currentPosition++;
// when 4 digits typed
if (currentPosition == 4)
{
entered[4] = '\0'; // terminate string
delay(200);
// check password
if (!strcmp(entered, password))
{
// if correct
lcd.clear();
lcd.print("Door is Unlocked");
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
// keep unlocked for 10 sec
for (int sec = 9; sec>0;sec--)
{
lcd.setCursor(0, 1);
lcd.print("Locking in: ");
lcd.print(sec);
delay(1000);
}
// auto lock again
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
lcd.clear();
lcd.print("Door is Locked");
delay(2000);
}
else {
// if wrong password
lcd.clear();
lcd.print(" Wrong Password ");
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(buzzer, HIGH); // buzzer ON
// buzzer will stay ON until next key is pressed
}
// reset input buffer
currentPosition = 0;
}
}
}
}
// change password routine
void changePassword()
{
char oldPass[5];
int i = 0;
lcd.clear();
lcd.print("Old Password:");
// first ask for old password
while (i < 4) {
char k = keypad.getKey();
if (k && k >= '0' && k <= '9')
{
oldPass[i] = k;
lcd.setCursor(i, 1);
lcd.print('*');
i++;
}
}
oldPass[4] = '\0';
// if old password wrong
if (strcmp(oldPass, password) != 0)
{
lcd.clear();
lcd.print(" Wrong Old Pass ");
delay(2000);
lcd.clear();
lcd.print(" ENTER PASSWORD ");
currentPosition = 0;
return;
}
// enter new password
lcd.clear();
lcd.print("New Password:");
i = 0;
while (i < 4) {
char k = keypad.getKey();
if (k && k >= '0' && k <= '9')
{
password[i] = k;
lcd.setCursor(i, 1);
lcd.print('*');
i++;
}
}
password[4] = '\0'; // save new pass
lcd.clear();
lcd.print(" Password Saved ");
delay(2000);
lcd.clear();
lcd.print(" ENTER PASSWORD ");
currentPosition = 0;
}