#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Adafruit_NeoPixel.h>
#include <Servo.h>


#define buzzerPIN 3
#define servoPIN  6
#define pirPIN    8
#define neoPIN    12
#define NUMPIXELS 16
#define DS1307_ADDRESS 0x68

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, neoPIN, NEO_GRB + NEO_KHZ800);
Servo myservo;

void pinDirs() {
    pinMode(pirPIN, INPUT);
    pinMode(neoPIN, OUTPUT);
    pinMode(buzzerPIN,OUTPUT);
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

uint8_t bcdToDec(uint8_t value)
{
  // Converts from BCD to decimal
  return ((value / 16 * 10) + (value % 16));
}

void getTime() {
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 0x07);

  uint8_t seconds = bcdToDec(Wire.read());
  uint8_t minutes = bcdToDec(Wire.read());
  uint8_t hours = bcdToDec(Wire.read() & 0xff);
  uint8_t wday = bcdToDec(Wire.read());
  uint8_t mday = bcdToDec(Wire.read());
  uint8_t month = bcdToDec(Wire.read());
  uint8_t year = bcdToDec(Wire.read());
 // Shows the data on the display
  lcd.setCursor(0,1);
  lcd.print("Time is ");

  // Adds 0 (clear) if the time is less than 10
  if (hours < 10)
    lcd.print("0");

  lcd.print(hours);
  if (seconds & 1)
    lcd.print(":");
  else
    lcd.print(" ");

  // Adds 0 (clear) if minutes are less than 10
  if (minutes < 10)
     lcd.print("0");

  lcd.print(minutes);

  if (seconds & 1)
    lcd.print(":");
  else
    lcd.print(" ");

  if (seconds < 10)
     lcd.print("0");

  lcd.print(seconds);

 
}

void setup()
{
  Wire.begin();
	lcd.init();
	lcd.backlight();
  strip.begin();
  myservo.attach(servoPIN);
  lcd.println("...setting up....");
  colorWipe(strip.Color(0, 0, 255), 50); // blue
  myservo.write(0);              // tell servo to go to position in variable 'pos'
  delay(15);              
	// Initialize the serial port at a speed of 9600 baud
	Serial.begin(9600);
  Serial.println("LCD I2C Example: ");
}

void loop()
{
 lcd.clear();
 if (digitalRead(pirPIN)==LOW){
   lcd.setCursor(0,0);
   lcd.println("you have escaped");
   getTime();
   digitalWrite(buzzerPIN, HIGH);
   myservo.write(90);              // tell servo to go to position in variable 'pos'          
   colorWipe(strip.Color(255, 0, 0), 50); // Green
 } else {
   lcd.setCursor(0,0);
  lcd.println("I can see you!!");
  getTime();
  digitalWrite(buzzerPIN, LOW);
  myservo.write(180);              // tell servo to go to position in variable 'pos'            
  colorWipe(strip.Color(0, 255, 0), 50); // Green
 }
 delay(1000);
 
}
GND5VSDASCLSQWRTCDS1307+