#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 20 chars and 4 line display
// Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int lockPin = 10; // Output pin for the lock
const int alertPin = 11; // Output pin for the alert
const int emergencyPin = 12; // Output pin for the emergency signal
const String correctPasscode = "1234"; // Change this to your desired passcode
const String resetCode = "BAD9791"; // Change this to your desired reset code
String enteredPasscode = "";
int attempts = 0; // Track the number of attempts for passcode
int resetAttempts = 0; // Track the number of attempts for reset code
bool isLocked = false; // Track if the keypad is locked
bool emergencyLock = false; // Track if the system is in emergency lock
void setup() {
pinMode(lockPin, OUTPUT);
pinMode(alertPin, OUTPUT);
pinMode(emergencyPin, OUTPUT);
digitalWrite(lockPin, LOW); // Lock is initially locked
digitalWrite(alertPin, LOW); // Alert is initially off
digitalWrite(emergencyPin, LOW); // Emergency signal is initially off
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Enter Passcode:");
lcd.setCursor(0, 1);
}
void loop() {
if (isLocked || emergencyLock) {
handleLock();
return;
}
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Check if entered passcode is correct
if (enteredPasscode == correctPasscode) {
lcd.setCursor(0, 1);
lcd.print("ACCESS GRANTED ");
digitalWrite(lockPin, HIGH); // Unlock the lock
digitalWrite(alertPin, LOW); // Turn off the alert
attempts = 0; // Reset attempts on successful entry
} else {
attempts++;
if (attempts >= 3) {
lcd.setCursor(0, 0);
lcd.print("CALL MARVIN FOR HELP");
lcd.setCursor(0, 1);
lcd.print("KEYPAD IS LOCKED ");
isLocked = true; // Lock the keypad
} else {
lcd.setCursor(0, 1);
lcd.print("ACCESS DENIED ");
digitalWrite(lockPin, LOW); // Keep the lock locked
digitalWrite(alertPin, HIGH); // Turn on the alert
}
}
delay(2000); // Show the message for 2 seconds
enteredPasscode = ""; // Clear the entered passcode
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the input line
lcd.setCursor(0, 1);
} else if (key == '*') {
// Clear the entered passcode
enteredPasscode = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the input line
lcd.setCursor(0, 1);
} else {
// Append the key to the entered passcode
if (enteredPasscode.length() < 10) {
enteredPasscode += key;
lcd.print('*'); // Display an asterisk for each entered character
}
}
}
}
void handleLock() {
static unsigned long lastScroll = 0;
static int scrollPosition = 0;
const String message = "CALL MARVIN FOR HELP!!! ";
// Scroll the message every 500ms
if (millis() - lastScroll >= 500) {
lcd.setCursor(0, 0);
String displayMessage = message.substring(scrollPosition) + " " + message.substring(0, scrollPosition);
lcd.print(displayMessage.substring(0, 20)); // Display only the first 20 characters
scrollPosition = (scrollPosition + 1) % message.length();
lastScroll = millis();
}
lcd.setCursor(0, 1);
if (emergencyLock) {
lcd.print("EMERGENCY LOCK ");
} else {
lcd.print("KEYPAD IS LOCKED ");
}
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Check if entered passcode matches the reset code
if (enteredPasscode == resetCode) {
lcd.setCursor(0, 0);
lcd.print("Enter Passcode: ");
lcd.setCursor(0, 1);
lcd.print(" ");
isLocked = false; // Unlock the keypad
emergencyLock = false; // Clear the emergency lock
attempts = 0; // Reset the attempts
resetAttempts = 0; // Reset reset attempts
digitalWrite(alertPin, LOW); // Turn off the alert
digitalWrite(emergencyPin, LOW); // Turn off the emergency signal
} else {
resetAttempts++;
if (resetAttempts >= 3) {
digitalWrite(emergencyPin, HIGH); // Turn on the emergency signal
emergencyLock = true; // Set the emergency lock
}
}
enteredPasscode = ""; // Clear the entered passcode
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the input line
lcd.setCursor(0, 1);
} else if (key == '*') {
// Clear the entered passcode
enteredPasscode = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the input line
lcd.setCursor(0, 1);
} else {
// Append the key to the entered passcode
if (enteredPasscode.length() < 10) {
enteredPasscode += key;
lcd.print('*'); // Display an asterisk for each entered character
}
}
}
}