On board clock

Is there any way to use the Arduino mega 2560 REV3 to keep track of time and if so where is a library I could try to use. I tried to find some help online but most of it was about external clocks. I read a little about the crystal oscillator on board but nothing ever came up to tell me if it could be used for time keeping. I understand that clocks have a certain accuracy, as long as it loses time over months rather than weeks I should be ok.

You don't actually need a library. The millis() function counts the number of milliseconds that have passed since the last reset of the board so you could do all the calculations yourself but it could become painful.

Having said that, look at the Time library. At its simplest it allows you to set a start time then updates a display of date and time but as you have read elsewhere, don't expect it to be very accurate. The TimeSerial example from the library should get you started

GavinSW:
I understand that clocks have a certain accuracy, as long as it loses time over months rather than weeks I should be ok.

Using the MCU oscillator to keep time will not give you hours of accuracy yet alone weeks or months and if the MCU is reset then the correct time is lost.
It is really worth using something like this to keep time as it will be more accurate and preserves the date/time if the Arduino is reset. I use the DS3231 in my bedside alarm clocks and they have lost/gained less than 2 minutes in two years.

Hi,
How accurate do you need the time keeping?

Tom... :slight_smile:

There's timelib which is very useful for converting different time formats, and doing calculations with time.

Of course if you want to keep track of the wall clock time you need some kind of external clock source, e.g. an RTC connected to your board, or by polling an NTP server.