#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define RED_LED 12
#define GREEN_LED 13
#define BLUE_LED 14
#define BUZZER 27
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] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 2, 15};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String input = "";
String masterPIN = "1234";
void showMessage(String line1, String line2 = "") {
display.clearDisplay();
display.setCursor(0, 0);
display.println(line1);
if (line2 != "") display.println(line2);
display.display();
}
void setup() {
Serial.begin(115200);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT); // Optional
pinMode(BUZZER, OUTPUT);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(BUZZER, LOW);
Wire.begin(21, 22); // SDA, SCL
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED init failed");
while (1);
}
display.setTextSize(1);
display.setTextColor(WHITE);
showMessage("Smart Access", "System Ready");
delay(2000);
showMessage("Enter PIN or", "Scan RFID");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
checkPIN();
} else if (key == '*') {
input = "";
showMessage("PIN Cleared");
delay(1000);
showMessage("Enter PIN or", "Scan RFID");
} else {
input += key;
showMessage("Enter PIN:", input);
}
}
if (Serial.available()) {
String rfid = Serial.readStringUntil('\n');
rfid.trim();
if (rfid == "#RFID123") {
grantAccess();
} else {
denyAccess();
}
input = "";
showMessage("Enter PIN or", "Scan RFID");
}
}
void grantAccess() {
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
showMessage("Access Granted ✅");
delay(3000);
digitalWrite(GREEN_LED, LOW);
}
void denyAccess() {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
for (int i = 0; i < 3; i++) {
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(BUZZER, LOW);
delay(200);
}
showMessage("Access Denied ❌");
delay(2000);
digitalWrite(RED_LED, LOW);
}
void checkPIN() {
if (input == masterPIN) {
grantAccess();
} else {
denyAccess();
}
input = "";
showMessage("Enter PIN or", "Scan RFID");
}