#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
RTC_DS1307 RTC;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
String now_time;
#include <Servo.h>
Servo door1;
int led1= 6;
int PIR = 4;
int sw_out =5;
void setup() {
RTC.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print(" ROOM CHECK IN");
door1.attach(3);
Serial.begin(9600);
pinMode(PIR, INPUT);
pinMode(sw_out, INPUT_PULLUP);
pinMode(led1,OUTPUT);
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
door1.write(0);
}
void loop() {
DateTime nowTime = RTC.now();
Serial.print(nowTime.hour(), DEC);
Serial.print(':');
Serial.print(nowTime.minute(), DEC);
Serial.print(':');
Serial.print(nowTime.second(), DEC);
Serial.println();
now_time = "Time = " +String(nowTime.hour())+":"+String(nowTime.minute())+":"+String(nowTime.second())+" ";
//lcd.clear();
lcd.setCursor(1,0);
lcd.print(" ROOM CHECK IN");
lcd.setCursor(1,1);
lcd.print(now_time);
int PIR1 = digitalRead(PIR);
if (PIR1 == 1 )
{
// ตรวจสอบเวลาที่ก่อน 10:12:00
if (nowTime.hour() < 11 || (nowTime.hour() == 11 && nowTime.minute() < 26))
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Ok Welcome");
lcd.setCursor(1, 1);
lcd.print(now_time);
moveservo();
moveclose();
}
// ตรวจสอบเวลาที่หลัง 10:12:00
else
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Come late");
lcd.setCursor(1, 1);
lcd.print(now_time);
moveservo();
moveclose();
}
}
if (digitalRead(sw_out) == LOW)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Door open");
moveservo();
moveclose();
}
delay(1000);
Serial.println(PIR1);
}
void moveservo ()
{
for (int pos = 0 ; pos <=90 ; pos += 1)
{
door1.write(pos);
delay(100);
}
}
void moveclose ()
{
for (int pos1 = 90 ; pos1 >=0 ; pos1 -= 1)
{
door1.write(pos1);
delay(30);
}
}