How do I make sure the Arduino only run this line of code once even after reset?

220 views Asked by At

I have a real-time Clock module and I'm trying to make an RGB Clock out of it. There is a particular line of code that sets the time and date to the real-time clock module. I put this in the setup code but every time I reset the Arduino it runs that line of code again. Is there any way that I can make sure that once this line in of code is run it will never run again even if I reset the Arduino?

2

There are 2 answers

1
Viktor Latypov On

It is a broad question which goes beyond programming. The answer to your concrete problem is "Remove that line of code" from setup, it will not run (I am sure you know this). However, you should provide some physical input (buttons + rudimentary menu-like interface) to set the clock on first usage. A second way of setting the time can be a serial port to which you can physically connect. This requires some code to read that serial port in your loop method.

In the release version of you Clock firmware I would leave just reading the time stored in EEPROM (from RTC module or an external chip), not the hard-coded value in setup.

0
datafiddler On

Your RTC might have a bit saying "RTC stopped", which you might use to switch to "normal behavior". It will be set when removing power and the RTC-battery. The RTC will probably read "1-Jan-1970 00:00" then, and won't increment unless set.

However, your demo code to set the RTC is just demo code, not intended to really use as is. (See Victor's response for more details)