DS3231 RTC calls issue

Hi

I'm working on a clock project and my main loop is looping more often than planned.

Here are some setup lines -

#include "RTClib.h"

RTC_DS3231 rtc;

Here's the start of the loop code -

void loop()
{

  int current_seconds = 255;

  do
      {
        DateTime now = rtc.now();
        seconds = now.second();
        minutes = now.minute();
        hours = now.hour();
        day = now.day();
        month = now.month();
        year = now.year()-2000;
      }
  while (seconds == current_seconds);

  current_seconds = seconds;

  Serial.println(seconds);
  Serial.println(current_seconds);

The same seconds and current_seconds values are printed several times.

The do ... while loop is not waiting for the next second before dropping down and updating the displays.

All of the time and date values are actually correct.

I've been looking at this for ages and nothing stands out.

I think that it's probably a really simple one.

Please can anyone else see the issue ?

Thanks

I suggest to use the unix time.
Please don't do a do-while in the loop(), let the loop() run as often as possible.

unsigned long previousUnixTime;

void setup()
{
 ...
 previousUnixTime = rtc.unixtime();
}

void loop()
{
 unixTime = rtc.unixtime();

 if( unixTime != previousUnixTime)
 {
   previousUnixTime = unixTime;

   // A new second just happened, display the new time.
  DateTime now = rtc.now();
  seconds = now.second();
  minutes = now.minute();
  hours = now.hour();
  ...
 }
}

Thanks for the reply.

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