Set the time with RtcDS1302 library

Hi - I'm using the library at: https://github.com/Makuna/Rtc/tree/master/src
to display the time. The example code shows setting of the time from the current time. I need to set the time according to a user input (for a Nixie Clock project), so I'm looking at the function in the library code:

 void SetDateTime(const RtcDateTime& dt)
    {
        // set the date time
        _wire.beginTransmission(DS1302_REG_TIMEDATE_BURST);

        _wire.write(Uint8ToBcd(dt.Second()));
        _wire.write(Uint8ToBcd(dt.Minute()));
        _wire.write(Uint8ToBcd(dt.Hour())); // 24 hour mode only
        _wire.write(Uint8ToBcd(dt.Day()));
        _wire.write(Uint8ToBcd(dt.Month()));

        // RTC Hardware Day of Week is 1-7, 1 = Monday
        // convert our Day of Week to Rtc Day of Week
        uint8_t rtcDow = RtcDateTime::ConvertDowToRtc(dt.DayOfWeek());

        _wire.write(Uint8ToBcd(rtcDow));
        _wire.write(Uint8ToBcd(dt.Year() - 2000));
        _wire.write(0); // no write protect, as all of this is ignored if it is protected

        _wire.endTransmission();
    }

I'm battling to work out how I can use this function to set the individual hours and minutes:
Something like:

mymin = 10; // for example
myhour = 2; // for example
Rtc.SetDateTime(myhour, mymin)

I'm sure it's simple, but I'm not quite getting what to do at the top level of the code in my sketch.
Thanks and regards
Russell

That function takes a reference to a RtcDateTime object as its argument. You can find the definition of this class in RtcDateTime.h.

Thanks - but I suppose I don't really know what I'm doing: I tried this:

void setDs1302()
{
  char datestring[20];
  char setmonth = 9;
  char setday = 4;
  int setyear = 2021;
  char sethour = 8;
  char setmin = 23;
  char setsec = 0; 
  snprintf_P(datestring,
             countof(datestring),
             PSTR("%04u/%02u/%02u %02u:%02u:%02u"),
             setyear,
             setmonth,
             setday,
             sethour,
             setmin,
             setsec );

Serial.println(datestring);
Serial.println("setting...");
Rtc.SetDateTime(datestring);
}

In order to hard code a time/date for testing: but I just get this output:

2021/09/04 08:23:00
setting...
01/01/2000 00:37:34
01/01/2000 00:37:36
01/01/2000 00:37:38
01/01/2000 00:37:40
01/01/2000 00:37:42
01/01/2000 00:37:44
01/01/2000 00:37:46

edit:
I think I missed the ':' required the object format:
this post helped: https://forum.arduino.cc/t/solved-ds3231-set-date-time-manually/454709

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.