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

// ESP 32 TO RFID PIN IS
// SDA > 21
// SCK > 18
// MOSI > 23
// MISO > 19
// RST > 5

#include "Define_Pin_Library.h"
#include "WifiSet.h"
#include "FirebaseSet.h"
#include "OtaSet.h"


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);
  if (initWiFi()) {
    OTA();
    Fire_set();
    wifi = 1;
  }
  else {
    wifi = 0;
  }
  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();
  delay(1000);
  lcd.setCursor(0, 0);
  lcd.print(" ENTER PASSWORD");
}

void loop() {
  DEMSTOP();
  // Open();
  DoorProg();

  if (wifi == 1) {
    OTA_loop();
    Fire_loop();
    //    RfidLoop();
  }

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

  }

}





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

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(1000);
    lcd.clear();
    for (int count = 20; count >= 0; count --) {
      lcd.setCursor(6, 1);
      lcd.print((String(count)) + " s");
      lcd.setCursor(0, 0);
      lcd.print("-WAIT TO CLOSE-");
      delay(1000);
    }
    lcd.clear();
    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(1000);
    lcd.clear();
    for (int count = 10; count >= 0; count --) {
      lcd.setCursor(6, 1);
      lcd.print((String(count)) + " s");
      lcd.setCursor(3, 0);
      lcd.print("- ERROR -");
      delay(1000);
    }
    lcd.clear();
    digitalWrite(PIRLED, LOW);
    delay(1000);
    DOpen();
  }
DOOR_CLOSEEND:
  if ((digitalRead(closeend) == LOW) && (flag == 2))
  {

    Stop();
    lcd.clear();

    for (int count = 5; count >= 0; count --) {
      lcd.setCursor(3, 1);
      lcd.print("IN");
      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;
    delay(1000);
    lcd.setCursor(0, 0);
    lcd.print(" ENTER PASSWORD");
  }
}

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 (flag == 1) {
    if ((digitalRead(EMS) == LOW)) {
      Stop();
      doorpos = 1;
      // flag = 2;
      delay(2000);
      digitalWrite(lock, LOW);
      lcd.setCursor(0, 0);
      lcd.print(" DOOR OPEN PAUSE");
      // lcd.clear();
    }
    if ((digitalRead(EMS) == HIGH)) {
      //  flag = 2;
      DOpen();
      sig = 0;
      digitalWrite(lock, HIGH);
      doorpos = 1;
      lcd.setCursor(0, 0);
      lcd.print("<<< DOOR OPENING");
      // lcd.clear();
    }
  }

  if (flag == 2) {
    if ((digitalRead(EMS) == LOW)) {
      Stop();
      doorpos = 1;
      flag = 2;
      delay(2000);
      digitalWrite(lock, LOW);
      lcd.setCursor(0, 0);
      lcd.print("DOOR CLOSE PAUSE");
      // lcd.clear();
    }
    if ((digitalRead(EMS) == HIGH)) {
      //  flag = 2;
      DClose();
      sig = 0;
      digitalWrite(lock, HIGH);
      doorpos = 1;
      lcd.setCursor(0, 0);
      lcd.print("DOOR CLOSING >>>");
      // lcd.clear();
    }
  }

}

void InputSig() {

  lcd.clear();
  lcd.print("-INSIDE OPENING-");
  delay(100);
  ServoOpen();
  delay(2000);
  DOpen();
  flag = 1;
  door = 0;
  doorpos = 0;

}

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