Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 159831 times) previous topic - next topic



not sure about github, but the time library download can be found here:

(not sure why is it in this unusal location, and not with the other libraries ?!?)


First of all, thanks for writing this very usefull library!  And of course, a request :-) I would like to use a syncTime function similar to setTime but registering the update as a sync. I do not want to use the setSyncProvider, I cannot instantaneously give back a sync time upon request. I see an commented out function header setExternalTime

The background is that I'm rewriting the DCF77 clock routine created by Mathias Dalheimer.  It translates a bit chain coming from a radio beacon every minute (in optimal conditions)  and ideally I would update the time immediately afterwards , rather than having to wait for Time callback. Could this be added to the lib?


Feb 02, 2012, 07:21 pm Last Edit: Feb 02, 2012, 07:25 pm by schrockstudio Reason: 1
Hi I'm working on a relatively simple project where I want to time some LEDs to go on and off at slow intervals.
-throughout a one day period,
-then once a week have a different set of "alarms" to trigger the LEDs,
-then return to the usual pattern,
-then once a month have a different set of "alarms" to trigger the LEDs,
-then return to the usual pattern.

I've already tested and assembled the test hardware for the events, now all I need to do if figure out the code.

Someone mentioned
One thing to watch out for is that the Arduino clock isn't very precise so it will gain or lose time over long intervals (minutes per day, possibly a day or two over six months).

Is that problem solved with the "TimeAlarms Library?

Also when attempting to use time alarms I'm getting some error messages when just attempting to verify the example sketch.
/Applications/Arduino.app/Contents/Resources/Java/libraries/TimeAlarms/TimeAlarms.cpp:25:22: error: WProgram.h: No such file or directory
/Applications/Arduino.app/Contents/Resources/Java/libraries/TimeAlarms/TimeAlarms.cpp: In member function 'void TimeAlarmsClass::delay(long unsigned int)':
/Applications/Arduino.app/Contents/Resources/Java/libraries/TimeAlarms/TimeAlarms.cpp:256: error: 'millis' was not declared in this scope

Can someone shed a bit of light on my issues, queries? thanks!
-What are the error messages?
-Is TimeAlarms Library even what I should be using?
-Will I loose signifigant time accuracy over 6 months with TimeAlarms Library?


Is that problem solved with the "TimeAlarms Library?

If software could make your hardware operate better, don't you suppose that that software would be part of the core software?

Also when attempting to use time alarms I'm getting some error messages when just attempting to verify the example sketch.

And you've searched and found that the library has not been updated for Arduino 1.0, right? WPrograms.h was renamed to Arduino.h.

-Will I loose signifigant time accuracy over 6 months with TimeAlarms Library?

Whether your system gains or looses time is not a library-related issue.
The art of getting good answers lies in asking good questions.


Feb 02, 2012, 09:09 pm Last Edit: Feb 02, 2012, 09:23 pm by ninja2 Reason: 1
The time inaccuracy is due to the Arduino hardware, the library itself will not fix that.

You can improve accuracy by adding a Real Time Clock (RTC) breakout such as the DS1307. I'm finding the inaccuracy of a DS1307 Real Time Clock is typically to loose around 5 to 8 second every day. It can be more or less than this but it gives you an idea. At 5 secs/day that would be about 15 minutes lost in six months. If this áccuracy is not good enough for your project, there are two possible solutions:

1) buy a more accurate RTC, such as the DS3231. See this thread http://arduino.cc/forum/index.php/topic,87846.0.html

2) run the clock for a few days and work out how much it gains/looses per day, then write software to compensate.

Your compiling problem is because you are using an old version of the time library . In the release notes for Arduino 1.0  it explains "wprogram.h" is now replaced by "arduino.h". You need to change the code to fix this. But it's your lucky day, to save you time I've attached a revised version of the Time library which has this update in Time.cpp:

#if ARDUINO >= 100
#include <Arduino.h>
#include <WProgram.h>


