#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 RTC;

byte rele = 9;
byte buzz = 11;
Servo servo1;

struct alarm {
  int jam;
  int menit;
};

void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  RTC.begin();

  pinMode(13, OUTPUT);       // inisiate LED onboard
  digitalWrite(13, LOW);     // inisiate LED onboard

  pinMode(buzz,OUTPUT);      // inisiate buzzer pin
  pinMode(rele, OUTPUT);     // inisiate rele pin
  digitalWrite(rele, LOW);   // rele kondisi off
  servo1.attach(10);
  servo1.write(0);           // katup tertutup

  lcd.setCursor(4,0);
  lcd.print("Project");
  lcd.setCursor(2,1);
  lcd.print("Digital Clock");
  delay(1500);
  lcd.clear();

  DateTime readRTC = RTC.now();

}

void loop() {
  // put your main code here, to run repeatedly:
  DateTime readRTC = RTC.now();
  home_display();
  lcd.setCursor(0,0);

  struct alarm action01; // seting waktu alarm ke-1
  action01.jam = 18;
  action01.menit = 14;

  struct alarm action02; // setting wkatu alarm ke-2
  action02.jam = 18;
  action02.menit = 15;

  if (readRTC.hour() == action01.jam && readRTC.minute() == action01.menit && readRTC.second() == 0)
  {
    action1();
    delay(3000);

    digitalWrite(13, LOW);
    noTone(buzz);
    servo1.write(0);                // katup tertutup
    digitalWrite(rele, LOW);        //Relay off 
  }

  else if (readRTC.hour() == action02.jam && readRTC.minute() == action02.menit && readRTC.second() == 0)
  {
    action2();
    delay(3000);

    digitalWrite(13, LOW);
    noTone(buzz);
    servo1.write(0);                // katup tertutup
    digitalWrite(rele, LOW);        //Relay off
  }

  else
  {
    digitalWrite(13, LOW);
    noTone(buzz);
    servo1.write(0);                //katup tertutup
    digitalWrite(rele, LOW);        //Relay off
    home_display();
  }
}

void home_display()
{
  DateTime readRTC = RTC.now();

  lcd.setCursor(0,0);
  lcd.print("DATE: ");
  lcd.print(readRTC.year());
  lcd.print("/");
  lcd.print(readRTC.month());
  lcd.print("/");
  lcd.print(readRTC.day());

  lcd.setCursor(0,1);
  lcd.print("TIME: ");
  lcd.print(readRTC.hour());
  lcd.print("/");
  lcd.print(readRTC.minute());
  lcd.print("/");
  lcd.print(readRTC.second());
  lcd.print("    ");
}

void action1()
{
  DateTime readRTC = RTC.now();
  digitalWrite(13, HIGH);
  tone(buzz,200);
  servo1.write(180);               // katup terbuka
  digitalWrite(rele, HIGH);        //Relay on

  lcd.setCursor(0,0);
  lcd.print("DATE: ");
  lcd.print(readRTC.year());
  lcd.print("/");
  lcd.print(readRTC.month());
  lcd.print("/");
  lcd.print(readRTC.day());

  lcd.setCursor(0,1);
  lcd.print("ALARM STAGE 1 !!"); 
}

void action2()
{
  DateTime readRTC = RTC.now();
  digitalWrite(13, HIGH);
  tone(buzz,200);
  servo1.write(180);               // katup terbuka
  digitalWrite(rele, HIGH);        //Relay on

  lcd.setCursor(0,0);
  lcd.print("DATE: ");
  lcd.print(readRTC.year());
  lcd.print("/");
  lcd.print(readRTC.month());
  lcd.print("/");
  lcd.print(readRTC.day());

  lcd.setCursor(0,1);
  lcd.print("ALARM STAGE 2 !!"); 
}
GND5VSDASCLSQWRTCDS1307+
NOCOMNCVCCGNDINLED1PWRRelay Module