#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define ROWS 4
#define COLS 4
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};
byte colPins[COLS] = {5, 4, 3, 2};
int ledPin1 = 12;
int ledPin2 = 11;
int ledPin3 = 10;
int buzzerPin = 13;
int relayPin = A0; // Use A0 as the relay pin
char password[] = "1234"; // Change this to your desired password
char enteredPassword[5]; // The entered password will be stored here
int passwordIndex = 0;
int wrongAttempts = 0; // Counter for wrong attempts
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.print(" Welcome!");
delay(1000);
lcd.clear();
lcd.print(" Enter Password");
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT); // Set the relay pin as output
for (int i = 0; i < ROWS; i++) {
pinMode(rowPins[i], INPUT_PULLUP);
}
for (int i = 0; i < COLS; i++) {
pinMode(colPins[i], OUTPUT);
digitalWrite(colPins[i], HIGH);
}
}
void loop() {
char key = getKey();
if (key != '\0') {
if (key == 'C') { // Clear all typed passwords
lcd.clear();
lcd.print(" Enter Password");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear entered password
} else if (key == 'D') { // Delete the previous typed letter
if (passwordIndex > 0) {
passwordIndex--;
lcd.setCursor(passwordIndex, 0);
lcd.print(" ");
}
} else if (key == 'A') { // Reset
lcd.clear();
lcd.print(" Enter Password");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear entered password
digitalWrite(ledPin1, LOW); // Turn off all LEDs
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
} else {
if (passwordIndex == 0) {
lcd.clear(); // Clear "Enter Password" message when typing the password
int startPos = (16 - strlen(password)) / 2; // Calculate starting position
lcd.setCursor(startPos, 0);
}
enteredPassword[passwordIndex] = key;
lcd.print("*");
passwordIndex++;
}
if (passwordIndex == 4) {
if (strcmp(enteredPassword, password) == 0) {
lcd.clear();
lcd.print(" Access Granted!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Good Day");
lcd.setCursor(0, 1);
animateText(" MASTER RAM"); // Call the function to animate text
digitalWrite(buzzerPin, HIGH); // Activate buzzer
digitalWrite(relayPin, HIGH); // Activate relay to turn on the light
delay(5000); // Buzz and light up for 5 seconds
digitalWrite(buzzerPin, LOW); // Turn off buzzer
digitalWrite(relayPin, LOW); // Turn off relay
} else {
lcd.clear();
lcd.setCursor (0,0);
lcd.print (" 3");
delay (1000);
lcd.clear();
lcd.print (" 2");
delay (1000);
lcd.clear();
lcd.print (" 1");
delay (1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" RUN"); // Display "Error" message for wrong password
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Enter Password");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear entered password
// Light up LEDs based on wrong attempts
if (wrongAttempts == 0) {
digitalWrite(ledPin1, HIGH);
} else if (wrongAttempts == 1) {
digitalWrite(ledPin2, HIGH);
} else if (wrongAttempts == 2) {
digitalWrite(ledPin3, HIGH);
// Activate siren-like buzzer
while (true) {
digitalWrite(buzzerPin, HIGH); // Activate buzzer
delay(100); // Beep duration
digitalWrite(buzzerPin, LOW); // Turn off buzzer
delay(100); // Silence duration
if (getKey() == 'A') { // Check for reset condition
break;
}
}
wrongAttempts = -1; // Reset wrong attempts counter after siren mode
}
wrongAttempts++;
}
}
}
}
char getKey() {
for (int col = 0; col < COLS; col++) {
digitalWrite(colPins[col], LOW);
for (int row = 0; row < ROWS; row++) {
if (digitalRead(rowPins[row]) == LOW) {
delay(50); // Debounce delay
if (digitalRead(rowPins[row]) == LOW) {
digitalWrite(colPins[col], HIGH);
return keys[row][col];
}
}
}
digitalWrite(colPins[col], HIGH);
}
return '\0';
}
// Function to animate text from left to right
void animateText(const char* text) {
int textLength = strlen(text);
for (int i = 0; i < textLength; i++) {
lcd.setCursor(i, 1);
lcd.print(text[i]);
delay(200); // Adjust delay for animation speed
}
}