Feb 02, 2012, 10:20 pm Last Edit: Feb 02, 2012, 10:28 pm by schrockstudio Reason: 1
Thanks ninja!

upon reading more it seems that you cannot use millisecond delays with the timealarm lib.
At 9pm every sunday I want to set the array to do a simple choreographed sweep through the LEDs back and forth 60 times and that requires the LEDs to High/Low with 100 milliseconds delays.
-Is that not possible using TimeAlarm Lib?
-If I simply calculate Milliseconds 604,800,000(once a week) and use the Arduino's clock, what is the typical drift? I can live with a little change. if that change is consistant. ie, it looses/gains 26.44 seconds a day. I can plan my events accordingly to that.

The 6months/deadline starts in 4 days. so I cannot order any of those shields.

Also a concern I have is, what if there is a power surge? does the Arduino reset the program completely or start where it left off?

Thanks! Thanks thanks!!!

*edit. PaulS I just read your post, thanks for letting me know about the Lib update!
-So as it sounds the hardware timing inconsistency isn't solved by software/code. so that means it is an A)inconsistant time change?
B)just inconsistant between different Arduinos, so I could calculate my specific hardwares drift and adjust?

Thank you for helping!


Just a passing suggestion.

It seems like what you're describing is a set of modes. One mode for a period of time, and then another, and so on.

One thing you could do is use the Arduino's EEPROM (non-volatile RAM) to write the current mode, so that on power-cycle it can restart in the last mode, where it left off. You could likewise do something like tick off the number of hours in that mode, so your recovery to the last mode could also be a little more granular than just a restart from the beginning.

EEPROM tips here: http://arduino.cc/it/Tutorial/EEPROMWrite


I need to include a clock in my project, that syncs with NTP. But im using an enc28j60 based ethernet controller(Using Ethercard library). so how can i modify the TimeNTP example to use it with my Ethernet controller.
(I have already established the internet connection)


I've downloaded and installed the subject library. The sketch (RealTimeClockDS1307_Test)
complies and loads without error but when I run it and view the Serial Monitor I just get
a sequence hex chars that do not appear to be incrementing... the output on
the Serial Monitor looks like this:

Code: [Select]

1: FF-1F-3F 1F:7F:7FP
2: FF-1F-3F 1F:7F:7FP
3: FF-1F-3F 1F:7F:7FP
4: FF-1F-3F 1F:7F:7FP
5: FF-1F-3F 1F:7F:7FP
6: FF-1F-3F 1F:7F:7FP
7: FF-1F-3F 1F:7F:7FP
8: FF-1F-3F 1F:7F:7FP
9: FF-1F-3F 1F:7F:7FP
10: FF-1F-3F 1F:7F:7FP
11: FF-1F-3F 1F:7F:7FP
12: FF-1F-3F 1F:7F:7FP
13: FF-1F-3F 1F:7F:7FP
14: FF-1F-3F 1F:7F:7FP
15: FF-1F-3F 1F:7F:7FP
16: FF-1F-3F 1F:7F:7FP
17: FF-1F-3F 1F:7F:7FP
18: FF-1F-3F 1F:7F:7FP
19: FF-1F-3F 1F:7F:7FP
20: FF-1F-3F 1F:7F:7FP
21: FF-1F-3F 1F:7F:7FP
22: FF-1F-3F 1F:7F:7FP
23: FF-1F-3F 1F:7F:7FP
24: FF-1F-3F 1F:7F:7FP
25: FF-1F-3F 1F:7F:7FP
Send ? for a list of commands.
26: FF-1F-3F 1F:7F:7FP
27: FF-1F-3F 1F:7F:7FP
28: FF-1F-3F 1F:7F:7FP

Looking at the DS1307 datasheet the default/power on  date/time should be  01/01/00 00:00:00 but I can't
get to this from FF-1F-3F 1F:7F:7FP ??


I don't know about that ''davidhbrown'' library but why not just use the stanndard time library of the examples provided in the arduino IDE ? (file>examples>time) That all works fine.


