#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Keypad setup
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);
// Predefined PIN
String correctPIN = "1234";
String enteredPIN = "";
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Halt
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Manual Authentication");
display.display();
delay(2000);
}
void loop() {
char key = keypad.getKey();
if (key) { // If a key is pressed
if (key == '#') { // '#' is the Enter key
checkAuthentication();
} else if (key == '*') { // '*' is the Clear key
enteredPIN = "";
updateDisplay("PIN Cleared");
} else { // Add digit to PIN
enteredPIN += key;
updateDisplay("Enter PIN: " + enteredPIN);
}
}
}
void checkAuthentication() {
if (enteredPIN == correctPIN) {
updateDisplay("Access Granted");
Serial.println("Access Granted");
} else {
updateDisplay("Access Denied");
Serial.println("Access Denied");
}
enteredPIN = ""; // Reset PIN after checking
}
void updateDisplay(String message) {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println(message);
display.display();
}