#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int servoPin = 9;
Servo myServo;
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] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2, A0};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27 for the I2C module, 16x2 display
char password[] = "1234"; // Change this to your desired password
char enteredPassword[5];
char securityAnswer[10]; // Increase size for alphanumeric input
int passwordIndex = 0;
int securityIndex = 0;
bool isAuthenticated = false; // Flag to track authentication status
void setup() {
Wire.begin();
lcd.init();
lcd.backlight();
lcd.print("Enter Password:");
myServo.attach(servoPin);
}
void loop() {
char key = keypad.getKey();
if (!isAuthenticated) { // Check if not authenticated
if (key) {
if (key == '*' || key == '#') {
// Ignore these keys
} else if (key == 'D') {
if (strcmp(enteredPassword, password) == 0) {
lcd.clear();
lcd.print("Password OK");
delay(1000);
lcd.clear();
lcd.print("Security Question:");
lcd.setCursor(0, 1);
lcd.print("What is UR FAV THINGS?");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
isAuthenticated = true; // Set authentication flag
} else {
lcd.clear();
lcd.print("Wrong Password");
delay(1000);
lcd.clear();
lcd.print("Enter Password:");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
}
} else {
enteredPassword[passwordIndex++] = key;
lcd.setCursor(passwordIndex - 1, 1);
lcd.print('*');
}
}
} else { // Authenticated
if (key && isAlphaNumeric(key)) { // Check if the key is alphanumeric
securityAnswer[securityIndex++] = key;
lcd.setCursor(securityIndex - 1, 1);
lcd.print('*');
} else if (key == '#') { // End of security answer
securityAnswer[securityIndex] = '\0'; // Null terminate the string
if (strcmp(securityAnswer, "3A23AC") == 0) { // Change "4" to your desired answer
lcd.clear();
lcd.print("Security Answer:");
lcd.setCursor(0, 1);
lcd.print("Correct!");
delay(1000);
lcd.clear();
lcd.print("Access Granted");
delay(2000);
lcd.clear();
myServo.write(90); // Rotate servo to 90 degrees
delay(2000);
myServo.write(0); // Rotate servo back to 0 degrees
lcd.print("Enter Password:");
isAuthenticated = false; // Reset authentication flag
securityIndex = 0; // Reset security answer index
} else {
lcd.clear();
lcd.print("Security Answer:");
lcd.setCursor(0, 1);
lcd.print("Incorrect!");
delay(1000);
lcd.clear();
lcd.print("Access Denied");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
isAuthenticated = false; // Reset authentication flag
securityIndex = 0; // Reset security answer index
}
}
}
}
bool isAlphaNumeric(char c) {
return isalnum(c) || c == ' ';
}