#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
// Define the OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Define the 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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define the correct password
const char password[] = "1234";
void setup() {
// Initialize the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Enter password:");
display.display();
}
void loop() {
static char enteredPassword[5] = "";
static byte passwordIndex = 0;
// Check if a key is pressed
char key = keypad.getKey();
if (key) {
// Check if the entered password is correct
if (key == '#') {
if (strcmp(enteredPassword, password) == 0) {
// Correct password
display.clearDisplay();
display.setCursor(0, 0);
display.println("Password correct!");
display.display();
delay(2000);
// Reset the entered password
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
} else {
// Incorrect password
display.clearDisplay();
display.setCursor(0, 0);
display.println("Password incorrect!");
display.display();
delay(2000);
// Reset the entered password
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
}
} else if (key == '*') {
// Clear the entered password
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
} else {
// Append the entered digit to the password
enteredPassword[passwordIndex] = key;
passwordIndex++;
// Display the entered password
display.clearDisplay();
display.setCursor(0, 0);
display.print("Enter password:");
display.setCursor(0, 20);
display.println(enteredPassword);
display.display();
}
}
}