Using RTC instead of millis to execute something every second

Hi,

Because millis is not accurate, I’d like to use RTC instead.

I have a project with an Lcd counter. it increase by One, every
second more or less because millis is not accurate

To execute my counter every second I use the common following line with millis

if(currentMillis - previousMillis > interval) {
do something
}

previousMillis = currentMillis;

I just installed the RTC DS3231 with the library RTClib.h and now, I have the clock
working on my project.

it print hour minute and second with the following

DateTime now = rtc.now();

lcd.print (now.hour(), DEC)

etc…

Question

now.second() is the variable for second is in it ?

How can use it to replace millis ?

I’m a beginner so I’m still fighting with initialize and variable to store values etc…

hddforensic: Hi,

Because millis is not accurate, I'd like to use RTC instead.

I have a project with an Lcd counter. it increase by One, every second more or less because millis is not accurate

To execute my counter every second I use the common following line with millis

if(currentMillis - previousMillis > interval) { do something }

previousMillis = currentMillis;

Question

now.second() is the variable for second is in it ?

How can use it to replace millis ?

You are most of the way there already.

Instead of using previousMillis, create a variable named previousSecond and use it. Then you will be able to compare now.second() to previousSecond.

(Hint: the symbol != means "does not equal".)

Hi,

I tried and i'm trying right now but I keep getting the message that "now" was not declared in this scope.

I initialized

unsigned long StarSec = 0; int EndSec = now.second();

So I can use EndSec - StartSec but I get "now" was not declared in this scope

You see, for me it's not clear for me yet on how to initialize and how to set variable.

In your original post on this thread you knew how to create a DateTime object called now. You’ve forgotten since then?

I think the DS3231 has the capability to output a 1 hz square wave on one of it’s pins. I would think about using that.

Jimmy

I have a project with an Lcd counter. it increase by One, every
second more or less because millis is not accurate

As I understand it from your previous threads there is something blocking the timely execution of the one second millis() based timer. I don’t see how constantly querying the i2c bus over and over to determine if a second as passed yet is going to help solve your problem. In fact, that overhead is likely to add to it.

I think the DS3231 has the capability to output a 1 hz square wave on one of it’s pins. I would think about using that.

I agree with @mixographer

But I don’t see that millis() “inaccuracy” is the root cause of the failure of the software timer’s ability to run exactly every second. Using another, more accurate measure of time, will not solve the problem of blocking code and sensor measurement time.

I initialized

unsigned long StarSec = 0; int EndSec = now.second();

WHERE did you put that code? You can't call now.second() outside of a function.