Why does opening the serial monitor cause code to execute?

I’ve got some code:-

  if (currentMillis > 30000 && INITIALSET < 1)   
  {
    Serial.print("Time to set the clock initially");
    Serial.println();
    RTCUD = 1;
  }
if (RTCUD == 1)
  {
     //DO LOTS OF STUFF  (removed for brevity/readability)     
      Serial.println("Time update successful");
      RTCUD = 0;      // RTC has been set for the morning / evening
      INITIALSET = 1; // RTC has been initially set
    }

  }

that seems to only run once, when the loop is 30seconds old. Problem is, if I then re-open the serial monitor, it runs again!

Any ideas why? I can post ALL my code if needed to answer.

Thanks

Any ideas why? I can post ALL my code if needed to answer.

Opening the serial monitor causes the opening of the PC com port which generates the DTR signal from the com port. The arduino used the DTR signal to cause a auto-reset for the board so your program is being forced to start over from the beginning of the code in your start-up function.

Lefty

Thank you!