24 hours timer

Hello,
I programmed Arduino UNO R3 to trigger a relay once every 24 hours from the moment it is powered on
I calculated milliseconds and used the function delay()
24 hours * 60 minutes each hour * 60 second every minute * 1000 milliseconds every second

so i used
delay(86400000)
86 million and 400 thousands milliseconds

and currently i'm still testing it...
Will this work?
If not Is there a better built-in function/library or even external library to do it?

the relay trigger doesn't have to be very accurate at the very exact time each day
I don't need it to be very accurate, It can shift few minutes every few months or so, thats okay with me

But how about after few months or years of use ?
Will it shift the timing much ?
I read somewhere about using millis() function
and others use RTC (Real Time Clock) module

I don't want to use RTC module
Looking for a programmatical solution...
OR if my previous method using the delay() will work for such a very high number of millisecond ?

Thank you

delay is generally to be avoided although it can work for crude timing it will essentially make your uC unresponsive. Look at millis for timing. This type of question is asked a lot so there are multiple examples if you search. There are also tutorials in the stickies above.

Read this: millis() - Arduino Reference

Remember that your 'clock' will be reset if there is any glitch that resets the arduino. A RTC holds the time and so is more reliable. You could use a counter and increment it for each minute and another for each hour etc and store things in eeprom but you have limited read writes. Might be better with this type of timer anyway as you could adjust it more easily. I would still use millis to achieve it though.

eg (only logic not true code)

startTime = millis()
if millis() - startTime >= 60000
minutes ++
if minutes == 60
hour++
minutes = 0

etc

Also, an RTC can be programmed to generate an interrupt after setting up an alarm. I've used an RTC to generate an alarm interrupt; worked well.

Also, an unsigned integer with a Uno is dfferent then a Mega, or a Due/ESP32.

A ESP32 has a micros() clock producing a 64bit (uint_64) that rolls over once every several hundred years.

For long period timers I use the Icstation Adjustable Timer Module
https://smile.amazon.com/gp/product/B01KZP9XTI/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 to generate an interrupt. I found this timer to be very accurate. After a timing sequence is complete, pulse 'input' low to restart the timer.

Idahowalker:
Also, an unsigned integer with a Uno is dfferent then a Mega, or a Due/ESP32.

Really? What does Mega-, and Due- unsigned ints look like?

If you time by minutes, you can adjust the "millisInMinute" variable up a few numbers if your Arduino runs fast, or down if slow to fine tune, but it will still drift according to temperature and supply voltage. I put 3 in the "minutesInDay" variable for demo, replace that with 1440 for normal operation.

uint32_t
  timer,
  millisInMinute = 60000, // adjust for calibration
  minutesInDay = 3;// replace 3 with 1440
  
int minutes;

void setup()
{
  pinMode(LED_BUILTIN,OUTPUT);
}

void loop()
{
  if(millis() - timer >= millisInMinute)
  {
    timer += millisInMinute;
    if(++minutes >= minutesInDay)
    {
      minutes = 0;
      // do daily task, just an LED blink here
      digitalWrite(LED_BUILTIN,HIGH);
      delay(3000);
      digitalWrite(LED_BUILTIN,LOW);
    }
  }
}

For example, your Arduino runs 4 seconds fast per hour, there are 3600 seconds in an hour so, 3600 + 4 = 3604, 3604 / 3600 * millisInMinute = 60066, put that number in "millisInMinute" and try again.

Railroader:
Really? What does Mega-, and Due- unsigned ints look like?

Oh 8 bit ints, 16 bit ints, 32 bit ints so on and so forth.

Idahowalker:
Oh 8 bit ints, 16 bit ints, 32 bit ints so on and so forth.

Thanks!
Whow! Being an UNO-guy I don't need to know how to declare those different ints....

“On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value.”

Atmega (Uno, Mega . . .) is 2 bytes (16 bit).