Millis and Using Multiple Arduino

Hi,

I'm doing a project that needs to be synchronized between 4 arduinos and looping during 1month.
I'm using millis and one power supply for four, but it seems there is two problems.

  1. Four arduinos are synchronized exactly for the first setSTART, but they are not synchronized exactly from second setSTART, and
  2. They did Turn off after some hours.

The code is like below:

long time =0;

void loop()
{

long offset = millis() - time;

if (offset >= 0 && offset <1000) setSTART(offset);
else if (offset >= 1000 && offset <5000) setA(offset);
else if (offset >= 5000 && offset <8000) setB(offset);
else if (offset >= 8000 && offset <10000) setC(offset);
else if (offset >= 10000){
time = millis();
setSTART(offset);
}

}

Please, any help?
Thanks!
Kao

Kao,

From what I can see the "second setSTART" occurs at some point 10 seconds or more after the first one, right? There are a number of reasons why the four Arduinos would not be synchronized at that point. For one, are setSTART, setA, setB, and setC deterministic, i.e., do they take a fixed, predictable amount of time? Even if so, the difference in accuracy between the crystals that drive the 4 CPUs will cause some disparity in each Arduino's perception of the time and quickly lead to them becoming out of sync.

Mikal

Hi Mikal,

Yes, "second setSTART" occurs additional millis after the first one - unfortunately this is quite unpredictable amount of time, and each arduinos makes different additional millis. Do you mean this is a hardware problem than my code's problem?

I just read about millisRollover() function. Do you think it will be helpful? It's a hard problem....

Thanks!!!

Hi Kao,

The first thing you need to do is address the millis rollover problem. You can do this by using new millis code in wiring.c as discussed in this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210889307/#3

Your arduino boards will drift by a few seconds per day due to tolerances in the crystal on the boards. If you need greater accuracy than that then you either need to drive the boards from the same clock source or sync the boards at regular intervals.

There is a DateTime library in the playground here Arduino Playground - DateTime that may be useful for handling time in you application and the example sketch shows how to syncronise time using the serial port. One of you Arduinos could send out a time sync message every hour or so and the others listening on the serial port would sync to that time.

The DateTime library maintains system time in seconds and there is a now() function that returns the number of seconds since the system was started. If your application does not need clock times and dates than you can ignore this functionality and just use the elapsed seconds returned from now().

I hope that helps.

Mikal - thanks a lot!

I just want to make sure two things:

I changed the below parts of wiring.c - Did I do correctly?
To use DateTime library, I need an external computer, right?

// The number of times timer 0 has overflowed since the program started.
// Must be volatile or gcc will optimize away some uses of it.
volatile unsigned long timer0_clock_cycles = 0;
volatile unsigned long timer0_millis = 0;

SIGNAL(SIG_OVERFLOW0)
{
// timer 0 prescale factor is 64 and the timer overflows at 256
timer0_clock_cycles += 64UL * 256UL;
while (timer0_clock_cycles > clockCyclesPerMicrosecond() * 1000UL) {
timer0_clock_cycles -= clockCyclesPerMicrosecond() * 1000UL;
timer0_millis++;
}
}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

cli();
m = timer0_millis;
SREG = oldSREG;
testCount++; // added for this test to count total calls to this function

return m;
}

Kao--

I don't think you need an external computer to use DateTime. Perhaps the illustrious author (mem!) can enlighten? :slight_smile:

Mikal