#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
//LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
//SERVO
Servo myServo;
int servoPin = 10;
int closedPosition = 0; // Locked
int openPosition = 90; // Unlocked
//KEYPAD
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
uint8_t rowPins[ROWS] = {9, 8, 7, 6};
uint8_t colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String password = "090423"; // PASS
String inputP = "";
void setup() {
myServo.attach(servoPin);
myServo.write(closedPosition); // dapat naka lock muna
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { //ito magseserve as "enter"
if (inputP == password) {
lcd.clear();
lcd.print("Unlocked");
myServo.write(openPosition); // ma a-unlock pag tama
delay(5000);
lcd.clear();
lcd.print("Enter Password:");
myServo.write(closedPosition); // then ma la-lock ulit
} else {
lcd.clear();
lcd.print("Wrong Password");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
}
inputP = ""; // Reset the input password
} else if (key == '*') { // para sa delete or pag reset ng na input
inputP = "";
lcd.clear();
lcd.print("Enter Password:");
} else {
inputP += key;
lcd.setCursor(0, 1);
lcd.print(inputP);
}
}
}