#include <Arduino.h>
#include <Ds1302.h>
#include <LiquidCrystal_I2C.h>

#define PIN_ENA 2
#define PIN_CLK 5
#define PIN_DAT 4

LiquidCrystal_I2C lcd(0x27,16,2);

// DS1302 RTC instance
Ds1302 rtc(PIN_ENA, PIN_CLK, PIN_DAT);

int flag_set = 0;


uint8_t parseDigits(char* str, uint8_t count)
{
    uint8_t val = 0;
    while(count-- > 0) val = (val * 10) + (*str++ - '0');
    return val;
}

const static char* WeekDays[] =
{
    "Senin,  ",
    "Selasa, ",
    "Rabu,   ",
    "Kamis,  ",
    "Jum'at, ",
    "Sabtu,  ",
    "Minggu, "
};


void setup()
{
    Serial.begin(115200);
    // initialize the RTC
    rtc.init();
    
    lcd.init();
    lcd.backlight();
    lcd.clear();
    delay(1000);
    lcd.setCursor(0,0);
    lcd.print(" Input Set Time ");
    lcd.setCursor(0,1);
    lcd.print(" YYMMDDWhhmmss  ");
        
    Serial.println("Input the date and time (YYMMDDWhhmmss): ");
}


void loop()
{
    static char buffer[13];
    static uint8_t char_idx = 0;

    if (char_idx == 13)
    {
        Serial.println("data masuk");
        // structure to manage date-time
        Ds1302::DateTime dt;

        dt.year = parseDigits(buffer, 2);
        dt.month = parseDigits(buffer + 2, 2);
        dt.day = parseDigits(buffer + 4, 2);
        dt.dow = parseDigits(buffer + 6, 1);
        dt.hour = parseDigits(buffer + 7, 2);
        dt.minute = parseDigits(buffer + 9, 2);
        dt.second = parseDigits(buffer + 11, 2);

        // set the date and time
        rtc.setDateTime(&dt);

        char_idx = 0;

        Serial.print("rtc set");
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" Alhamdulillah ");
        lcd.setCursor(0,1);
        lcd.print("Time HasBeen Set");
        
        flag_set = 1;
        delay(3000);
        
        lcd.clear();
    }

    if (Serial.available())
    {
        buffer[char_idx++] = Serial.read();
//        Serial.write(buffer[char_idx++]);
    }

    if (flag_set == 1) {
      Ds1302::DateTime now;
      rtc.getDateTime(&now);
  
      static uint8_t last_second = 0;
      if (last_second != now.second)
      {
          last_second = now.second;
  
          Serial.print("20");
          Serial.print(now.year);    // 00-99
          Serial.print('-');
          if (now.month < 10) Serial.print('0');
          Serial.print(now.month);   // 01-12
          Serial.print('-');
          if (now.day < 10) Serial.print('0');
          Serial.print(now.day);     // 01-31
          Serial.print(' ');
          Serial.print(WeekDays[now.dow - 1]); // 1-7
          Serial.print(' ');
          if (now.hour < 10) Serial.print('0');
          Serial.print(now.hour);    // 00-23
          Serial.print(':');
          if (now.minute < 10) Serial.print('0');
          Serial.print(now.minute);  // 00-59
          Serial.print(':');
          if (now.second < 10) Serial.print('0');
          Serial.print(now.second);  // 00-59
          Serial.println();

          lcd.setCursor(0,0);
          lcd.print(WeekDays[now.dow - 1]);
          lcd.print(now.day);
          lcd.print('/');
          lcd.print(now.month);
          lcd.print('/');
          lcd.print(now.year);
          lcd.setCursor(0,1);
          lcd.print("Jam : ");
          if(now.hour<10) lcd.print('0');
          lcd.print(now.hour);
          lcd.print(':');
          if(now.minute<10) lcd.print('0');
          lcd.print(now.minute);
          lcd.print(':');
          if(now.second<10) lcd.print('0');
          lcd.print(now.second);
      }
      
    }
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
DS1302Breakout
OF05ZATBreakout