Hi, I am working on a project that requires me to use 2 separate Arduinos running independently from each other. Now, both of these Arduino's are running the same code, but I noticed that after 10 minutes or so, one of them falls behind and this time difference keep increasing with time. Like I already mentioned, the Arduino`s are identical and I bought them at the same time and they are running the same copy of the program. Any ideas what might cause this and how can I fix it?
Thank you.
Here is the link to the Arduino that I bought just in case.
Like I mentioned, it starts with a small variation but after an hour of the start the variation goes up to few seconds. I will grow larger if I let it on for a longer time.
Some variation in the accuracy of millis(), etc. is to be expected. I'd guess the Nanos are using ceramic resonators for the clock source. The clock speed of the resonator could be off by 1%. So worst case scenario that could add up to 2% variance in clock speed between your Nanos. The variation you're seeing is only around 0.08% so actually pretty good considering! You could add an RTC to each for more accurate time keeping.
The most important question is: does it matter? If your project depends on absolutely synced Nanos, then even an RTC will not help, you must use a shared clock source or a "sync signal" from one Nano to the other from time to time
Keep track of time on the faster one using micros(), and once a minute or every 30 seconds or every 10 seconds, send a time blip to the other to get it back in sync.
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;
// Initial time to start, 00:00:00 with 0 years, 0 days.
// adjust as needed.
// Get this working, then more display code if needed
byte hundredths;
byte tenths;
byte secondsOnes = 0;
byte oldsecondsOnes;
byte secondsTens = 0;
byte minutesOnes = 0;
byte minutesTens = 0;
byte hoursOnes= 0;
byte hoursTens = 0;
int days = 0; //
byte years = 0;
void setup(){
Serial.begin(115200); // make serial monitor match
Serial.println ("Setup Done");
}
void loop(){
currentMicros = micros();
// how long's it been?
elapsedTime = currentMicros - previousMicros;
if ( elapsedTime >=10000UL){ // 0.01 second passed? Update the timers
previousMicros = previousMicros + 10000UL;
hundredths = hundredths+1;
if (hundredths == 10){
hundredths = 0;
tenths = tenths +1;
if (tenths == 10){
tenths = 0;
secondsOnes = secondsOnes + 1;
if (secondsOnes == 10){
secondsOnes = 0;
secondsTens = secondsTens +1;
if (secondsTens == 6){
secondsTens = 0;
minutesOnes = minutesOnes + 1;
if (minutesOnes == 10){
minutesOnes = 0;
minutesTens = minutesTens +1;
if (minutesTens == 6){
minutesTens = 0;
hoursOnes = hoursOns + 1;
if (hoursOnes == 10){
hoursOnes = 0;
hoursTens = hoursTens +1;
if ( (hoursTens == 2) && (hoursOnes == 4){
hoursOnes = 0;
hoursTens = 0;
if (days>1){
days = days-1;
}
else {
days = 365;
years = years - 1;
}
} // 24 hr rollover check
} //hoursTens rollover check
} // hoursOnes rollover check
} // minutesTens rollover check
} // minutesOnes rollover check
} // secondsTens rollover check
} // secondsOnes rollover check
} // tenths rollover check
} // hundredths rollover check
} // hundredths passing check
if (oldSecondsOnes != secondsOnes){ // show the elapsed time << or Send a time blip to the other Processor
oldSecondsOnes = secondsOnes;
Serial.print (years);
Serial.print(":");
Serial.print(days);
Serial.print(":");
Serial.print(hoursTens);
Serial.print(hoursOnes);
Serial.print(":");
Serial.print(minutesTens);
Serial.print(minutesOnes);
Serial.print(":");
Serial.print(secondsTens);
Serial.println(secondsOnes);
} // end one second check
} // end loop