#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
#include "icons.h"
// For RFID and Passcode
#define SS_PIN 5
#define RST_PIN 17
String UID = "B3 97 37 96";
String password = "1989";
MFRC522 rfid(SS_PIN, RST_PIN);
// For Solenoid locking mechanism
#define SOLENOID_PIN 16
// LCD Configurations
#define I2C_ADDR 0x26
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Keypad setup
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = { 13, 12, 14, 27 };
byte colPins[KEYPAD_COLS] = { 26, 25, 33, 32 };
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
void setup() {
Serial.begin(9600);
// LCD Init
lcd.init();
lcd.backlight();
init_icons(lcd);
// Solenoid Init
pinMode(SOLENOID_PIN, OUTPUT);
// RFID init
SPI.begin();
rfid.PCD_Init();
}
void loop() {
lock();
doorLockedLogic();
}
/* =============================================== */
void lock() {
digitalWrite(SOLENOID_PIN, LOW);
}
void unlock() {
digitalWrite(SOLENOID_PIN, HIGH);
}
void wrongCodeCard() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Denied! ");
lcd.setCursor(0, 1);
lcd.print(" Wrong code or card ");
showWaitScreen(200);
lock();
doorLockedLogic();
}
String RFIDTag() {
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
}
return ID;
}
String inputSecretCode() {
lcd.setCursor(7, 3);
lcd.print("[____]");
lcd.setCursor(8, 3);
String result = "";
delay(500);
while (result.length() < 4) {
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
String ID = RFIDTag();
ID.toUpperCase();
if (ID.substring(1) == UID) {
lcd.setCursor(7, 3);
lcd.print("[____]");
lcd.setCursor(8, 3);
for (byte i = 0; i < 4; i++) {
lcd.print("*");
delay(200);
}
unlock();
doorUnlockedLogic();
} else {
wrongCodeCard();
}
}
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
}
}
return result;
}
void showWaitScreen(int delayMillis) {
lcd.setCursor(4, 3);
lcd.print("[..........]");
lcd.setCursor(5, 3);
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("=");
}
}
void setNewCode() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Enter new code ");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Confirm new code ");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
password = newCode;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Code mismatch ");
lcd.setCursor(0, 1);
lcd.print(" Door not locked! ");
showWaitScreen(200);
lock();
doorLockedLogic();
}
}
void doorLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Door Locked! ");
lcd.write(ICON_LOCKED_CHAR);
lcd.setCursor(0, 1);
lcd.print(" Enter code or card ");
String userCode = inputSecretCode();
if (userCode.equals(password)) {
unlock();
doorUnlockedLogic();
} else {
wrongCodeCard();
}
}
void doorUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.print(" Door Unlocked! ");
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(0, 2);
lcd.print(" # or CARD = lock ");
lcd.setCursor(0, 3);
lcd.print(" A = new code ");
delay(500);
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
String ID = RFIDTag();
ID.toUpperCase();
if (ID.substring(1) == UID) {
lock();
doorLockedLogic();
}
}
key = keypad.getKey();
}
if (key == 'A') {
setNewCode();
} else if (key == '#') {
lock();
doorLockedLogic();
}
}
Loading
esp32-devkit-v1
esp32-devkit-v1