#include <Keypad.h>
#include <Wire.h> // Add the Wire library for I2C communication
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo ServoMotor;
int position = 0;
int ServoPosition = 90;
const byte ROWS = 4;
const byte COLS = 4;
char pass[6];
char unlock[6];
char keys[ROWS][COLS]{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 11, 10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char keyPress; // Rename this variable to avoid conflict with 'key' variable
void setup() {
ServoMotor.attach(3); // Corrected typo
lcd.init();
lcd.backlight();
}
void loop() {
if (ServoPosition == 90 && position == 0) {
do {
position = 0;
lcd.setCursor(0, 0);
lcd.print("Press Enter ");
lcd.setCursor(0, 1);
lcd.print("Any key to LOCK"); // Corrected typo
}
while (keypad.getKey() == NO_KEY); // Corrected typo
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Password 6 DIGIT");
while (position < 6) {
lcd.setCursor(position, 1);
keyPress = keypad.getKey(); // Rename this variable
if (keyPress != NO_KEY) {
pass[position] = keyPress; // Rename this variable
lcd.print("*");
position++;
}
}
ServoPosition = 10;
}
ServoMotor.write(ServoPosition);
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Password!");
if (ServoPosition == 10) {
int num = 0;
while (num < 6) {
lcd.setCursor(num, 1);
keyPress = keypad.getKey(); // Rename this variable
if (keyPress != NO_KEY) {
unlock[num] = keyPress; // Rename this variable
lcd.print("*");
num++;
}
}
int chk = 0;
while (chk < 6) {
if (unlock[chk] == pass[chk]) {
chk++;
} else {
num = 0;
chk = 5;
ServoPosition = 10;
lcd.clear();
lcd.print("Error");
delay(1000);
lcd.clear();
}
}
if (chk == 6) {
ServoPosition = 90;
ServoMotor.write(ServoPosition);
position = 0;
}
}
}