CURRENT TIME

Hi.

I'm looking for a function to return the current time in seconds / microseconds since the begin of the year or something like that.
Someone can help me?

Thanks in advance

How long has your Arduino been running and where is it getting its time information from ?

The arduino can tell you how long it has been since the board was powered up or reset, but it has no way of telling the time of day or year without a RTC chip.

Is it posible to get the PC time for example? Just once. To increment every second exists a library (Date Time library) with a function now() doing it.

Yes, you can create a script on the PC to send the Arduino the time. But you cannot reset or power off the Arduino after it has been set. An RTC is a much better solution.

If you need to convert the time from years, months, days, hours, etc. to straight seconds, there are programs for that. Or you could write your own: it's a good exercise in programming logic.

Weird how these days, we take knowing the time for granted. Our phones synchronise over the 'net and we don't think twice about it. Look into the history of watches and timekeeping - what a big deal it was to be able to keep track of seconds accurately.

How many microseconds are there in a year? 10^6 * 60 * 60 * 24 * 365.25. About 10^13, or 2^43. You'll need a long long int (64 bit) to track it, unless you want to go bit-fiddling with a 6-byte quantity.

hcaldas:
Is it posible to get the PC time for example? Just once. To increment every second exists a library (Date Time library) with a function now() doing it.

You can get the current PC date/time when the sketch was compiled. This information is made available in some compiler constants you could read from your sketch.

So if you compile and upload your sketch, you could have the current PC time, just a few seconds late.

Unfortunately: When resetting the Arduino, which also happens when opening the Serial monitor, the time will reset to that time after each reset.

For having "real time" on your Arduino, you'd either need:

  • a RTC module or
  • an Ethernet-Shield with Internet connection

hcaldas,
Go find the Time library. It tracks time the proper way, using an epoch.
It will track the time for you in seconds and has many utility functions for setting the time and conversion
from epoch time to human time including deltas.

--- bill