#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>
const int green_led = 13 ; // the Arduino pin, which connects to the IN pin of relay
const int red_led = 12;
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 4; // four columns
int Motion_sen = 10; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = LOW;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
int count = 0;
const String password_1 = "1234"; // change your password here
String input_password = "";
int alarm_disable = 0;
Servo servo; // create servo object to control a servo
// LCD pins <--> Arduino pins
const int RS = A0, EN = A1, D4 = A2, D5 = A3, D6 = A4, D7 = A5;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int total=0;
void setup() {
// put your setup code here, to run once:
pinMode(green_led, OUTPUT); // initialize pin as an output.
pinMode(red_led, OUTPUT);
pinMode(Motion_sen, INPUT); // declare sensor as input
servo.attach(11); // attaches the servo on pin 9 to the servo object
lcd.begin(16, 2); // set up number of columns and rows
}
void loop() {
char key = keypad.getKey();
if (key){
decide();
}
else{
message("","");
}
}
void decide(){
write_pass_at (0,0, "PLEASE ENTER CODE");
get_str(4);
if(input_password == password_1 ) {
alarm_disable = 0;
open_door();
} else if(input_password == "*#*#"){
message("GOING TO","SETTING");
delay(1500);
setting();
}
else if (input_password == "####"){
alarm_disable = 1;
motion_detect();
}
else if(input_password != password_1 ){
warning();
}
}
void motion_detect(){
message ("MOTION SENSOR","ENABLED");
while(alarm_disable){
char key = keypad.getKey();
val = digitalRead(Motion_sen); // read input value
if(val == HIGH) { // check if the input is HIGH
digitalWrite(red_led, HIGH); // turn LED ON
if (pirState == LOW) {
message("UNAUTHORIZED MOTION","DETECTED");
digitalWrite(red_led, HIGH); // unlock the door for 20 seconds
total = 5;
delay(3000);
//ALARM();
//MESSAGE TO USER PHONE
pirState = HIGH;
}
} else {
digitalWrite(red_led, LOW); // turn LED OFF
if(pirState == HIGH) {
message("UNAUTHORIZED MOTION","ENDED");
pirState = LOW;
delay(1500);
message ("","");
}
}
if (key){
total=5;
message ("","");
decide ();
key = 0;
}
}
}
void save_state(){
}
void message(String c, String c2){
lcd.clear();
lcd.setCursor (0,0);
lcd.print(c);
lcd.setCursor (0,1);
lcd.print(c2);
}
void write_pass_at(int col, int row,String c){
lcd.setCursor(col,row);
lcd.print(c);
}
void get_str(int c){
count = 0;
input_password ="";
for (;count < c;){
char key = keypad.getKey();
// motion_detect();
if (key){
write_pass_at (count,1, "*");
input_password += key;
count++;
key=0;
delay(50);
}
}
}
void setting(){
message("YOU ARE INSIDE","SETTING");
delay (2000);
manual();
message("PLEASE INSERT", "YOUR CHOICE");
delay (2000);
message("YOUR CHOICE","");
get_str (4);
if(input_password == "****"){
message("PLEASE INSERT", "OLD PASSWORD");
delay(1500);
message("OLD PASSWORD","");
delay(1000);
get_str (4);
if (input_password == password_1){
message("NEW PASSWORD", "");
delay (1000);
get_str (4);
String *p = &password_1;
*p = input_password;
message("PASSWORD CHANGED","SUCESSFULLY");
delay(1500);
message("","");
total = 0;
}
else{
warning();
}
}
else{
message("","");
}
}
void manual(){
message("INSERT '****'", "TO CHANGE PASS");
delay (2000);
message("INSERT '####'", "TO EXIT SETING");
delay (2000);
}
void open_door(){
message (" WELCOME", "OPENED FOR 10Se");
digitalWrite(green_led, HIGH); // unlock the door for 20 seconds
servo.write(0); // tell servo to go to position in variable 'pos'
delay(10000);
servo.write(130); // tell servo to go to position in variable 'pos'
digitalWrite(green_led, LOW); // lock the door
count = 0;
total = 0;
message ("","");
}
void warning(){
message ("WRONG PASSWORD", "TRY AGAIN");
delay(1000);
if (++total < 4){
message ((String)total, "times YOU TRIED");
delay(1000);
message ("","");
}
else{
digitalWrite(red_led, HIGH); // unlock the door for 20 seconds
message ((String)total, "times YOU TRIED");
delay (1000);
if (total == 6){
message ("YOU ARE UNAUTHORIZED","INDIVIDUAL");
delay (2000);
message ("ALARM!!!!", "ALARM!!!!");
delay (1000000);
}
else{
message ("YOU ARE BLOCKED", "FOR 10 MNTS");
delay(10000);
}
message ("", "");
digitalWrite(red_led, LOW); // unlock the door for 20 seconds
}
}