Go Down

Topic: Time Library added to Playground (Read 39 times) previous topic - next topic

Korman

#120
Nov 01, 2010, 11:43 am Last Edit: Nov 01, 2010, 11:44 am by Korman Reason: 1
There may be a weakness in setTime(). Try to call now() right before you call setTime(). If that solves the problem, let us know it, then we can have it fixed.

Korman

PaulS

Quote
Try to call now() right before you call setTime().

If the value returned by now() is supposed to be set by the call to setTime(), how in the heck is calling now() first going to accomplish anything?

Korman

#122
Nov 01, 2010, 11:58 am Last Edit: Nov 01, 2010, 11:59 am by Korman Reason: 1
No, I mean something like:
Code: [Select]
[glow]now();[/glow]
setTime(pctime);
timeStorage = now();


And no, that's not voodoo, it might prevent a potential problem from happening. If everything works fine with the additional now(), we have discovered a bug which will be easy to solve. If it doesn't the thing I imagined to be a problem isn't one and we're barking up the wrong tree.

Korman

Thomas de Graaff

#123
Nov 01, 2010, 12:06 pm Last Edit: Nov 01, 2010, 12:11 pm by Thomas_de_Graaff Reason: 1
That indeed solves the problem. So if I understand it right, to get a precise sync, for the moment, you need to call now() just before setTime() ?

Quote
\0x07Waiting for sync message

\0x07
now() time before syncing = 3
pctime, time that was synced with
= 1288477507
timeStatus() after syncing = 2
now() time after syncing = 1288477507


now() time before syncing = 1288477549
pctime, time that was synce
d with = 1288477507
timeStatus() after syncing = 2
now() time after syncing = 1288477507


now() time before syncing = 1288477534
pctime, time that was sy
nced with = 1288477507
timeStatus() after syncing = 2
now() time after syncing = 1288477507


Code: [Select]
/*
* TimeSerial.pde
* example code illustrating Time library set through serial port messages.
*
* Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
* you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2010
T1262347200  
*
* A Processing example sketch to automatically send the messages is inclided in the download
*/

#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
unsigned long timeStorage = 0;

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(){
 /* // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print(" ");
 Serial.print(day());
 Serial.print(" ");
 Serial.print(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 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    
       }
     }  
     timeStorage = now();
     Serial.println();
     Serial.print("now() time before syncing = ");
     Serial.println(timeStorage);
     setTime(pctime);   // Sync Arduino clock to the time received on the serial port
     timeStorage = now();
     Serial.print("pctime, time that was synced with = ");
     Serial.println(pctime);
     Serial.print("timeStatus() after syncing = ");
     Serial.println(timeStatus());
     Serial.print("now() time after syncing = ");
     Serial.println(timeStorage);
   }  
 }
}

time_t requestSync()
{
 Serial.print(TIME_REQUEST,BYTE);  
 return 0; // the time will be sent later in response to serial mesg
}


Korman

#124
Nov 01, 2010, 12:35 pm Last Edit: Nov 01, 2010, 12:36 pm by Korman Reason: 1
Quote
That indeed solves the problem.


Ok, the bug in the setTime function is that it doesn't reset prevMillis to the value of millis(). So the next time you call now(), the difference between the last call to now() and the current value of millis() will be added to the time you just adjusted with setTime(). If now is called often enough, that isn't a visible problem. However if you call now() only every 5 seconds and use setTime() directly, you get that weird offset you saw.

Next step: Finding out who maintains the Time library and where to post a patch...

Korman

Go Up