#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
const int selectButton = 2;
const int upButton = 3;
const int downButton = 4;
const int okButton = 7;
const int saveButton = 8;
const int ledHijau = 9;
int day = 0;
int month = 0;
int year = 0;
DateTime selectedDate;
int days_to_hatch;
bool is_hatching = false;
const int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const char* dayNames[] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
int menuOption = 0;
int menuMax = 2;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
selectedDate = selectedDate;
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
pinMode(saveButton, INPUT_PULLUP);
pinMode(okButton, INPUT_PULLUP);
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
int current_day = now.day();
int current_month = now.month();
int current_year = now.year();
int current_day_of_week = now.dayOfTheWeek();
lcd.setCursor(10, 0);
lcd.print(String(current_day) + "/" + String(current_month) + "/" + String(current_year));
lcd.setCursor(10, 1);
lcd.print(String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()));
if (days_to_hatch < 0) {
days_to_hatch = 0;
}
updateHatchingStatus();
if (digitalRead(selectButton) == LOW) {
delay(250);
menuOption++;
if (menuOption > menuMax) {
menuOption = 0;
}
switch (menuOption) {
case 1:
day = current_day;
month = current_month;
year = current_year;
dateSelectionMode();
break;
case 2:
//menu kedua
break;
}
delay(250);
}
}
void dateSelectionMode() {
DateTime selectedDate(year, month, day, 0, 0, 0);
DateTime now = rtc.now();
TimeSpan difference = selectedDate - now;
days_to_hatch = difference.days() + 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pilih tggl menetas:");
lcd.setCursor(0, 1);
lcd.print(String(day) + "/" + String(month) + "/" + String(year));
lcd.setCursor(10, 1);
lcd.print("-->up/down");
lcd.setCursor(0, 2);
lcd.print("total 21 hari kdepan");
lcd.setCursor(0, 3);
lcd.print("tekan tombol --> OK");
while (menuOption == 1) {
if (digitalRead(upButton) == LOW) {
delay(200);
day++;
if (day > daysInMonth[month - 1]) {
day = 1;
month++;
}
selectedDate = DateTime(year, month, day, selectedDate.hour(), selectedDate.minute(), selectedDate.second());
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(String(day) + "/" + String(month) + "/" + String(year));
difference = selectedDate - now;
days_to_hatch = difference.days() + 1;
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("total " + String(days_to_hatch) + " hari kdepan");
}
if (digitalRead(downButton) == LOW) {
delay(200);
day--;
if (day < 1) {
month--;
if (month < 1) {
month = 12;
year--;
}
day = daysInMonth[month - 1];
}
selectedDate = DateTime(year, month, day, selectedDate.hour(), selectedDate.minute(), selectedDate.second());
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(String(day) + "/" + String(month) + "/" + String(year));
}
if (digitalRead(okButton) == LOW) {
delay(200);
lcd.clear();
//confirmationMode();
setDaysToHatch();
}
}
}
/*void confirmationMode() {
lcd.setCursor(0, 0);
lcd.print("Tanggal sudah benar?");
lcd.setCursor(0, 1);
lcd.print(String(day) + "/" + String(month) + "/" + String(year));
lcd.setCursor(0, 2);
lcd.print("Tekan OK lagi Untuk");
lcd.setCursor(0, 3);
lcd.print("cek jumlah hari");
while (menuOption == 1) {
if (digitalRead(okButton) == LOW) {
delay(200);
lcd.clear();
//menu = 3;
setDaysToHatch();
}
}
}*/
void setDaysToHatch() {
DateTime selectedDate(year, month, day, 0, 0, 0);
DateTime now = rtc.now();
TimeSpan difference = selectedDate - now;
days_to_hatch = difference.days() + 1;
DateTime startDate = selectedDate - TimeSpan(days_to_hatch, 0, 0, 0);
lcd.setCursor(0, 0);
lcd.print("Tgl tetas: ");
lcd.setCursor(11, 0);
lcd.print(String(day) + "/" + String(month) + "/" + String(year));
lcd.setCursor(0, 1);
lcd.print("Jmlh hari menetas:");
lcd.print(days_to_hatch);
lcd.setCursor(0, 2);
lcd.print("Tgl mulai: " + String(startDate.day()) + "/" + String(startDate.month()) + "/" + String(startDate.year()));
lcd.setCursor(0, 3);
lcd.print("Tekan tombol SAVE...");
while (menuOption == 1) {
if (digitalRead(saveButton) == LOW) {
delay(200);
is_hatching = true;
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Pengaturan diset");
delay(1500);
lcd.clear();
menuOption = 0;
}
}
}
void calculateDaysToHatch(DateTime selectedDate) {
DateTime now = rtc.now();
TimeSpan difference = selectedDate - now;
int days_passed = 21 - difference.days();
//Serial.println(days_to_hatch);
//Serial.println(days_passed);
//koding untuk di wokwi
if (days_passed < 0) {
days_passed = 0;
}
else if (days_passed > 21) {
days_passed = 0;
} else {
days_to_hatch = 21 - days_passed;
}
// Koding di IDE
/*if (days_passed < 0) {
days_passed = 0;
}
else {
days_to_hatch = 21 - days_passed;
}*/
//DateTime hatch_date = selectedDate - TimeSpan(hatching_day, 0, 0, 0);
DateTime hatch_date = selectedDate;
lcd.setCursor(0, 2);
lcd.print("H: ");
//lcd.print("Jumlah hari:");
lcd.print(days_passed);
Serial.println(days_passed);
lcd.setCursor(5, 2);
lcd.print("TARg: " + String(hatch_date.day()) + "/" + String(hatch_date.month()) + "/" + (hatch_date.year()));
lcd.setCursor(0, 3);
lcd.print("Menetas dalam: ");
lcd.print(days_to_hatch);
lcd.print(" hr");
}
void updateHatchingStatus() {
if (is_hatching) {
if (selectedDate.year() != 0) {
//calculateDaysToHatch(DateTime(year, month, day, 0, 0, 0)); --> jika tidak menggunakan data dari eeprom
calculateDaysToHatch(selectedDate);
if (days_to_hatch > 0) {
//hitungMundur();
digitalWrite(ledHijau, HIGH);
} else if (days_to_hatch == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Egg has hatched!");
lcd.setCursor(0, 1);
lcd.print("Remove the chick.");
lcd.setCursor(0, 2);
lcd.print("Tekan SELECT untuk");
lcd.setCursor(0, 3);
lcd.print("mengatur waktu tetas");
is_hatching = false;
if (digitalRead(selectButton) == LOW) {
delay(200);
lcd.clear();
menuOption = 0;
}
} else {
/*lcd.setCursor(0, 2);
lcd.print("Belum ada proses");
lcd.setCursor(0, 3);
lcd.print("penetasan telur");*/
}
} else {
lcd.setCursor(0, 2);
lcd.print("Waktu penetasan blum");
lcd.setCursor(0, 3);
lcd.print("di atur,tekan SELECT");
}
}
}