#include <SoftwareSerial.h>
SoftwareSerial BlueTooth(12, 13);
#include<LiquidCrystal_I2C.h>
#include<Keypad.h>
#include <Servo.h>
#define buzzerPin 10
#define servoPin 11
LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char storepass[]="2004";
char DoorOpen= 'a';
char DoorLock= 'b';
unsigned short int i=0,count=0,number=sizeof(storepass)-1;
char pass[sizeof(storepass)-1];
unsigned short int inputpass=2004;
Servo myServo;
char inputpassword;
unsigned char countwrong=0;
char key;
void WrongPass()
{
tone(buzzerPin,1000);
delay(100);
noTone(buzzerPin);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Wrong Password");
delay(2000);
i=0;
count=0;
lcd.setCursor(0,0);
lcd.print("Enter the Pass.");
lcd.setCursor(0,1);
countwrong++;
}
void Opendoor()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Correct Password");
delay(2000);
i=0;
count=0;
lcd.setCursor(0,0);
myServo.write(100);
delay(3000);
lcd.print("Welcome Doctor! ");
delay(2000);
myServo.write(0);
lcd.clear();
delay(2000);
lcd.print("Enter the Pass. ");
countwrong=0;
lcd.setCursor(0,1);
if (key>='0'&&key!='#')
{
lcd.print('*');
pass[i]=key;
i++;
}
}
void Doorclosed()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DOOR CLOSED! ");
delay(2000);
i=0;
count=0;
lcd.setCursor(0,0);
lcd.clear();
myServo.write(0);
delay(2000);
lcd.print("Enter the Pass. ");
countwrong=0;
}
void setup()
{
BlueTooth.begin(9600);
BlueTooth.println("Enter the Pass. ");
lcd.begin(16,2);
lcd.print("Enter the Pass. ");
lcd.setCursor(0, 1);
lcd.backlight();
pinMode(buzzerPin,OUTPUT);
myServo.attach(servoPin);
}
void loop() {
if (BlueTooth.available())
{
inputpassword=BlueTooth.read();
if(inputpassword==DoorOpen)
{
Opendoor();
inputpassword='a';
//Open Ring
}
else if(inputpassword==DoorLock)
{
Doorclosed();
inputpassword='b';
//Closed Ring
}
}
myServo.write(0);
delay(15);
char key = keypad.getKey();
if (key>='0'&&key!='#')
{
lcd.print('*');
pass[i]=key;
i++;
}
else if(key=='#')
{
if(i==number)
{
for(short int j=0;j<number;j++)
{
if(pass[j]==storepass[j])count++;
}
if(count==number)
{
Opendoor();
}
else
WrongPass();
}else
WrongPass();
}
if(countwrong==3)
{
tone(buzzerPin,1000);
delay(3000);
noTone(buzzerPin);
countwrong=0;
}
}