Go Down

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


Yes how stupid thanks may be back later if i can't get my head around the alarms, thanks again keith


Keith, don't beat yourself up. I can see why it seemed logical to include the TimeAlarm library within the Time folder. Unfortunately, Arduino does not currently handle library installation or inter-library dependencies very well, which  is why I chose to have TimeAlarms as a separate library. Perhaps when Arduino support a simple mechanism for installing libraries, problems such as yours will go away.



How is "adjustTime(adjustment);" command used ?

I want to  send a command to my program ,so an hour or a day is added to timer.
Later those commands will be sent by the press of a button

Is this command ok ? adjustTime(hour()++);
Code: [Select]
#include <Time.h> 

void setup()  {

  Serial.println("sending time to arduino");

void loop(){

void digitalClockDisplay(){

  Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");

void printDigits(int digits){
  if(digits < 10)


Feb 14, 2013, 07:51 am Last Edit: Feb 14, 2013, 08:21 am by LarryD Reason: 1
I want to  send a command to my program ,so an hour or a day is added to timer.

When you detect the command you can use the following:

time_t t  = now();
setTime(t + X );                     //X would be 3600 for 1 hour or 86400 for 1 day


adjustTime(X) ;                     //X is + or - moves the time ahead or back X seconds ex: +3600 or -3600
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.



Hello Michael,
I placed this code in a class: http://playground.arduino.cc//Main/DS1302
And I could add the 'get', 'set', 'read' and 'write' functions.

But I don't understand the difference between the 'sysTime' and the time in the RTC.
Are they synchronized with the 'syncInterval' ?
If the Arduino is running a little slow, it is no problem.
If the Arduino is running a little faster, it could cause a glitch for the number of seconds.

How is this handled ?
I do not know how this is done in a PC or other embedded system. I was just wondering what would happen.


I'm attempting to use the Time.h library in a program, but the program won't compile due to an issue in the .cpp file:

/Users/.../libraries/Time/Time.cpp: In function 'time_t now()':
/Users/.../libraries/Time/Time.cpp:240: error: 'millis' was not declared in this scope
/Users/.../libraries/Time/Time.cpp: In function 'void setTime(time_t)':
/Users/.../libraries/Time/Time.cpp:268: error: 'millis' was not declared in this scope

I'm not an expert, so I'd rather not go digging in the library files.  I just updated to 1.0.3 from what I assume was the beta version.


I just updated to 1.0.3

Have you updated the Time library, too? Change WProgram.h to Arduino.h in the header and/or source file(s).
The art of getting good answers lies in asking good questions.



Hello everyone,  I'm trying to use the Arduino time library at http://playground.arduino.cc/Code/Time in my code, but the download link brings me to a page that says "The page "Time/Zip" doesn't exist."

Anyone know where I can download this library?



Anyone know where I can download this library?

Looks like the problem is fixed as of this morning.  Thanks.


I just downloaded new update of Time library. When I try to compile it gives me an error:
sketchbook/libraries/Time/DateStrings.cpp:41:56: error: variable 'monthNames_P' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
sketchbook/libraries/Time/DateStrings.cpp:58:54: error: variable 'dayNames_P' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
sketchbook/libraries/Time/DateStrings.cpp:59:51: error: variable 'dayShortNames_P' must be const in order to be put into read-only section by means of '__attribute__((progmem))'

It is due to new gcc-avr version 4.7.1

Three lines of code in DateStrings.cpp in library should be replaced by:
line 41: PGM_P PROGMEM monthNames_P[] =
line 58: PGM_P PROGMEM dayNames_P[] = { dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7};
line 59: const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThrFriSat";

It, at least, compiles fine. There is no need to downgrade gcc-avr as suggested in several posts.

If someone come across the same problem, I hope it will helps.


I'm sorry if this has been covered but I couldn't find it.
I modified the 'TimeSerialDateStrings' to include a 16x2 I²C LCD Display.
It seems to display okay for a minute, but then seems to get erroneous data causing it to display incorrect information.

It would start off displaying:
10:38:16 Mon
Apr 08, 2013

but after a minute it would change to something seemingly random like:
21:11:26 Fri
Aug 29, 2104

and it doesn't do this once. I haven't been able to get a time interval of when these errors occur, but if I could get some help fixing them, it would be appreciated. I have also seen it display years that contain 5 digits. That may have been an alignment issue tho.

Code: [Select]
* TimeSerialDateStrings.pde
* example code illustrating Time library date strings
* This sketch adds date string functionality to TimeSerial.pde

#include <Time.h> 
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(0); //I²C

#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

void setup()  {
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");

void loop(){   
  if(Serial.available() )
  if(timeStatus()!= timeNotSet)
  //delay(1000); //Disabled because I am not sending time to the Serial Monitor.
                 //I'm sending it to an LCD and the 1 second delay causes it to apear like it skips 1 second every 11 seconds even tho it really doesn't.

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

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  //lcd.print(":"); //seemed to be getting in the way, nut I may be wrong
  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 a header and ten 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

time_t requestSync()
  //return 0; // the time will be sent later in response to serial mesg


Code: [Select]
time_t requestSync()
  //return 0; // the time will be sent later in response to serial mesg

A non-void function MUST return a value. Since this is your sync function, the fact that it doesn't return anything is what is screwing up your clock.
The art of getting good answers lies in asking good questions.


Last night, I got this message. I re-enabled the two lines in question then resent the time code.
I then applied external power and removed the Serial Line and shut down my computer to leave it running without the computer.
It ran all night. I just got home from work and turned on my computer. The Arduino Clock is still in perfect sync with my PC.
It is displaying all the correct information.

Thank you for your help!


Hi all...I'm working at my first project using Arduino and it's IDE and I want to use a macro which is inside the Time.h..For example,after a reset or a power supply fault to the board, I want to calculate the second of the day when the board recovers, therefore to find in which cycle of a finite state machine the program is..For this I want to use the elapsedSecsToday, but I'm far from knowing how to use it, efectivelly...Here is my code,until now, and I want to run this macro in the set-up sequence..Can you give me a clue how to do it?
Code: [Select]
// GPS + RTC  FSM clock
#include "Wire.h"
#include <DS1307RTC.h>
#include <Time.h>       
#include <Timezone.h> //Jack Christensen's Timezone 
#include <TinyGPS.h>//GPS and NewSoftSerial libraries are the work of Mikal Hart
#define DS1307_ADDRESS 0x68

//Timezone settings..=============================.

TimeChangeRule myDST = {
  "ROM", Last, Sun, Mar, 3, +180};    //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {
  "ROM", First, Sun, Nov, 3, +120};     //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);
TimeChangeRule *tcr;        //pointer to the time change rule, use to get TZ abbrev
time_t utc, local,t;//var that must be seen by others functions

//Tiny GPS====================================

TinyGPS gps;
boolean valid = false;

volatile boolean interruptComplete = false;
volatile boolean rtcisrunning = false;

//=========GPS checking=============================================
time_t gpsTimeSync()
  unsigned long age = 0 ;   
  unsigned long date, time;
  gps.get_datetime(NULL, NULL, &age);

  //No time update to DS1307 if GPS signal is lost....
  if (age == TinyGPS::GPS_INVALID_AGE || age >1500 ){
    //Serial.println("GPS lost..");
    valid = false;
    valid = true ;

//  GPS to DS1307 local time =====================================   
time_t gpsToArduino(){
  tmElements_t tm;
  int year;
  gps.crack_datetime(&year, &tm.Month, &tm.Day, &tm.Hour, &tm.Minute, &tm.Second, NULL, NULL);
  tm.Year = year-1970 ;
  time_t time = makeTime(tm);//GPS data--> time_t....
  utc = time;// time from GPS....
  local = myTZ.toLocal(utc, &tcr);
  //RTC.set(local);// moved from here to the loop...

// read  a register from DS1307....

void readreg() {
  byte reg;
  Wire.beginTransmission(0x68);              // write the control register
  Wire.write(0x00);                           // register address 07H)

  Wire.requestFrom(0x68, 1);
  Serial.println (reg,HEX);

//Debug on serial monitor..=================================================
void monitor_out(){
  char dateTime[20];
  sprintf(dateTime, "%4d-%02d-%02d %02d:%02d:%02d", year(),
  month(), day(), hour(),minute(), second()) ;
  Serial.print(" - day of week: ");


void getGPS(){
  bool newdata = false;
  //unsigned long start = millis();
  // Every second we print an update
  //while (millis() - start < 1000)
  if (feedgps()) newdata = true;

// ***********************************************
// read GPS serial data and send to tinyGPS
// ***********************************************
static bool feedgps() {
  while (Serial1.available()) {
    if (gps.encode(Serial1.read())) {
      return true;
  return false;

void setup()
  Serial1.begin(19200); // for GPS
  Serial1.println ("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28");//RMC only...
  Serial1.println ("$PMTK220,1000*1F");//fix at 1000 ms
  attachInterrupt(0, interruptHandler, FALLING);
  delay(1000);//debug only..
      while ( ! valid ){
         if (valid){
      RTC.set(local);// DS1307 set to the local time from timezone..
      Serial.println ( "DS set");//debug only
      delay(1000);//debug only
    //Serial.println (elapsedSecsToday());
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
    Serial.println("Unable to sync with the RTC");
    Serial.println("RTC has set the system time");
    delay(500);//debug only


void loop()

  if (interruptComplete){

    if (valid){  // If GPS sentence is valid,write the time, if not, skip..

      RTC.set(local);// DS1307 set to the local time from timezone..
      Serial.println ( "beep");//debug only
    if (! valid){
      Serial.println ("No GPS");// Serial2---->Ethernet...
    RTC.get();// time from DS1307...
    monitor_out();   //print it...
    interruptComplete = false;
  getGPS();// get data from GPS

//interrupt vector=============================
void interruptHandler()
  rtcisrunning = true;
  interruptComplete = true;


Go Up