//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>
#include <SPI.h>
#include <MFRC522.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; // For Lock/Servo
int IN1 = A0; // DOOR MOTOR OPEN ROTATION
int IN2 = A1; // DOOR MOTOR CLOSE ROTATION
int openend = 2; // DOOR OPEN LIMITE SWITCH
int closeend = 3; // DOOR CLOSE LIMITE SWITCH &  DOOR OPENING SIGNAL
int pir = 4; // FOR DOOR CLOSEING SEQURITY
int EMS = 0; //Sudden Stop
int PIRLED = 1; //Emergancy Idicuter
#define SS_PIN 5  //RFID SDA PIN
#define RST_PIN 6 //RFID RESET PTN

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;
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
int statuss = 0;
int out = 0;

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();
   SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522

}

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(" TOUCH RFID");


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

  // }
    // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  //Show UID on serial monitor
  Serial.println();
  Serial.print(" UID tag :");
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  Serial.println();
  if (content.substring(1) == "5A 45 0A B1" , "A3 3F 51 AC") //change UID of the card that you want to give access
  {
    Serial.println(" Access Granted ");
    Serial.println(" Welcome ");
    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;
    // Firebase.setString(fbdo, "/Home/Swigik/Door/open", "1" );
    delay(1000);
    Serial.println(" DOOR IS OPEN ");
    Serial.println();
    statuss = 1;
  }

  // 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(20000);

    DClose();
    sig = 1;

  }
EMERGENCEY:
  if ((digitalRead(pir) == HIGH) && (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(10000);
    digitalWrite(PIRLED, LOW);
    delay(1000);
    DOpen();


  }
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(2000);
    digitalWrite(lock, LOW);
    lcd.clear();
  }
  if ((digitalRead(EMS) == HIGH) && (doorpos == 1) && (flag == 2)) {
    //  flag = 2;
    DClose();
    sig = 0;
    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 ///////////