#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);//for LCD
int LedpinG = 12;
int LedpinR = 13;
//int trigPin = 11;
//int echoPin = 1;
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
const int MAX_PASSWORD_LENGTH = 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] = {9, 8, 7, 6};
byte pin_columns[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_columns, ROW_NUM, COLUMN_NUM);
const String password = "1234";
String input_password = "";
void setup() {
Serial.begin(9600);
input_password.reserve(MAX_PASSWORD_LENGTH);
pinMode(LedpinG, OUTPUT);
pinMode(LedpinR, OUTPUT);
//pinMode(trigPin, OUTPUT);
//pinMode(echoPin, INPUT);
lcd.init();
lcd.backlight();
delay(1000);
lcd.setCursor(3,0.5);
lcd.print("Hello there !!!");
delay(1000);
lcd.setCursor(1,2);
lcd.print("Enter the password");
delay(2000);
lcd.clear();
lcd.noBacklight();
delay(1000);
lcd.backlight();
lcd.setCursor(1,1.5);
lcd.print("Password: ");
delay(1000);
lcd.noBacklight();
}
void loop() {
char key = keypad.getKey();
//int distance;
//int duration;
//digitalWrite(trigPin, LOW);
//delayMicroseconds(2);
//digitalWrite(trigPin, HIGH);
//delayMicroseconds(10);
//digitalWrite(trigPin, LOW);
//duration =pulseIn(echoPin,HIGH);
//distance = duration*0.0344/2;
if (key) {
Serial.println(key);
lcd.backlight();
lcd.print("X");
if (key == '*') {
input_password = "";
} else if (key == '#'){
if (password.equals(input_password)) {
Serial.println("Password is correct");
digitalWrite(LedpinG, HIGH);
delay(1000);
digitalWrite(LedpinG, LOW);
lcd.clear();
lcd.setCursor(1,1.5);
lcd.print("Password is correct");
lcd.setCursor(4,3);
lcd.print("CONGRAST!!!");
delay(2000);
lcd.clear();
lcd.setCursor(1,1.5);
lcd.print("Password: ");
delay(1000);
lcd.noBacklight();
} else {
Serial.println("Password is incorrect, try again");
digitalWrite(LedpinR, HIGH);
delay(1000);
digitalWrite(LedpinR, LOW);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Password INCORRECT");
lcd.setCursor(4,3);
lcd.print("Try again!!!");
delay(2000);
lcd.clear();
lcd.setCursor(1,1.5);
lcd.print("Password: ");
delay(1000);
lcd.noBacklight();
}
input_password = "";
} else {
input_password += key;
}
}
}