// default pass is 12ABC
// enter pass and then use "#" to unlock.
// when unlocked, use # after 5 sec to lock (no pass required)
// to change pass click "*" and enter old pass.
// then click #.
// now type the new pass and click # and wait 5 sec.
// you have successfully set your new pass
#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display settings
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3D // Address of the SSD1306 OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
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] = { 2, 3, 4, 5 }; // Connect keypad rows to Arduino pins
byte colPins[COLS] = { 6, 7, 8, 9 }; // Connect keypad columns to Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Initialize the keypad
Servo myServo; // Servo for locking/unlocking the door
const int lockPosition = 90; // Servo locked position
const int unlockPosition = 180; // Servo unlocked position
String password = "12ABC"; // Default password
String input = ""; // To store user input from keypad
bool settingNewPassword = false; // Flag for setting a new password
bool verifyOldPassword = false; // Flag for verifying the old password
bool doorLocked = true; // Door state: true means locked
void setup() {
Serial.begin(9600); // Start serial communication
myServo.attach(10); // Attach servo to pin 10
myServo.write(lockPosition); // Lock the door initially
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Set I2C address to 0x3C
Serial.println(F("SSD1306 allocation failed")); // Print error to Serial Monitor
for (;;); // Stop execution if initialization fails
}
// Clear the display and show the welcome message
display.clearDisplay();
display.setTextSize(1); // Set text size for OLED
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 0); // Set cursor to top-left
display.println("Enter Password:"); // Display initial prompt
display.display(); // Update OLED
}
void loop() {
char key = keypad.getKey(); // Get keypress from keypad
if (key) {
Serial.print(key); // Print keypress to Serial Monitor
input += key; // Append keypress to input string
// Update the OLED with the current input
display.clearDisplay();
display.setTextSize(1); // Set text size for OLED
display.setCursor(0, 0); // Set cursor to top-left
if (verifyOldPassword) {
display.println("Enter Old Pass:"); // Show "Enter Old Pass" prompt
} else if (settingNewPassword) {
display.println("Set New Password:"); // Show "Set New Password" prompt
} else if (doorLocked) {
display.println("Enter Password:"); // Show "Enter Password" prompt
} else {
display.println("Door Locked"); // Show "Door Locked" message
}
display.setCursor(0, 10); // Move cursor down for input display
display.println(input); // Show the current input
display.display(); // Update OLED
if (key == '#') { // Handle # keypress (confirm/enter)
if (verifyOldPassword) { // Verify old password
if (input.substring(0, input.length() - 1) == password) { // Compare input with current password
Serial.println("\nOld Password Correct!");
displayMessage("Old Password OK"); // Show success message
delay(2000);
settingNewPassword = true; // Enable new password setup
verifyOldPassword = false;
input = ""; // Reset input
displayMessage("Set New Password:"); // Prompt for new password
Serial.println("Set New Password:");
} else {
Serial.println("\nOld Password Incorrect!");
displayMessage("Old Password NO"); // Show error message
delay(2000);
input = ""; // Reset input
verifyOldPassword = false; // Disable verification mode
displayMessage("Enter Password:"); // Reset to default prompt
Serial.println("Enter Password:");
}
} else if (doorLocked) { // Handle door unlocking/locking
if (settingNewPassword) { // Set new password
password = input.substring(0, input.length() - 1); // Update password
Serial.println("\nNew Password Set!");
displayMessage("New Password Set!"); // Show success message
delay(5000);
settingNewPassword = false; // Disable new password setup
input = ""; // Reset input
displayMessage("Enter Password:"); // Reset to default prompt
Serial.println("Enter Password:");
} else { // Verify and unlock door
if (input.substring(0, input.length() - 1) == password) { // Compare input with password
Serial.println("\nPassword Correct!");
displayMessage("Password Correct!"); // Show success message
unlockDoor(); // Unlock the door
delay(5000);
} else {
Serial.println("\nPassword Incorrect!");
displayMessage("Password Incorrect!"); // Show error message
delay(5000);
}
input = ""; // Reset input
displayMessage("Enter Password:"); // Reset to default prompt
Serial.println("Enter Password:");
}
} else { // Lock the door
lockDoor(); // Lock the door
displayMessage("Door Locked"); // Show door locked message
delay(5000);
displayMessage("Enter Password:"); // Reset to default prompt
input = ""; // Reset input
Serial.println("Enter Password:");
}
}
if (key == '*') { // Handle * keypress (start password change)
input = ""; // Reset input
verifyOldPassword = true; // Enable verification mode
displayMessage("Enter Old Pass:"); // Prompt for old password
Serial.println("\nEnter Old Pass:");
}
}
}
void unlockDoor() {
myServo.write(unlockPosition); // Move servo to unlocked position
doorLocked = false; // Update door state
}
void lockDoor() {
myServo.write(lockPosition); // Move servo to locked position
doorLocked = true; // Update door state
}
void displayMessage(String message) {
display.clearDisplay(); // Clear OLED
display.setTextSize(1); // Set text size for OLED
display.setCursor(0, 0); // Set cursor to top-left
display.println(message); // Display custom message
display.display(); // Update OLED
}