#include <LiquidCrystal.h>
const int password[8] = {1,1,1,1,1,1,1,1};
int entered[8];
int flag = 0;
int i = 0;
bool alarm = 1;
int attempts = 3;
const int rs=12, en=11, d4 =10, d5=9, d6 = 8, d7=7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
pinMode(A5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(13, OUTPUT);
lcd.print("Password-Based");
lcd.setCursor(0,1);
lcd.print("Locking System");
delay(2000);
lcd.clear();
digitalWrite(13, LOW);
}
void loop(){
if(flag == 0)
{
switch (alarm){
case 0:
lcd.setCursor(0,0);
lcd.print("UNLOCKED");
break;
case 1:
lcd.setCursor(0,0);
lcd.print("LOCKED");
break;
}
bool enter = digitalRead(2);
bool k0 = digitalRead(A0);
bool k1 = digitalRead(A1);
bool k2 = digitalRead(A2);
bool k3 = digitalRead(A3);
bool k4 = digitalRead(A4);
bool k5 = digitalRead(A5);
bool k6 = digitalRead(3);
bool k7 = digitalRead(6);
bool k8 = digitalRead(5);
bool k9 = digitalRead(4);
bool match = 1;
if (i>7 || enter == LOW)
{
flag++;
}
//int input = k0*1 + k1*2 + k2*4 + k3*8 + k4*16 + k5*32 + k6*64 + k7*128 + k8*256 + k9*512;
if(flag == 1){
lcd.setCursor(0,0);
lcd.print("Enter Password:");
lcd.setCursor(0,1);
//k9 LOW
if(k9 == LOW){
entered[i] = 9;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);}
//k8 LOW
if(k8 == LOW){
entered[i] = 8;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k7 LOW
if(k7 == LOW){
entered[i] = 7;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);}
//k6 LOW
if(k6 == LOW){
entered[i] = 6;
lcd.setCursor(i,1);
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k5 LOW
if(k5 == LOW){
entered[i] = 5;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k4 LOW
if(k4 == LOW)
{
entered[i] = 4;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k3 LOW
if(k3 == LOW){
entered[i] = 3;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k2 LOW
if(k2 == LOW)
{
entered[i] = 2;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k1 LOW
if(k1 == LOW)
{
entered[i] = 1;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}
//k0 LOW
if(k0 == LOW){
entered[i] = 0;
lcd.setCursor(i,1);
lcd.print(entered[i]);
i++;
delay(1000);
}}
// Processing and Output Phase
if(flag == 2){
lcd.clear();
match = 0;
//Verification loop
for(int a = 0; a < 8; a++){
if(password[a] != entered[a])
{
match++;
delay(100);
}}
//Result Output
if(match == 0){
lcd.setCursor(0,0);
lcd.print("Authorized");
alarm = !alarm;
lcd.setCursor(0,1);
if(alarm == 0)
{
lcd.print("Alarm disabled");
}
else if(alarm == 1)
{
lcd.print("Alarm enabled");
}
flag = 0;
match = 1;
i = 0;
attempts = 3;
delay(5000);
lcd.clear();
}
else if(match == 1){
lcd.clear();
lcd.print("Unauthorized");
attempts--;
match = 0;
flag = 0;
i = 0;
lcd.setCursor(0,1);
if(attempts > 0){
lcd.print(attempts);
lcd.print(" attempts left");
delay(1000);
lcd.clear();
}
else {
lcd.clear();
lcd.print("Access denied");
lcd.setCursor(0,1);
lcd.print("System locked");
flag++;
delay(1000);
digitalWrite(13, HIGH);
}}}
if (flag>2){
for(;;)
{
lcd.print("")
}
}
}