#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
Servo myservo;
int pos = 90;
int val = 0;
int a = 0;
int failed = 0;
// Display setup
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// Keypad setup
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
String pass="";
String p="";
// Function to display splash screen
void showSplashScreen() {
lcd.print("Arduino");
lcd.setCursor(0, 3); // Set cursor position for 'Arduino'
String message = "Authenticator";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}
//Function to set new password
void setPassword()
{
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0); // Set cursor position
byte flag=0;
while(flag<1)
{
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Set New Password:");
lcd.setCursor(1,1);
lcd.print("Only 4 Digits!!");
delay(2000);
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Set New Password:");
lcd.setCursor(6,1);
lcd.print("");
lcd.setCursor(6,1);
pass="";
while(pass.length()<4)
{
char key=keypad.getKey();
if (key >= '0' && key <= '9')
{
lcd.print('*');
pass += key;
}
}
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
String pass1="";
lcd.print("Confirm Password:");
lcd.setCursor(6,1);
lcd.print("");
lcd.setCursor(6,1);
while(pass1.length()<4)
{
char key=keypad.getKey();
if (key >= '0' && key <= '9')
{
lcd.print('*');
pass1 += key;
}
}
lcd.clear();
lcd.cursor();
if(pass==pass1)
{
lcd.setCursor(1, 0);
lcd.print("New Password");
lcd.setCursor(0,1);
lcd.print("is Set!");
digitalWrite(13, HIGH);
flag= flag+1;
}
else
{
lcd.setCursor(1, 0);
lcd.print("Code Mismatch!");
lcd.setCursor(0,1);
lcd.print("Try Again!");
delay(2000);
flag=0;
}
}
}
//Function to verify password
bool checkPassword()
{
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
String pass2="";
lcd.print("Enter Password:");
lcd.setCursor(6,1);
lcd.print("");
lcd.setCursor(6,1);
while(pass2.length()<4)
{
char key=keypad.getKey();
if (key >= '0' && key <= '9')
{
lcd.print('*');
pass2 += key;
}
}
lcd.clear();
lcd.cursor();
if(pass2==pass)
{
return true;
}
else
{
return false;
}
}
//Function to Grant Access to Set New Password
void verifyOldPassword()
{
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Enter Previous");
lcd.setCursor(0,1);
lcd.print("Password!");
delay(2000);
val=0;
while(val<1)
{
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
lcd.print("Password:");
p="";
while(p.length()<4)
{
char key=keypad.getKey();
if (key >= '0' && key <= '9')
{
lcd.print('*');
p += key;
}
}
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
if(p==pass)
{
lcd.print("Access Granted To");
lcd.setCursor(0, 1);
lcd.print("Set password!");
val = val + 1;
pos=0;
myservo.write(pos);
digitalWrite(13, HIGH);
delay(2000);
}
else
{
lcd.print("Try Again!");
}
digitalWrite(13, LOW);
pos=90;
myservo.write(pos);
delay(2000);
}
}
//Function to run the process continuously
void retry()
{
if (a==0)
{
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
lcd.print("1 - Try Again!");
lcd.setCursor(0,1);
lcd.print("A -New Password");
delay(2000);
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Enter Choice:");
lcd.setCursor(6,1);
val=0;
while(val < 1)
{
char key=keypad.getKey();
if(key=='1')
{
lcd.print("*");
val = val + 1;
}
else if ( key == 'A')
{
verifyOldPassword();
setPassword();
val = val + 1;
failed = 0;
}
else if (key == '2' || key == '3' || key == '4' ||key == '5' ||key == '6' ||key == '7' ||key == '8' || key == '9' ||key == '0' ||key == 'B' ||key == 'C' ||key == 'D' ||key == '*' ||key == '#')
{
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Incorrect");
lcd.setCursor(0,1);
lcd.print("Try Again!");
val = 0;
delay(2000);
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Enter Choice:");
lcd.setCursor(6,1);
}
}
}
else
{
lcd.clear();
lcd.cursor();
lcd.setCursor(1,0);
lcd.print("A -New Password"); // changed here
delay(2000);
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Enter Choice:");
lcd.setCursor(6,1);
val=0;
while(val < 1)
{
char key=keypad.getKey();
if ( key == 'A')
{
lcd.print("*");
verifyOldPassword();
setPassword();
val = val + 1;
delay(1000);
}
else if (key == '1' || key == '2' || key == '3' || key == '4' ||key == '5' ||key == '6' ||key == '7' ||key == '8' || key == '9' ||key == '0' ||key == 'B' ||key == 'C' ||key == 'D' ||key == '*' ||key == '#')
{
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Incorrect");
lcd.setCursor(0,1);
lcd.print("Try Again!");
val = 0;
delay(2000);
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Enter Choice:");
lcd.setCursor(6,1);
}
}
}
}
void setup()
{
Serial.begin(115200);
lcd.begin(16, 2);
myservo.attach(6);
showSplashScreen(); // Display splash screen
setPassword();
pinMode(13, OUTPUT);
}
void loop()
{
delay(2000);
digitalWrite(13, LOW);
if(checkPassword())
{
lcd.setCursor(1, 0);
lcd.print("Access Granted!");
pos=0;
a=1;
failed = 0;
myservo.write(pos);
digitalWrite(13, HIGH);
delay(3000);
}
else
{
lcd.setCursor(1, 0);
lcd.print("Access Denied!");
a=0;
failed = failed + 1;
}
digitalWrite(13, LOW);
pos=90;
myservo.write(pos);
delay(2000);
if (failed == 3)
{
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
lcd.print("Resource");
lcd.setCursor(0,1);
lcd.print("Locked!!");
delay(2000);
exit(0);
}
retry();
}