#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const byte rows=4;
const byte cols=4;
int i=0;
String y="12345";
String s="";
String n="";
char keys[rows][cols]={{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
byte rowpin[rows]={13,12,11,10};
byte colpin[cols]={9,8,7,6};
Keypad des_key= Keypad(makeKeymap(keys),rowpin,colpin,rows,cols);
void setup()
{
lcd.clear();
lcd.init();
lcd.backlight();
}
void loop()
{
char key= des_key.getKey();
if(key>0)
{
switch(key)
{
case 'D': changePassword();
default: checkPassword();
}
}
}
void checkPassword()
{
char key= des_key.getKey();
if(key>0)
{
if(key!='*')
{
s=s+key;
if(key=='#')
{
lcd.setCursor(i-1,0);
lcd.print(" ");
i=i-1;
s.remove(i);
}
else
{
lcd.setCursor(i,0);
lcd.print("*");
i=i+1;
}
}
else
{
if(y.equals(s))
{
lcd.setCursor(0,1);
lcd.print("Correct Password.");
}
else
{
lcd.setCursor(0,1);
lcd.print("Wrong Password.");
}
}
}
}
void resetPassword()
{
s.remove(0);
lcd.setCursor(0,0);
lcd.clear();
}
void changePassword()
{
lcd.setCursor(0,0);
lcd.print("Enter password:");
checkPassword();
if(y.equals(s))
{
resetPassword();
lcd.setCursor(0,0);
lcd.print("New Password:");
delay(1000);
lcd.clear();
char key1= des_key.getKey();
if(key1>0)
{
while(key1!='*')
{
char key1= des_key.getKey();
s=s+key1;
if(key1=='#')
{
lcd.setCursor(i-1,0);
lcd.print(" ");
i=i-1;
s.remove(i);
}
else
{
lcd.setCursor(i,0);
lcd.print("*");
i=i+1;
}
}
}
n=s;
lcd.setCursor(0,1);
lcd.print("Changed.");
}
else
{
lcd.setCursor(0,0);
lcd.print("Wrong Input.");
}
}