//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>

Servo myservo;



#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 = 10; // pin to inside / emergencey switch

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 flag = 0; //Door opening Position


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



void setup()
{
  digitalWrite(redPin, HIGH); // 2 leds are glowing for system a ready signal
  digitalWrite(greenPin, HIGH);
 
  delay(3000);
  myservo.attach(9);
  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 (pled,OUTPUT);

  ServoClose();

}

void loop(){
 
DoorProg();

  if (digitalRead(sw1) == LOW)
{
  
     
      delay(100);
      ServoOpen();
      delay(1000);
      DOpen();
      flag =1;
      door = 0;

}
  
  
  // if (door == 0)
  // {
  //   customKey = customKeypad.getKey();

  //   if (customKey == '#')

  //   {
    
  //     delay(100);
  //     ServoClose();
  
  //     door = 1;
  //   }
  // }

  else
  
   Open();
   


}


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()
{
  for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // 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()
{
  for (pos = 0; pos <= 180; pos += 180) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // 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()
{

 
  
 
  
  customKey = customKeypad.getKey();
  if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  {
    Data[data_count] = customKey; // store char into data array
    (data_count, 1); // move cursor to show each new char
     // 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)
    {
      
      ServoOpen();
      delay(1000);
     DOpen();
     flag =1;
      
      
      door = 0;
      
    }
    else
    {
     
      delay(500);
      door = 1;
     
      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();
      delay(5000);
     DClose();
    
    
    }
EMERGENCEY:
  if((digitalRead(pir) == LOW)&& (flag == 2))
    
  {
    flag = 1;
   Stop();
   delay(2000);
    DOpen();
    
    
  }
DOOR_CLOSEEND:
  if ((digitalRead(closeend) == LOW)&&(flag == 2))
  {
    flag = 0;
     Stop();
    delay(100);
    ServoClose();
    door = 1;
    
    // goto DOOR_OPEN;
    
  }
}

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);
}

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