RTC 模块当前时间戳已截断秒数

问题描述 投票:0回答:1

我有一段简单的C(Arduino)代码,它使用非阻塞间隔计时器来比较两个不同的时间戳值,以确定天气或特定间隔x是否已经过去,如果是这样,则重置计时器并记录/打印日期时间戳。我面临的问题是,第一个时间戳之后的所有时间戳都有秒字段的截断值,例如14:02:00 而不是 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

我使用的RTC是一个常见的DS3231,我使用Adafruit RTC库作为代码。

我想我明白为什么会发生这种情况,我相信这可能是因为我只是检查我的 if 语句天气或不仅仅是一分钟的时间单位已经过去,但也许我还需要以某种方式考虑秒数.

如何修改我的代码以便保留准确的秒值?

datetime arduino timestamp adafruit real-time-clock
1个回答
0
投票

谢谢大家的建议。我最终通过使用原始 .unixtime() 方法而不是仅检查分钟来解决这个问题,这返回了一个显示秒数的时间戳。

© www.soinside.com 2019 - 2024. All rights reserved.