#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Preferences.h>
// Keypad setup
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] = {13, 14, 27, 26};
byte colPins[COLS] = {25, 33, 32, 23};
// Create keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Create LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Create RTC object
RTC_DS1307 rtc;
// Create storage object
Preferences preferences;
// Output pins
const int RELAY_PIN = 19;
const int LED_PIN = 18;
const int BUZZER_PIN = 5;
// Security settings
String correctPIN = "1234";
String enteredPIN = "";
int wrongAttempts = 0;
const int MAX_ATTEMPTS = 3;
const unsigned long LOCK_TIME_SECONDS = 30;
uint32_t lockoutEndTime = 0;
// System status
bool isLocked = false;
unsigned long lockStartTime = 0;
bool doorUnlocked = false;
unsigned long doorUnlockStart = 0;
void showMessage(String line1, String line2)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
void unlockDoor()
{
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
doorUnlocked = true;
doorUnlockStart = millis();
showMessage("Access Granted", "Door Unlocked");
}
void checkDoorTimer()
{
if (doorUnlocked && millis() - doorUnlockStart >= 5000)
{
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
doorUnlocked = false;
showMessage("Door Locked", "Enter PIN:");
}
}
void wrongPIN()
{
wrongAttempts++;
preferences.putInt("attempts", wrongAttempts);
digitalWrite(BUZZER_PIN, HIGH);
delay(300);
digitalWrite(BUZZER_PIN, LOW);
if (wrongAttempts < MAX_ATTEMPTS)
{
showMessage("Wrong PIN!", "Try Again");
delay(1500);
}
else
{
DateTime now = rtc.now();
lockoutEndTime = now.unixtime() + LOCK_TIME_SECONDS;
preferences.putUInt("lockEnd", lockoutEndTime);
isLocked = true;
wrongAttempts = 0;
preferences.putInt("attempts", 0);
showMessage("System Locked", "Wait 30 Seconds");
}
}
void checkLockout()
{
DateTime now = rtc.now();
if (isLocked)
{
if (now.unixtime() >= lockoutEndTime)
{
isLocked = false;
lockoutEndTime = 0;
preferences.putUInt("lockEnd", 0);
showMessage("Lockout Over", "Enter PIN:");
}
else
{
int remainingTime = lockoutEndTime - now.unixtime();
lcd.setCursor(0, 0);
lcd.print("System Locked ");
lcd.setCursor(0, 1);
lcd.print("Wait ");
lcd.print(remainingTime);
lcd.print(" seconds ");
}
}
}
void checkPIN()
{
if (enteredPIN == correctPIN)
{
wrongAttempts = 0;
preferences.putInt("attempts", 0);
showMessage("Access Granted", "Welcome!");
unlockDoor();
}
else
{
wrongPIN();
}
enteredPIN = "";
}
void readKeypad()
{
if (isLocked)
{
return;
}
char key = keypad.getKey();
if (key)
{
if (key >= '0' && key <= '9')
{
if (enteredPIN.length() < 4)
{
enteredPIN += key;
lcd.setCursor(0, 1);
lcd.print("PIN: ");
for (int i = 0; i < enteredPIN.length(); i++)
{
lcd.print("*");
}
}
}
else if (key == '*')
{
enteredPIN = "";
showMessage("Enter PIN:", "");
}
else if (key == '#')
{
if (enteredPIN.length() == 4)
{
checkPIN();
}
else
{
showMessage("Enter 4 Digits", "Try Again");
delay(1000);
showMessage("Enter PIN:", "");
}
}
}
}
void setup()
{
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Wire.begin(21, 22);
lcd.init();
lcd.backlight();
if (!rtc.begin())
{
showMessage("RTC Error!", "Check Connection");
while (true)
{
delay(100);
}
}
if (!rtc.isrunning())
{
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
preferences.begin("access", false);
lockoutEndTime = preferences.getUInt("lockEnd", 0);
wrongAttempts = preferences.getInt("attempts", 0);
if (lockoutEndTime > rtc.now().unixtime())
{
isLocked = true;
showMessage("System Locked", "Wait 30 Seconds");
}
else
{
isLocked = false;
lockoutEndTime = 0;
preferences.putUInt("lockEnd", 0);
showMessage("Smart Access", "Enter PIN:");
}
}
void loop()
{
checkLockout();
checkDoorTimer();
readKeypad();
delay(50);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4