#include <Arduino.h>
#include <uRTCLib.h>
#include <LiquidCrystal_I2C_Hangul.h>
const byte button_set = 11;
const byte button_up = 12;
const byte button_down = 13;
const byte relay = 10;
const byte buzzer = 9;
byte up_ditekan;
int down_ditekan;
byte set_ditekan;
byte tombol_up;
int tombol_down;
int set_hh;
int set_mm;
int set_ss;
byte menuSet;
byte commitSet;
uRTCLib rtc(0x68);
LiquidCrystal_I2C_Hangul lcd = LiquidCrystal_I2C_Hangul(0x27,16,2);
char daysOfTheWeek[7][12] = {"AHAD", "SENIN", "SELASA", "RABU", "KAMIS", "JUM'AT", "SABTU"};
void setup() {
pinMode(button_set, INPUT_PULLUP);
pinMode(button_up, INPUT_PULLUP);
pinMode(button_down, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
tombol_up = 0;
tombol_down = 0;
set_ditekan = 0;
set_hh = 0;
set_mm = 0;
set_ss = 0;
Serial.begin(9600);
URTCLIB_WIRE.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("SMART PROJECTS");
lcd.setCursor(1,1);
lcd.print("BURHAN ALFARISI");
delay(2000);
lcd.clear();
delay(300);
}
void lcdDT(){
rtc.refresh();
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[rtc.dayOfWeek()-1]);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(rtc.day());
lcd.print('/');
lcd.print(rtc.month());
lcd.print('/');
lcd.print(rtc.year());
lcd.setCursor(8, 1);
lcd.print(rtc.hour());
lcd.print(':');
lcd.print(rtc.minute());
lcd.print(':');
if (rtc.second() < 10) {
lcd.print(0);
}
lcd.println(rtc.second());
//delay(1000);
}
void loop() {
lcdDT();
// cek kondisi tombol set
if (set_ditekan == 5){
set_ditekan = 0;
}
if (digitalRead(button_set) != 1 ){
set_ditekan++;
menuSet = 2; // angka 2 adalah 2x tombol set ditekan
delay(200);
// Menu Set waktu relay
while (set_ditekan == menuSet){
// variable tombol_up bertambah 1 bila tombol up ditekan 1x
if (digitalRead(button_up) != 1 ){
tombol_up++; set_hh++; set_mm++; set_ss++;
if(tombol_up > 59){
tombol_up = 0; set_hh = 0 set_mm = 59; set_ss = 59;
if(set_hh > 23){ // mode 24 jam
set_hh = 0;
}
if(set_mm >59){
set_mm = 0;
}
if (set_ss >59){
set_ss = 0;
}
}
}
// variable tombol_down berkurang 1 bila tombol down ditekan 1x
if (digitalRead(button_down) != 1 ){
tombol_down--; set_hh--; set_mm--; set_ss--;
if(tombol_down < 0){
tombol_down = 59; set_hh = 23 set_mm = 59; set_ss = 59;
if(set_hh < 1){ // mode 24 jam
set_hh = 0;
}
if(set_mm <0){
set_mm = 59;
}
if (set_ss <0){
set_ss = 59;
}
}
}
if (digitalRead(button_set) != 1 ){
set_ditekan++;
delay(200);
}
Serial.println();
Serial.print("\ntombol set : ");
Serial.print(set_ditekan);
Serial.print("\ntombol up : ");
Serial.print(tombol_up);
Serial.print("\ntombol down : ");
Serial.print(tombol_down);
delay(200);
}
// commit & apply Set waktu relay
commitSet = 4;
while (set_ditekan == commitSet){
Serial.println();
Serial.print("\nRelay Aktif Pukul : ");
Serial.print(set_hh);
Serial.print(" : ");
Serial.print(set_mm);
Serial.print(" : ");
Serial.print(set_ss);
if (digitalRead(button_set) != 1 ){
set_ditekan++;
delay(200);
}
delay(1000);
}
Serial.println();
Serial.print("\ntombol set : ");
Serial.print(set_ditekan);
Serial.print("\ntombol up : ");
Serial.print(tombol_up);
Serial.print("\ntombol down : ");
Serial.print(tombol_down);
delay(200);
}
}