Time Library added to Playground

This forum only allows links to images and code, you need to upload the item in the link to some other public web site. If you do that I will put a link to your VB app in the playground.

A suggestion for your VB app, if you read a character at time from the serial port you can check for the character 7 which is what Arduino sends when it wants the time. This will set the time without you having to press a button if you send the Time string whenever you received the character 7

Also, you can strip off the Carriage Return / Line Feed at the end of the clock display so you don't get that funny character at the end of the line in the list box.

Your earlier post inspired be to create a new example sketch that allows setting of explicit times from the serial port.

The time can also be set as follows:
S[hh:mm:ss dd mm yy]

Values must be given in order starting from hh
For example: S[14] sets the hour to 2pm, S[14:10] sets 2:10 pm
S[14:10:00 09 01 10] sets the time to 14:10:00 9th Jan 2010

Values must be two digits with preceeding 0 if necessary (eg 05 to set the value 5)
Year must be two digits, 2010 is entered as 10

Opening and closing square brackest are required, colon and spaces are optional.

Here is the new sketch:

/* 
 * NewTimeSerial.pde
 * example code illustrating Time library date strings
 *
 * Time can be set using an 11 character message consisiting of the letter 'T' 
 * followed by the ten digit number of seconds since Jan 1 1970 (Unix time)
 *
 * The time can also be set as follows:
 * S[hh:mm:ss dd mm yy]
 * Values must be given in order starting from hh
 * S[14] sets the hour to 2pm, S[14:10] sets 2:10 pm 
 * values must be two digits with preceeding 0 if necessary (eg 05 to set the value 5)
 * year must be two digits, 2010 is entered as 10
 * opening and closing square brackest are required, colon and spaces are optional
 *  
 * 
 */

#include <Time.h>  
#include <ctype.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_SET_TAG 'S'    // Indicates start of time set string
#define TIME_SET_StrLen 15  // the maximum length of a time set string [hhmmssddmmyyyy]
#define TIME_REQUEST  7     // ASCII bell character requests a time sync message 

TimeElements te;            // holds the time elements for setting the time 

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() ) 
  {
    char tag = Serial.read();
    if(tag == TIME_HEADER)
      processSyncMessage();
    else if(tag == TIME_SET_TAG)
      processSetTime();   
  }
  if(timeStatus()!= timeNotSet) 
  {
    digitalClockDisplay();  
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(dayStr(weekday()));
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(monthShortStr(month()));
  Serial.print(" ");
  Serial.print(year()); 
  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 processSetTime(){
  //[hh:mm:ss dd mm yyyy]
  char setString[TIME_SET_StrLen];
  int index = 0;
  char c = Serial.read();
  if( c != '[')
     return;  // first character must be opening square brackets
  do 
  {
     c = Serial.read();
     if( isdigit(c))  // non numeric characters are discarded
       setString[index++] = c -'0'; // convert from ascii    
  }
  while (c != ']'); // wait for trailing square brackets

  breakTime(now(), te);   // put the time now into the TimeElements structure  
  
  int count = index;
  int element = 0;
  for( index = 0; index < count; index += 2)  // step through each pair of digits
  {
      int val = setString[index] * 10 + setString[index+1] ; // get the numeric value of the next pair of numbers
      switch( element++){
        case  0 :  te.Hour = val; break; 
        case  1 :  te.Minute = val; break; 
        case  2 :  te.Second = val; break; 
        case  3 :  te.Day = val; break; 
        case  4 :  te.Month= val; break; 
        case  5 :  te.Year = val + 30; break; // year 0 is 1970        
      }    
  }
  setTime( makeTime(te));
}

void processSyncMessage() {
  // wait for ten digts, convert to time and update systemTime
  // todo - timeout after say 5 seconds    
  time_t pctime = 0;
  while(Serial.available() < TIME_MSG_LEN-1 )  // time message consists of a header and ten ascii digits      
    ; 
  for(int i=0; i < TIME_MSG_LEN -1; i++)
  {   
    char 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);  
  return 0; // the time will be sent later in response to serial mesg
}