#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
const int greenLed = 7;
const int redLed = 6;
const int buzz = 5;
const int rows = 4;
const int cols = 4;
char keys[rows][cols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[rows] = {0, 1, 2, 3};
byte colPins[cols] = {A0, A1, A2, A3};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
const int password = 1234;
int inputPassword = 0;
int passLength = 0;
int attempt = 0;
void setup() {
lcd.begin(16, 2);
}
void loop() {
if(attempt < 3) {
while (passLength < 4) {
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
char key = keypad.getKey();
if(key >= '0' && key <= '9') {
inputPassword = inputPassword * 10 + (key - '0');
lcd.setCursor(passLength, 1);
lcd.print('*');
passLength++;
}
}
if(password == inputPassword){
digitalWrite(greenLed, HIGH);
digitalWrite(buzz, HIGH);
}
else {
attempt++;
digitalWrite(redLed, HIGH);
inputPassword = 0;
passLength = 0;
lcd.clear();
}
}
else {
digitalWrite(redLed, HIGH);
digitalWrite(buzz, HIGH);
delay(1000);
digitalWrite(redLed, LOW);
digitalWrite(buzz, LOW);
delay(1000);
}
}