How to check arduino long time periods?


I use Arduino MKR 1010 WiFi. I would like to change the state of one pin from high to low for a long periods of time par example for 2 days. What is the correct procedure to do that. I created one small program changing the pin state for a period of 1 hour. Is it possible to use this method for longer periods of time as 2-5 days par example?

Here is my code:

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 3600000;
int f = 0;
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  startMillis = millis();

// the loop function runs over and over again forever
void loop() {
  currentMillis = millis();
  if (currentMillis - startMillis >= period && f == 0)
    digitalWrite(LED_BUILTIN, HIGH);
    startMillis = millis();
    f = 1;
  if (currentMillis - startMillis >= period && f == 1)
    digitalWrite(LED_BUILTIN, LOW);
    startMillis = millis();
    f = 0;

Sure. The millis() function returns an unsigned long number, which will cover over 40 days. Not only that, but if you check for roll over, you can keep it going until the end of your device's useful lifetime, like half a billion years.

I don't know about the MKR1010 but the oscillator on Unos and Megas is not very accurate so millis() would not keep time with a clock over long periods - hours or days.

If you want to keep to clock time you probably need a Real Time Clock (RTC) module.