#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include <string.h>
// #include <Time.h>
#include <TimeLib.h>
#include <string.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal LCD(8, 9, 10, 11, 12, 13); // RS,EN,D4,D5,D6,D7
Servo servo;
const int buzzerPin = 14; // pin-14 = pin-A0
const int servoPin = 15; // pin-15 = pin A-1
const byte ROWS = 4, COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {16, 17, 2, 3};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {4, 5, 6, 7};
// Create the Keypad
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char password[16], string[16];
int flag_h_setpassword = 1, flag_inputpassword = 1, flag_inputstring = 0, flag_opendoor = 1, flag_state = 0, flag_remoteopen = 0, flag_lockdown = 0;
int count = 0, trial_count = 0, pos = 0, state = 0, flog = 0;
// flag_setpassword = flag for setting the password,
// flag_stringinput = flag for taking input the string,
// int hour = 0, minute = 0;
void setup()
{
for (int k = 8; k < 14; k++)
{
pinMode(k, OUTPUT); // pins 8-14 are enabled as output
}
LCD.begin(16, 2); // initializing LCD
// for the Peizo-buzzer
// Serial.begin(8600) ;
pinMode(buzzerPin, OUTPUT);
// for Servo-Motor
// pinMode(servoPin, OUTPUT) ;
servo.attach(servoPin);
// for Bluetooth-Module
Serial.begin(9600);
// Now, the Welcome message...
LCD.setCursor(0, 0);
LCD.print(" WELCOME !!");
LCD.setCursor(0, 1);
LCD.print("Set a Password :");
InitializePassword(), InitializeString();
CloseDoor();
}
void InitializePassword()
{
for (int i = 0; i < 16; ++i)
password[i] = 0;
}
void InitializeString()
{
for (int i = 0; i < 16; ++i)
string[i] = 1;
}
void H_EnterTime()
{
CloseDoor();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Enter Time (HHMM):");
LCD.setCursor(0, 1);
flag_inputstring = 1, count = 0;
}
void OpenDoor()
{
if (flag_opendoor == 1)
return;
for (pos = 15; pos <= 100; ++pos)
{
servo.write(pos);
delay(15);
}
flag_opendoor = 1;
}
void CloseDoor()
{
if (flag_opendoor == 0)
return;
for (pos = 100; pos >= 15; --pos)
{
servo.write(pos);
delay(15);
}
flag_opendoor = 0;
}
void loop()
{
if (flog == 0)
{
char key = kpd.getKey();
if (key != NO_KEY)
{
// If enter key is pressed
if (key == '#')
{
if (count == 4)
{
int current_hour = hour(), current_minute = minute();
// int current_hour = 3, current_minute = 20;
// int input_time = hour * 100 + minute;
int current_time = current_hour * 100 + current_minute;
char buffer[10];
sprintf(buffer, "%d", current_time);
// char x[5], y[5];
// x[0] = string[0];
// x[1] = string[1];
// y[0] = string[2];
// y[1] = string[3];
// String myString1(x);
// int myInt1 = myString1.toInt();
// String myString2(y);
// int myInt2 = myString2.toInt();
// int sum = myInt1 * 60 * 60 + myInt2 * 60;
// int sum1 = current_hour * 60 * 60 + current_minute * 60;
if (strcmp(buffer, string) >= 0)
{
if (strcmp(buffer, string) == 0)
{
LCD.clear();
LCD.print("***VERIFIED!!***");
Serial.println("UNLOCKED !!");
// Open the door
for (int i = 0; i < 3; ++i)
{
tone(buzzerPin, 500, 200);
delay(230);
tone(buzzerPin, 100, 200);
delay(300);
OpenDoor();
}
flog = 0;
}
else
{
flog = 1;
LCD.clear();
LCD.print("Wait");
Serial.println("Wait !!");
}
}
else
{
LCD.clear();
LCD.print("Wrong Time !");
Serial.println("Someone unsuccessfully attempted to open the lock !");
trial_count++;
tone(buzzerPin, 100, 1000);
delay(1000);
H_EnterTime();
}
}
else
{
InitializeString(), H_EnterTime();
}
}
// If a digit is pressed
else if (isdigit(key) && flag_inputstring == 1)
{
string[count] = key;
count++;
LCD.print(key);
delay(100);
}
}
}
else
{
LCD.clear();
LCD.print("Wait");
Serial.println("Wait !!");
}
}