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 ?
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.
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
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.