/*
-----------------------------------------------------------------------------
PROJECT TITLE : SMART DOOR BELL SYSTEM 
------------------------------------------------------------------------------
*/

//include for the program
#include <Servo.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

Servo myservo;

int pos = 0;

//tft define
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

int ledPin = 8;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;     
int buzzer = 6; 

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input  
  pinMode(buzzer, OUTPUT); 
  myservo.attach(10);

  //tft screen display program(setup)
  tft.begin();

  tft.setCursor(25, 0);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2.5);
  tft.println("  WELCOME TO");

  tft.setCursor(13, 45);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2.5);
  tft.println(" SMART DOOR ALARM");

  tft.setCursor(0, 95);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2.5);
  tft.println("     THIS IS A");

  tft.setCursor(0, 122);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2.5);
  tft.println("     DOOR ALARM");

  tft.setCursor(0, 150);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2.5);
  tft.println("     PUT HAND ");

  tft.setCursor(0, 180);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2.5);
  tft.println("     TO USE ME");

  tft.setCursor(0, 230);
  tft.setTextColor(ILI9341_ORANGE);
  tft.setTextSize(2.5);
  tft.println("     THANK YOU");

  tft.setCursor(0, 300);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2.5);
  tft.println(" - TEAM HLSL ");
}

 void loop() {
 val = digitalRead(inputPin);  
  if (val == HIGH) {            
    digitalWrite(ledPin, LOW);  
    if (pirState == LOW) {
        for (pos = 0; pos <= 180; pos += 1) { 
        // in steps of 1 degree
        myservo.write(pos);             
        delay(15);
  }
    
      pirState = HIGH;
      
        tone(buzzer, 1000); 
        digitalWrite(ledPin, HIGH);
        delay(1000);        
        noTone(buzzer);     
        digitalWrite(ledPin, LOW);
        delay(1000);    
        tone(buzzer, 1000); 
        digitalWrite(ledPin, HIGH);
        delay(1000);        
        noTone(buzzer);          
    }
  } else {
    digitalWrite(ledPin, LOW); 
    if (pirState == HIGH) {
          for (pos = 180; pos >= 0; pos -= 1) { 
            myservo.write(pos);              
            delay(15);   
          }
      pirState = LOW;
    }
  }
}