I have a sketch that is essentially a clock. At first, I had it set up so that the sketch initially set the time using setTime(XXXXXXXXXX) to 12AM on a certain day (the day doesn't matter since the clock does not display the date, I just chose a day at 12AM and put that epoch time value in in place of the Xs.)
After that, there are two buttons the user can use to set the time. I programmed it so that one does "setTime(now()+3600)", the other does "setTime(now()+60)". Basically, one advances the time by an hour, one by a minute, just like a normal clock.
I realized that the Arduino does not keep accurate time by itself, so I added a GPS module, and set the sync provider as GPS.
This generally works well, but since a GPS lock is not always available when you first give power to the device, I still allowed the buttons on the back to advance the time. My idea was that if someone manually set the time before the GPS lock was obtained, then I would set a variable indicating that had happened, and then when it did finally get a GPS lock, it would take into account how far off the current time was from UTC, and take that into account every time it re-synced to the GPS. (So, essentially, I assume that if you set the time yourself, that time is correct, and the GPS sync would just keep that time accurate, because I would offset the GPS set time by however far off it was from UTC when it first got a GPS lock.)
Problem is, if you manually set the time using the buttons, whenever the GPS lock is obtained, it seems like the whole board resets, so I can't do any of the calculations I just talked about, because everything starts over from the beginning.
The puzzling thing is that the board only resets like this if you manually set the time before getting a GPS lock. If you just leave it alone, it gets a GPS lock after a minute or two, factors in the default timezone offset I programmed it with (eastern time), and all is good.
Even if you then advance the time manually after it gets that first GPS lock, it still doesn't reset the next time it syncs with GPS. (I made it so that advancing the time after getting GPS lock both advances the time and adjusts the timezone offset forward by however much you advanced the time, so the next time it syncs, it doesn't go back to the old time.
I'm aware that opening new serial connections can reset the board, but I'm not handling the serial connection any different whether you set the time before GPS lock happens or not. The GPS is set to transmit and receive on pins A4 and A5 using the SoftwareSerial library, and the serial connection begins right at the start of the sketch. (The buttons to advance time are connected to A0 and A1 if it matters.)
Any idea why adjusting the time manually before a GPS lock is obtained seems to make the Arduino reset when it gets a lock? This is an Uno board BTW. Thanks.