Fabian Butkovich
Fabian Butkovich

Reputation: 21

RTC module current timestamp has truncated seconds

I have a simple bit of C (Arduino) code which uses a non-blocking interval timer to compare two different timestamp values to determine weather or not a specific interval x has passed, if so, then reset the timer and log/print the date timestamp. The problem I am facing is that all of my timestamps after the first one have a truncated value for the seconds field e.g. 14:02:00 instead of 14:02:31.

  if (abs(time_current.minute() - time_previous.minute()) >= 1)
  {
    // Restart the non-blocking RTC timer
    time_previous = time_current;

    // Convert temperature in type FLOAT to character array buffer which can be concatenated with other characters
    dtostrf(ReadTemperatureF(), 3, 1, tempF);

    // Convert temperature in type FLOAT to character array buffer which can be concatenated with other characters
    dtostrf(ReadHumidity(), 3, 1, humd);

    logfile.print(time_current.timestamp(DateTime::TIMESTAMP_DATE));
    logfile.print(',');
    logfile.print(time_current.timestamp(DateTime::TIMESTAMP_TIME));
    logfile.print(',');
    logfile.print(tempF);
    logfile.print(',');
    logfile.println(humd);
    logfile.flush();

#if DEBUG == 1
    Serial.print(time_current.timestamp(DateTime::TIMESTAMP_DATE));
    Serial.print(',');
    Serial.print(time_current.timestamp(DateTime::TIMESTAMP_TIME));
    Serial.print(',');
    Serial.print(tempF);
    Serial.print(',');
    Serial.println(humd);
#endif

    CreateNewLogFile(time_current);
  }

enter image description here

The RTC which I am using is a common DS3231 which I am using the Adafruit RTC library for the code.

I think I understand why this is happening, I believe it may be because I am only checking if my if statement weather or not only a minute unit of time has passed, but perhaps I also need to factor in seconds as well somehow.

How can I modify my code so that I can retain an accurate value of the seconds?

Upvotes: 0

Views: 49

Answers (1)

Fabian Butkovich
Fabian Butkovich

Reputation: 21

Thank you all for your suggestions. I ended up solving this by resorting to using the raw .unixtime() method instead of only checking the minutes, this returned a timestamp which had the seconds shown.

Upvotes: 0

Related Questions