//project written by Subhadip Biswas
//More information & support my channel www.youtube.com/techzero
//download liquidcrystal laibary
//download keypad laibary
//I dont use display but you can use,i also written code for display
//Give a schematic in my website https://techzeroyoutube.blogspot.com
//I am use home made keypad.do you find how to make it gotoA my youtube channel www.youtube.com/techzero



#include <Keypad.h>
//#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h> 

//Servo myservo;

LiquidCrystal_I2C lcd(0x27,16,2); 

#define Password_Lenght 5 // Give enough room for 4 chars + NULL char

int pos = 0;    // variable to store the servo position

char Data[Password_Lenght]; // 4 is the number of chars it can hold + the null char = 5
char Master[Password_Lenght] = "1234";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;

int redPin = A2; //pin to red led
int greenPin = A3; //pin to green led
int sw1 = 9; // pin to inside / emergencey switch
int lock = 10;
int IN1 = A0; // DOOR MOTOR OPEN ROTATION
int IN2 = A1; // DOOR MOTOR CLOSE ROTATION
int openend = 12; // DOOR OPEN LIMITE SWITCH
int closeend = 13; // DOOR CLOSE LIMITE SWITCH &  DOOR OPENING SIGNAL 
int pir = 11; // FOR DOOR CLOSEING SEQURITY
int EMS = 0; //Sudden Stop
int PIRLED = 1; //Emergancy Idicuter

int flag = 0; //variable
int doorpos = 0; //variable
int sig = 0; //variable

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
bool door = true;

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8}; //connect to the column pinouts of the keypad

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad

// int ExterIn = LOW;


void setup()
{
  lcd.init();
  lcd.backlight();
  digitalWrite(redPin, HIGH); // 2 leds are glowing for system a ready signal
  digitalWrite(greenPin, HIGH);
  lcd.begin(16,2);
  lcd.home ();
  lcd.print("  -WELCOME TO-");
  lcd.setCursor(0, 1);
  lcd.print("  -SMART DOOR-");
  digitalWrite(lock, HIGH);
  delay(3000);
  pinMode(lock,OUTPUT);
  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(sw1,INPUT_PULLUP);

  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(openend,INPUT_PULLUP);
  pinMode(closeend,INPUT_PULLUP);
  pinMode(pir,INPUT_PULLUP);
  pinMode (EMS, INPUT_PULLUP);
  pinMode(PIRLED, OUTPUT);
  ServoClose();
   lcd.clear();

}

void loop(){
DEMSTOP();
//  lcd.clear();
 Open();

DoorProg();
 


  if ((digitalRead(sw1) == LOW)&&(sig == 0))
  {InputSig();
  sig = 1;}

  else
   if (door == 0)
  {
   lcd.clear();
   lcd.print("WAITING........");
   delay(100);
    lcd.clear();

  }

  // if (doorpos == 1)
  // {
  //  lcd.clear();
  // //  lcd.print("WAITING........");
  //  delay(100);

  // }
 
}



void clearData()
{
  while (data_count != 0)
  { // This can be used for any array size,
    Data[data_count--] = 0; //clear array for new data
  }
  return;
}

void ServoOpen()
{
    digitalWrite(lock, HIGH);         // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    digitalWrite(redPin, LOW);
     
    digitalWrite(greenPin, HIGH); // signal for Door Open & Password Corect
  
}

void ServoClose()
{
    digitalWrite(lock, LOW);              // tell servo to go to position in variable 'pos'
    delay(20);                       // waits 20ms for the servo to reach the position
    digitalWrite(redPin, HIGH); // signal for door is close
    digitalWrite(greenPin, LOW);
  
}

void Open()
{
  lcd.setCursor(0, 0);
  lcd.print(" ENTER PASSWORD");


  customKey = customKeypad.getKey();
  if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  {
   Data[data_count] = customKey ;
    lcd.setCursor(data_count, 1);
      lcd.print('*');// print char at said cursor and show *
    data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
   
  }

  if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  {
    if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)
    {
      lcd.clear();
      ServoOpen();
      lcd.setCursor(0, 0);
      lcd.print("  --WELCOME--");
      lcd.setCursor(0, 1);
      lcd.print("  DOOR IS OPEN");
      delay(1000);
      door = 0;
      delay(1000);
      DOpen();
      flag =1;
      doorpos = 0;
      
    }
    else
    {
       lcd.clear();
     lcd.setCursor(0, 0);
      lcd.print(" WRONG PASSWORD");
      lcd.setCursor(0, 1);
      lcd.print("   TRY AGAIN");
      delay(500);
      door = 1;
      lcd.clear();
     
      digitalWrite(redPin, LOW);
      delay(100);
      digitalWrite(redPin, HIGH);
      delay (100);
       digitalWrite(redPin, LOW); // signal for wrong password
      delay(100);
      digitalWrite(redPin, HIGH);
      delay (100);
    }
     clearData();
  }
}

void DoorProg()
{  
DOOR_CLOSE:  
  if ((digitalRead(openend) == LOW)&&(flag == 1))
    {
     flag = 2;
       
       Stop();

    lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("-DOOR IS  READY-");
      lcd.setCursor(0, 1);
      lcd.print("  --TO CLOSE--");

      delay(10000);

     DClose();
    sig = 1;
    
    }
EMERGENCEY:
  if((digitalRead(pir) == LOW)&& (flag == 2))
    
  {
    flag = 1;
   Stop();
   lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" -ERROR! DOOR-");
      lcd.setCursor(0, 1);
      lcd.print("  --IS COVER--");
      digitalWrite(PIRLED, HIGH);
   delay(5000);
    DOpen();
     digitalWrite(PIRLED, LOW);
    
    
  }
DOOR_CLOSEEND:
  if ((digitalRead(closeend) == LOW)&&(flag == 2))
  {
    
     Stop();
     lcd.clear();
 
    for(int count = 3; count>=0; count --){
  lcd.setCursor(6, 1);
  lcd.print((String(count))+" s");
  lcd.setCursor(0, 0);
     lcd.print("-DOOR IS CLOSED-");
     delay(1000);
    ServoClose();
    door = 1;
    lcd.clear(); 
    sig = 0;
    flag = 0;
    // digitalWrite(sw1, LOW);
    }
    
    
  }
}

void DOpen() {
  //digitalWrite(ENA, HIGH);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
}


void DClose() {
  //digitalWrite(ENA, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
}
void Stop() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
}

void DEMSTOP()
{
  if ((digitalRead(EMS) == LOW) && (doorpos == 0)&&(sig == 1)){
 Stop();
 doorpos = 1;
 flag = 2;
 delay(3000);
 digitalWrite(lock, LOW);
 lcd.clear(); 
  }
  if ((digitalRead(EMS) == HIGH) && (doorpos == 1) &&(flag == 2)){
//  flag = 2;
 doorpos = 0;
 DClose();
//  sig = 1;
 digitalWrite(lock, HIGH);
//  doorpos = 1;

  }

}

void InputSig(){

   lcd.clear();
     lcd.print("DOOR IS OPENING");
      delay(100);
      ServoOpen();
      delay(1000);
      DOpen();
      flag =1;
      door = 0;
      doorpos = 0;

}

/////////// PROJECT WRITTEN BY SUBHADIP BISWAS //////////
////////// THANK YOU ///////////