#include <Wire.h> // Library untuk komunikasi I2C
#include <LiquidCrystal_I2C.h> // Library untuk LCD I2C
#include <RTClib.h> // Library untuk Real-time Clock
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27,16,2); //Mengakses alamat LCD
const int BalkonDanTerasRelay = 13; // Pin untuk relay lampu balkon dan teras
const int RuangTengahDanKamarRelay = 12; // Pin untuk relay lampu ruang tengah dan kamar
void setup() {
lcd.init(); // Reset LCD
lcd.backlight(); // Menyalakan background LCD
rtc.begin(); // Mulai Real time clock
pinMode(BalkonDanTerasRelay, OUTPUT);
pinMode(RuangTengahDanKamarRelay, OUTPUT);
rtc.adjust(DateTime(2023, 10, 11, 7, 30, 0));
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Tgl : "); lcd.print(now.day()); lcd.print("/"); lcd.print(now.month()); lcd.print("/"); lcd.print(now.year());
lcd.setCursor(0,1);
lcd.print("Jam : "); lcd.print(now.hour()); lcd.print(":"); lcd.print(now.minute()); lcd.print(":"); lcd.print(now.second());
;
if (now.hour() >= 18 && now.minute() >= 0 && now.second() >=0 ||now.hour()<6) {
// Saat pukul 18.00
digitalWrite(BalkonDanTerasRelay, HIGH); //Nyalakan lampu Teras dan Balkon
} else if (now.hour() >= 6 && now.minute() >= 0) {
// Saat pukul 06.00
digitalWrite(BalkonDanTerasRelay, LOW); // Matikan lampu Teras dan Balkon
}
if (now.hour() >= 17 && now.minute() >= 0 && now.second() >= 0 || now.hour()<6) {
// Saat pukul 17.00
digitalWrite(RuangTengahDanKamarRelay, HIGH); //Nyalakan lampu Ruang Tengah dan Kamar
} else if (now.hour() >= 6 && now.minute() >= 0) {
// Saat pukul 06.00
digitalWrite(RuangTengahDanKamarRelay, LOW); //Matikan lampu Ruang Tengah dan Kamar
}
}