#include <Wire.h>

// =================================================================
// 1. SET THE DESIRED START TIME HERE (Use 24-hour format for hours)
// =================================================================
byte setSecond     = 50;   // 0-59
byte setMinute     = 25;  // 0-59
byte setHour       = 17;  // 0-23 (e.g., 14 for 2:00 PM)
byte setDayOfWeek  = 7;   // 1-7  (e.g., 1=Sunday, 2=Monday)
byte setDate       = 18;  // 1-31 (Day of the month)
byte setMonth      = 4;   // 1-12 (Month)
byte setYear       = 26;  // 0-99 (e.g., 26 for 2026)
// =================================================================

#define RTC_ADDRESS 104 // I2C address for the RTC (104 in decimal, 0x68 in hex)

void setup() {
  Wire.begin();
  Serial.begin(230400);
  
  // Wait for Serial Monitor to open before running
  while (!Serial) {
    ; 
  }
  
  Serial.println("Writing new time to the Real Time Clock...");
  
  // Begin the I2C transmission
  Wire.beginTransmission(RTC_ADDRESS);
  Wire.write(0); // Start at register pointer 0 (Seconds)
  
  // Write the time data in Binary Coded Decimal (BCD) format
  Wire.write(decToBcd(setSecond));
  Wire.write(decToBcd(setMinute));
  Wire.write(decToBcd(setHour));
  Wire.write(decToBcd(setDayOfWeek));
  Wire.write(decToBcd(setDate));
  Wire.write(decToBcd(setMonth));
  Wire.write(decToBcd(setYear));
  
  Wire.endTransmission();

  Serial.println("Time successfully set! Reading back data:");
  Serial.println("-----------------------------------------");
}

void loop() {
  // Read the time back from the RTC to verify it is ticking
  Wire.beginTransmission(RTC_ADDRESS);
  Wire.write(0); // Reset register pointer to 0
  Wire.endTransmission();
  
  Wire.requestFrom(RTC_ADDRESS, 7); // Request the 7 time/date bytes
  
  if (Wire.available() >= 7) {
    byte second = bcdToDec(Wire.read() & 0x7f);
    byte minute = bcdToDec(Wire.read());
    byte hour   = bcdToDec(Wire.read() & 0x3f);
    byte day    = bcdToDec(Wire.read());
    byte date   = bcdToDec(Wire.read());
    byte month  = bcdToDec(Wire.read());
    byte year   = bcdToDec(Wire.read());

    // Print to Serial Monitor
    Serial.print("RTC Time: ");
    if(month < 10) Serial.print("0"); Serial.print(month); Serial.print("/");
    if(date < 10) Serial.print("0"); Serial.print(date); Serial.print("/20");
    if(year < 10) Serial.print("0"); Serial.print(year); Serial.print("  ");
    
    if(hour < 10) Serial.print("0"); Serial.print(hour); Serial.print(":");
    if(minute < 10) Serial.print("0"); Serial.print(minute); Serial.print(":");
    if(second < 10) Serial.print("0"); Serial.println(second);
  }

  delay(1000); // Wait one second before printing again
}

// -----------------------------------------------------------------
// Helper Functions: RTCs require Binary Coded Decimal (BCD) format
// -----------------------------------------------------------------
byte decToBcd(byte val) {
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val) {
  return ( (val/16*10) + (val%16) );
}