Using Millis for keeping time

Hello, everyone, I am new to Arduino and this is my first project! In the project, I am using Millis() to keep track of time but it isn’t so accurate. The timer is 14 seconds too slow every hour. I know that the internal clock of the Arduino isn’t exact but I think 14 seconds is too big of an error. I am wondering why this is the case. Is it because my code is long and that’s why it doesn’t keep track of time properly? I don’t have any for or while loops that would block the code. Any help would be appreciated. Thank you!

I attached the code at the bottom, any feedback on that would be very helpful as well.

AlarmClock_working.ino (8.67 KB)

long unsigned int time; long unsigned int btnTimer = 0;

long unsigned int ??? use unsigned long

.

It is not your code (well at least I trust it is'nt). The millis() function relies on the onboard ceramic resonator that drives the CPU clock on the Arduino. It is not a precision frequency source. It is often significantly slow or fast compared to a quartz crystal resonator and is extremely temperature sensitive.

Instead of this

time = millis();

use

time += 999;

to avoid accumulating errors.

There is a long discussion about this in the demo Several Things at a Time

If that does not give you sufficiently accurate time you will need to get a Real Time Clock (RTC) module.

...R

Robin2:
Instead of this

time = millis();

use

time += 999;

to avoid accumulating errors.

There is a long discussion about this in the demo Several Things at a Time

If that does not give you sufficiently accurate time you will need to get a Real Time Clock (RTC) module.

…R

The OP used this
    if (millis() - time > 999)
to replace the more straightforward
    if (millis() - time >= 1000)
thus use

time += 1000;

not

time += 999;

aarg: The OP used this     if (millis() - time > 999)

I assumed he did that for a reason and I just followed his lead.

In either case it would be better to put that number in a variable so that the code was like

if (millis() - time >= interval) {
    time += interval;
    // other stuff
}

...R

Yes, that is de rigeur for millis() use.

Thank you, everyone, for their replies, I took all of them into consideration. Anyways, using

time += 1000

rather than

time = millis();

made my clock much more accurate.

Thanks again!