How can I synchronize two milis in two separate

Goodnight, I have a little problem, well actually its pretty big. I want to make a little gizmo, and I need to synchronize the milis value in two separated arduinos, I will use Xbee Pro's as the comm between the two arduinos. The only thing that I remenber to do is to send 50-100 packets, measure the time that it takes to send then, calculate the mean delay time(a sort of lag), and send the actual milis value plus the lag value, can this work?

Sending the millis value should be done as-is, do NOT include the discrepency in it (this will fudge up your measurement).
For best accuracy, you would continuesly send the echo (you know how fast the crystal goes, and how much time the other needs to respond (best accuracy when using a type of interrupt?), deduct this from the flight time… half it (goes to, and back) to get the time of flight.
Have the other ping its millis every now and again, you know how long the flight time is… so you can adjust (do this locally).

One thing to watch out for is drift, both in the descrepency of the time of flight, aswell as the drift between the timers. Also, the sent millis could conceivably be sent ‘intermittently’, that it is sent or received at not too precise intervals.

I just wonder what application would need that precise synchronizing?

One drag-racing chronometer, using the sensors in each end I cant just send data from the finish line to the start line, because when using the Xbee pro I cant really just send serial data and expect it to be delivered, and because Xbee API as a lot going on I cannot know for sure if the serial data sent trought then always takes the same time from the finish to the start sensors. "Sensors" are the complete conjunction of photo gates, arduino doing the processing and the Xbee module.

first thought: did you try to reset them both with a single switch?

First thought: did you try to reset them both with a single switch?

Well presumably they are 'wireless' so they can't be connected with one switch. ;)

I did look into this but for my mini car race timer unit, I just made it wired. :p

I'll be intrigued to watch what you come up with. :)

because Xbee API as a lot going on I cannot know for sure if the serial data sent trought then always takes the same time

Which would mean you would continuesly have to get the time-of-flight for packets. Though for this to work, you do need a fairly fairly failure resistant setup.. you can only ignore so many time-of-flight pings, its an entirely different matter if the finish-line or start-line trigger isn't received.

Good luck!

The idea is synchronizing the two milis timers, then one arduino stores the start milis, and the other will store the finish milis, then I just need to send the finish milis value and compute the total time in the start arduino(that will be hooked up to one pc runing software made using Qt), this will be one hell of an hair puller until it is done :-/

First thought: did you try to reset them both with a single switch?

Well presumably they are 'wireless' so they can't be connected with one switch. ;)

I understood that, but (minutes) before the race one could bring both together and do the simultaneous reset.

By the way for an excellent protocoll for timekeeping check the NTP protocol /synchronization. You could copy the handshake to sync two clocks as it takes transporttime into account.

The reset is impossible to be accurately done, because there are things like button bounce that cause different times of button press to each micro, and then each crystal as is own start-up delay/time until it is outputting one clean square wave.

The reset is impossible to be accurately done,

If you are open to bringing them together for Reset, then couldn't you make use of a temporary-wired connection to sync the clocks? (I'm not sure how, I was just thinking it would simplify your synchronization.)

...check the NTP protocol /synchronization

This seems like a good idea - copy the NTP time synchronization logic.

http://en.wikipedia.org/wiki/Network_Time_Protocol

Another possibility is to introduce a dedicated time-keeping unit and have this unit broadcast a time all other units can sync to. Transmit time would then be irrelevant as long as it is the same for all receiving units.

I will not be able to got to all the events that my friends organize, some of then will be at the week and at that days I'm at the university, so it must be preferably an automatic and transparent system so that theu dont have to mess with it, as the end result will be one module or the two broken down. I have read it, thats almost what I was thinking about, thanks ;) Its a pity that I cant find any code for the server side :/

Could the time be resolved precisely enough with a GPS in each unit? We start and stop remote loads attached to a single generator this way to avoid jolting the engine and generator with each start and stop

I will search about the GPS idea too, but it must be reliable..

You can pretty much count on a gps to work if it ever worked. The trick is to write something that will grab the millisec at the same moment relative to a minute roll over or something noteable. I have never attempted anything very precise because we are happy with 50 ms, which is only three sixty cycle waveforms. Good enough for us.

Our gps s output a 1 second square wave. I do not know if the signal is synchronized to real time. If it is, great, you could watch the serial stream from the gps until a strategic roll over takes place, then watch the square wave for a programmed number of transitions and sync on a transition.

sync_millis = millis();

After that:

elapsed_time = millis() - sync_millis

A button would notify the board to sync on the next five minute roll over, or what ever. Sync both ends at say 9:00 and they should be good for a day. A lamp will let you know that a good sync has taken place.