#include <Keypad.h>
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Buzzer.h> // Include the Tone library for buzzer
// Define constants for better readability and maintainability
const int PASSWORD_LENGTH = 5;
const int BUZZER_PIN = 10; // Define the buzzer pin
const int BUZZER_FREQUENCY = 1500; // Define the buzzer frequency
const int MAX_WRONG_ATTEMPTS = 5; // Maximum allowed wrong attempts before locking
const int LOCK_DURATION = 30000; // Lock duration in milliseconds (30 seconds)
// Initialize LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Declare variables
char password[PASSWORD_LENGTH];
char initial_password[PASSWORD_LENGTH], new_password[PASSWORD_LENGTH];
int vcc = 11;
int i = 0;
int relay_pin = 11;
char key_pressed = 0;
const byte rows = 4;
const byte columns = 4;
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
byte row_pins[rows] = {2, 3, 4, 5};
byte column_pins[columns] = {6, 7, 8, 9};
Keypad keypad_key = Keypad(makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
int wrong_attempts = 0; // Counter for wrong attempts
unsigned long lock_start_time = 0; // Timestamp to track lock start time
bool device_locked = false; // Flag to indicate device lock state
// Setup function
void setup()
{
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
// Set pin modes
pinMode(relay_pin, OUTPUT);
pinMode(vcc, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
// Display startup message
lcd.print(" WELCOME");
lcd.setCursor(0, 1);
delay(3000);
lcd.clear();
// Prompt user to enter password
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
initialpassword();
}
// Main loop function
void loop()
{
digitalWrite(relay_pin, HIGH);
// Check if '#' key is pressed to initiate password change
key_pressed = keypad_key.getKey();
if (key_pressed == '#' && !device_locked)
change();
// Check for keypad input
if (key_pressed && !device_locked)
{
// Mask the password input with '*'
password[i++] = key_pressed;
lcd.print('*');
}
// Check if password length is reached and device not locked
if (i == PASSWORD_LENGTH && !device_locked)
{
delay(200);
// Read initial password from EEPROM
for (int j = 0; j < PASSWORD_LENGTH; j++)
initial_password[j] = EEPROM.read(j);
// Check if entered password matches initial password
if (!(strncmp(password, initial_password, PASSWORD_LENGTH)))
{
lcd.clear();
lcd.print("Pass Accepted");
digitalWrite(relay_pin, LOW);
buzzOnce(); // Buzz once for correct password
delay(2000);
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
i = 0;
wrong_attempts = 0; // Reset wrong attempts counter
}
else
{
// Increment wrong attempts counter
wrong_attempts++;
// Check if maximum wrong attempts reached
if (wrong_attempts >= MAX_WRONG_ATTEMPTS)
{
device_locked = true;
lock_start_time = millis(); // Record lock start time
}
digitalWrite(relay_pin, HIGH);
lcd.clear();
lcd.print("Wrong Password");
buzzEscapeAlarm(); // Buzz like an escape alarm for wrong password
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
i = 0;
}
}
// Check if device is locked
if (device_locked)
{
// Check if lock duration has passed
if (millis() - lock_start_time >= LOCK_DURATION)
{
device_locked = false; // Unlock the device
lcd.clear();
lcd.print("Device Unlocked");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
}
else
{
lcd.clear();
lcd.print("Device in Lock");
lcd.setCursor(0, 1);
lcd.print("Wait for 30 secs");
delay(500);
}
}
}
// Function to change password
void change()
{
int j = 0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(0, 1);
while (j < PASSWORD_LENGTH)
{
char key = keypad_key.getKey();
if (key)
{
new_password[j++] = key;
lcd.print('*'); // Mask the password input with '*'
}
key = 0;
}
delay(500);
// Check if entered current password matches initial password
if ((strncmp(new_password, initial_password, PASSWORD_LENGTH)))
{
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0, 1);
lcd.print("Try Again");
delay(1000);
}
else
{
j = 0;
lcd.clear();
lcd.print("New Password:");
lcd.setCursor(0, 1);
while (j < PASSWORD_LENGTH)
{
char key = keypad_key.getKey();
if (key)
{
initial_password[j] = key;
lcd.print('*'); // Mask the password input with '*'
EEPROM.write(j, key);
j++;
}
}
lcd.print("Pass Changed");
delay(1000);
}
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
key_pressed = 0;
}
// Function to initialize password from EEPROM
void initialpassword()
{
for (int j = 0; j < PASSWORD_LENGTH; j++)
EEPROM.write(j, j + 49);
for (int j = 0; j < PASSWORD_LENGTH; j++)
initial_password[j] = EEPROM.read(j);
}
// Function to make the buzzer sound once
void buzzOnce()
{
tone(BUZZER_PIN, 1000, 100); // Buzz at 1000 Hz for 100 ms
}
// Function to make the buzzer sound like an escape alarm
void buzzEscapeAlarm()
{
for (int i = 0; i < 5; i++)
{
tone(BUZZER_PIN, BUZZER_FREQUENCY, 200); // Buzz at specified frequency for 200 ms
delay(300); // Wait for 300 ms between each buzz
}
}