Bug in Time.h ?

Hi everyone,

I am testing the Time.h library for a project of mine, and I get a stange behavior:

I ask to print the date and time on the console every second. This prints fine for a full 5 minutes, then the date and time is printed with garbage, a "7" being printer just before every number I request to the time library...

This is the output when the problem begins:
15/9/2013 10:50:56
15/9/2013 10:50:57
15/9/2013 10:50:58
15/9/2013 10:50:59
715/79/72013 710:751:70
715/79/72013 710:751:71
715/79/72013 710:751:72
715/79/72013 710:751:73

and below is the code. Any idea what it could be ? I get that problem on a Uno and a Mega :frowning:
Thanks in advance.

#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 

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

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)   
  {
    digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
    digitalClockDisplay();  
  }
  delay(1000);
}

void digitalClockDisplay(){
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year()); 
  
  Serial.print(" ");

  // digital clock display of the time
  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.print(second());

  Serial.println(); 


}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

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() ; 
    Serial.print(c);  
    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()
{
  //Serial.print(TIME_REQUEST,BYTE);  
    Serial.print(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

Initialisation Date time value I use = T1379241960

In setup() you register requestSync as the sync provider, and this prints TIME_REQUEST (7) to the serial port. It's reasonable to suppose this is what is producing the spurious '7' characters in your output.

The comments imply that 7 is intended to be the BEL character. I don't know whether the Arduino serial terminal supports BEL (it may just display a garbage character instead) but if you wanted to output the BEL character (rather than a decimal representation of the ascii code 7) then you'd need to use Serial.write() rather than Serial.print(). It's not clear how that would provide you with a time sync, though.

It's not clear how that would provide you with a time sync, though.

It's what the Processing app is expecting, to tell it to send the PC time to the Arduino, via the serial port.

Ok, thanks guys, problem solved.

I suppose the default setSyncInterval() is 300, this is why after 5 minute I was sending this 7...

Thanks.