#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
#define SERVO_PIN 8
#define Password_Length 8
int signalPin = A4;
char Data[Password_Length];
char Master[Password_Length] = "123A456";
byte data_count = 0, master_count = 0;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {13, 12, 11, 9}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins
byte pin_column[COLUMN_NUM] = {7, 6, 5, 4}; // GIOP16, GIOP4, GIOP0, GIOP2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password_1 = "555444"; // change your password here
const String password_2 = "564255"; // change your password here
const String password_3 = "545423"; // change your password here
String input_password;
int angle = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
void setup() {
lcd.init();
lcd.backlight();
pinMode(signalPin, OUTPUT);
Serial.begin(9600);
servo.attach(SERVO_PIN);
servo.write(angle);
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Enter Password:");
delay (100);
char key = keypad.getKey();
if (key) {
Data[data_count] = key;
lcd.setCursor(data_count,1);
lcd.print(Data[data_count]);
data_count++;
Serial.println(key);
if (key == 'B') {
input_password = "";
lcd.clear();
clearData();
} else if (key == 'A') {
if (input_password == password_1 || input_password == password_2 || input_password == password_3) {
Serial.println("Valid Password => unlock the door");
lcd.print("Correct");
clearData();
if (angle == 0)
angle =180;
else //if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo.write(angle);
Serial.print("Rotate Servo Motor to ");
Serial.print(angle);
Serial.println("°");
delay(5000);
if (angle == 0)
angle =180;
else //if(angle == 90)
angle = 0;
servo.write(angle);
Serial.print("Rotate Servo Motor to ");
Serial.print(angle);
Serial.println("°");
} else {
Serial.println("Invalid Password => Try again");
lcd.print("Incorrect");
clearData();
}
input_password = ""; // reset the input password
} else {
input_password += key; // append new character to input password string
}
}
}
void clearData()
{
while(data_count !=0)
{
Data[data_count--] = 0;
}
return;
}