sureshparanjape:
Hi to all respondents to my query. Many thanks for inputs.That I am so new to this field that millis() reverts to zero after 4294967295 milliseconds was information to me. My sketch envisages giving manually IntialHour,InitialMintue to start the loop part of the sketch,since Seconds and milliseconds would not be accurate manually! I could do so by connecting to PC,downloading current time of PC and start clock at that time. However, it is presently beyond my skill!
The pc would need to have a program running that sends the current system time in some sort of packet over the serial port. And your Arduino sketch would need to be able to recognize this has arrived and interpret it. That is indeed quite dificult for a beginner to setup. And requires more than just Arduino knowledge. I don’t know of any other examples that you can replicate this from. Most often you see hardware solutions that use a special clock chip to count the time and date. Or use a radio-receiver to pull a special signal out of the air. But that is location specific. I don’t know if there is such a radio transmitter in India (or whatever your country is, I’m just guessing based on your name) Using a hardware GPS receiver is also a possibility to synchronise your clock with atomic time. But somewhat expensive and requires line-of-sight with the satelites.
The solution below, which is already suggested, is more simple. It should keep running indefinitely until your battery runs out. As time passes by you’ll notice there is a slight difference with other clocks. But that is normal, as it is not an atomic-accurate clock. No clocks tick in the exact same rate. Depending on how many years you can count, it should go on forever.
But can I have something similar to below to continue the lock after rollover crisis?
Loop
if (millis() !=0)
{regular working part of the program;}
else {InitialHour=PresentHour;InitalMinute=PresentMinute;InitialSecond=PresentSecond ;InitialMilli=PresentMilli;
regular working part of the program;}
sureshparanjape
What happens to your clock when millis() rolls over, but never becomes exactly 0 when you call it? (Your Arduino might be doing other code during that specific milisecond) The millis value just before and after the roll over, is not equal to 0. So you have to be really really lucky to find millis() to be exactly 0, and have that PresentHour/Minute/Second initialisation taking place. It might not happen until the next 50-ish day event, or 99-ish, or 149-ish… or … you know.
Instead, use it to notice when a seconds roll over (or smaller values if you want that kind of precision) and count your own clock-second values. This happens much more often, and with longer duration. Whenever it is 1000 (or 100 or whatever) higher than previous, you have a new (deci-)second. Now, when it is count 60 seconds also increase the minute and set seconds to 0. If it is also 60 minutes increase the hour and set minutes to 0. And so on.