I am looking at using this library for an internet connected timing device.  I noticed that now() uses millis() and prevMillis to update sysTime.  It would seem to me that millis() will wrap around or overflow after about 49.7 days.  Question: Should we not check if millis() is less than prevMillis and make the appropriate adjustment as this condition would be true if millis() overflowed?  I haven't worked out the code yet but does this make sense to anyone else?


Question: Should we not check if millis() is less than prevMillis and make the appropriate adjustment as this condition would be true if millis() overflowed?  I haven't worked out the code yet but does this make sense to anyone else?

There is no need. If millis() does overflow, and reset to a small value, millis() - prevMillis will still return the correct value (as long as that duration is less than 49.7 days).
The art of getting good answers lies in asking good questions.


Thanks.  This took me a while to figure out but then I realized it was unsigned arithmetic so you will always get a positive number that is the deference between the two regardless of which is bigger.  Nice!


Ok, so trying to use the Example sketch where time is synced by serial port to computer.

Manually sending the time message, or attempting to use the supplied Processing sketch both result in the Arduino being stuck "waiting for sync message".

If I understand the code correctly, when the Arduino receives a sync message it should print it out, right? Well, it only prints out the 'T' and goes back to waiting for sync. This leads me to believe the message is being truncated for some reason, or it is not reading it all.

For example this is what the serial monitor looks like right after sending a sync message note the "T":
Code: [Select]
0:00:09 1 1 1970
0:00:10 1 1 1970
0:00:11 1 1 1970
0:00:12 1 1 1970
Twaiting for sync message
waiting for sync message
waiting for sync message
waiting for sync message

Can anyone else verify that these sketches work as intended with their own setup? If so, maybe point out what might be going wrong.


The code running on the arduino is simply the example sketch found in the Time library and on this page: http://arduino.cc/playground/Code/Time
Code: [Select]
#include <Time.h> 

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message

// T1262347200  //noon Jan 1 2010

void setup()  {

void loop(){   
  if(Serial.available() )
  if(timeStatus()!= timeNotSet)
    Serial.println("waiting for sync message");

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  if(digits < 10)

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of header & 10 ASCII digits
    char c = Serial.read() ;
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();         
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port

The processing sketch I'm running is also the one included with the library.
Code: [Select]
* SyncArduinoClock.
* portIndex must be set to the port connected to the Arduino
* The current time is sent in response to request message from Arduino
* or by clicking the display window
* The time message is 11 ASCII text characters; a header (the letter 'T')
* followed by the ten digit system time (unix time)

import processing.serial.*;

public static final short portIndex = 1;  // select the com port, 0 is the first port
public static final char TIME_HEADER = 'T'; //header byte for arduino serial time message
public static final char TIME_REQUEST = 7;  // ASCII bell character
public static final char LF = 10;     // ASCII linefeed
public static final char CR = 13;     // ASCII linefeed
Serial myPort;     // Create object from Serial class

void setup() { 
  size(200, 200);
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this,Serial.list()[portIndex], 9600);

void draw()
  if ( myPort.available() > 0) {  // If data is available,
    char val = char(myPort.read());         // read it and store it in val
    if(val == TIME_REQUEST){
       long t = getTimeNow();
       sendTimeMessage(TIME_HEADER, t);   
       if(val == LF)
           ; //igonore
       else if(val == CR)           
         print(val); // echo everying but time request

void mousePressed() { 
  sendTimeMessage( TIME_HEADER, getTimeNow());   

void sendTimeMessage(char header, long time) { 
  String timeStr = String.valueOf(time); 
  myPort.write(header);  // send header and time to arduino

long getTimeNow(){
  // java time is in ms, we want secs   
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(new Date());
  int tzo = cal.get(Calendar.ZONE_OFFSET);
  int dst = cal.get(Calendar.DST_OFFSET);
  long now = (cal.getTimeInMillis() / 1000) ;
  now = now + (tzo/1000) + (dst/1000);
  return now;

Go Up