#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
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 relayPin = 13; // Connect the relay to pin 13
// Define your passcodes and corresponding messages
const char passcodes[][6] = {"70", "0704", "5678"}; // Change this to your desired passcodes
const char messages[][20] = {"\n1879", "\nCyber", "\nother"}; // Messages corresponding to passcodes
char enteredCode[6]; // Buffer to store entered code
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address to 0x27, 16 characters and 2 lines
void setup()
{
pinMode(relayPin, OUTPUT); // Set the relay pin as output
Serial.begin(9600); // Initialize serial communication for debugging
// Initialize LCD...some more
lcd.init();
lcd.backlight();
lcd.print("Enter Passcode:");
Serial.println("Enter Passcode:");
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
}
void loop()
{
char key = keypad.getKey();
if (key)
{
if (key == '#') // If # key is pressed, check the passcode
{
if (strlen(enteredCode) >= 2) // Check if passcode length is at least 2
{
enteredCode[strlen(enteredCode)] = '\0'; // Null terminate the string
bool passcodeMatched = false;
for (int i = 0; i < sizeof(passcodes) / sizeof(passcodes[0]); i++)
{
if (strcmp(enteredCode, passcodes[i]) == 0) // If the entered passcode matches one of the stored passcodes, activate the relay
{
digitalWrite(relayPin, HIGH);
lcd.clear();
centerText(messages[i]); //lcd.print(messages[i]); // Print corresponding message
Serial.println(messages[i]); // Print corresponding message to Serial monitor
delay(10000); // Delay to keep the relay activated for a short duration
digitalWrite(relayPin, LOW); // Deactivate the relay after delay
lcd.clear();
lcd.print("Enter Passcode:");
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
Serial.println("\nEnter Passcode:");
passcodeMatched = true;
break;
}
}
if (!passcodeMatched)
{
lcd.clear();
lcd.print("Access Denied");
Serial.println("\nAccess Denied");
delay(1000);
lcd.clear();
lcd.print("Enter Passcode:"); // Display prompt for next passcode entry
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
Serial.println("\nEnter Passcode:");
}
memset(enteredCode, 0, sizeof(enteredCode)); // Clear the entered code buffer
}
else
{
lcd.clear();
lcd.print("Invalid Passcode");
Serial.println("\nInvalid Passcode");
delay(1000);
lcd.clear();
lcd.print("Enter Passcode:"); // Display prompt for next passcode entry
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
Serial.println("\nEnter Passcode:");
}
}
else if (strlen(enteredCode) < sizeof(enteredCode) - 1) // If less than the buffer size
{
lcd.print(key); // Print the pressed key on LCD
Serial.print(key); // Print the pressed key to Serial monitor
enteredCode[strlen(enteredCode)] = key; // Store the pressed key
lcd.setCursor(strlen(enteredCode), 1); // Move cursor to the end of entered characters on the second line
}
}
}
void centerText(const char* text)
{
int len = strlen(text);
int startPos = (16 - len) / 2;
lcd.setCursor(startPos, 0);
lcd.print(text);
for (int i = 0; i < 5; i++)
{
lcd.setCursor(startPos, 0);
lcd.print(" ");
delay(200);
lcd.setCursor(startPos, 0);
lcd.print(text);
delay(200);
}
}