#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
#define REDLED 3
#define GREENLED 2
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
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] = {22, 24, 26, 28};
byte pin_column[COLUMN_NUM] = {23, 25, 27, 29};
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo motor;
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
const String passcode = "1907";
String input_passcode;
int attempts = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
motor.attach(4);
motor.write(90);
pinMode(REDLED, OUTPUT);
pinMode(GREENLED, OUTPUT);
input_passcode.reserve(33);
lcd.println("***HYDRATECH***");
lcd.setCursor(0, 1);
lcd.println("***SECURITY****");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("ENTER PASSCODE");
}
void loop() {
motor.write(90);
digitalWrite(REDLED, LOW);
digitalWrite(GREENLED, LOW);
char key = keypad.getKey();
if (key) {
if (input_passcode.length() == 0) {
lcd.clear();
}
lcd.print(key);
// To delete
if (key == 'D') {
input_passcode = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("ENTER PASSCODE");
}
// To submit passcode
else if (key == 'C') {
if (passcode == input_passcode) {
lcd.clear();
lcd.println("ACCESS GRANTED");
digitalWrite(GREENLED, HIGH);
motor.write(0);
delay(3000);
lcd.clear();
attempts = 0;
} else {
lcd.clear();
lcd.println("ACCESS DENIED");
digitalWrite(REDLED, HIGH);
delay(3000);
lcd.clear();
attempts++;
} if (attempts > 3){
wait();
}
input_passcode = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("ENTER PASSCODE");
}
else {
input_passcode += key;
}
}
}
void wait(){
int wait_time = 11;
for(int i = 0 ; i < 11 ; i++){
wait_time--;
lcd.clear();
lcd.print("Please wait for ");
lcd.setCursor(0,1);
lcd.print(wait_time);
lcd.setCursor(3,1);
lcd.print("seconds");
delay(1000);
}
}