Time library and millis() rollover

Hi,

I am new to arduino and to C like programming language so if it’s a stupid question, sorry!!

Based on the code below I would conclude that after a rollover of millis() it is always lower that prevMillis and the nextSynctime will always be lower than the systime. My conclusion is that systime would stay the same am I correct?

time_t now(){
  while( millis() - prevMillis >= 1000){      
    sysTime++;
    prevMillis += 1000;	
#ifdef TIME_DRIFT_INFO
    sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift     
#endif	
  }
  if(nextSyncTime <= sysTime){
	if(getTimePtr != 0){
	  time_t t = getTimePtr();
      if( t != 0)
        setTime(t);
      else
        Status = (Status == timeNotSet) ?  timeNotSet : timeNeedsSync;        
    }
  }  
  return sysTime;
}

I would excpect something like:

If (millis() < prevMillis) {
prevMillis = 0;
nextSyncTime = sysTime; 
}

This would invoke an update e.g. from NTP.

Regards,

Mark

while( millis() - prevMillis >= 1000){

if prevMillis is declared a unsigned long this code works because the result will underflow and become bigger than 1000 again if a rollover occurs.

Time library : http://arduino.cc/playground/Code/Time

If you still want to handle potential rollovers for the millis() and micros() functions, but don’t need the other functions of the Time library, there’s also TimingRollover. It’s more of a snippet of code than a library and therefore will take-up less space on the IC chip.

Edit: The linked code is a similar idea to what pylon posted, but it uses an If statement and there’s more explanation for the math involved.

Thanks for the clarification!

Regards,

Mark