#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Initialize the LCD with I2C address 0x27 (common address for 20x4 LCDs)
LiquidCrystal_I2C lcd (0x27, 20,4);
// Define the 4x4 keypad
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char* correctPassword = "BD#07";
char enteredPassword[6];
byte passwordIndex = 0;
const int relayPin = 11;
unsigned long doorOpenTime = 0;
bool doorIsOpen = false;
void setup() {
lcd.begin(20,4);
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print (F("# CP BANG CO,. LTD #"));
delay(1000);
lcd.setCursor (5,1);
lcd.print (F("MAKING BY"));
delay(1000);
lcd.setCursor (2,2);
lcd.print (F(" ARIFUL ISLAM"));
delay(1000);
lcd.setCursor (0,3);
lcd.print (F("MAINTENANCE ENGINEER"));
delay(2000);
lcd.clear();
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is off
}
void loop() {
lcd.setCursor(0,0);
lcd.print (F("# CP BANG CO,. LTD #"));
lcd.setCursor(0,1);
lcd.print (F(" VALUKA 7 (PULLET) "));
lcd.setCursor(0, 2);
lcd.print(" Enter Password: ");
char key = keypad.getKey();
if (key) {
if (key == '*') {
// Reset password entry
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
lcd.clear();
lcd.setCursor(0, 2);
lcd.print(" Enter Password: ");
} else {
// Append the entered key to the password buffer
if (passwordIndex < sizeof(enteredPassword) - 1) {
enteredPassword[passwordIndex] = key;
passwordIndex++;
lcd.setCursor(passwordIndex, 3);
lcd.print('*'); // Show asterisks for entered keys
// Check if the entered password is complete and correct
if (passwordIndex == 5) {
enteredPassword[passwordIndex] = '\0';
if (strcmp(enteredPassword, correctPassword) == 0) {
if (doorIsOpen) {
// Close the door if it was open
digitalWrite(relayPin, LOW);
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Door Closed");
doorIsOpen = false;
} else {
// Open the door if it was closed
digitalWrite(relayPin, HIGH);
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Door Opened");
doorOpenTime = millis();
doorIsOpen = true;
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect Password");
}
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
}
}
}
// Close the door automatically after 1 minute
if (doorIsOpen && (millis() - doorOpenTime >= 60000)) {
digitalWrite(relayPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Closed");
doorIsOpen = false;
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
}