Go Down

Topic: adding up millis (Read 950 times) previous topic - next topic

stoopkid

I think the point is, though, that as long as they are all unsigned longs, if millis() were to rollover. Then when you subtract timeEarlier from timeNow, it will rollover backwards and still give you the difference. The only thing that would mess this up is if you tried to subtract more than 49 days, then your actual calculation would "rollover". But like I said, I'm not worried about that. I'm only worried about millis() rolling over.

Arrch

#6
Mar 01, 2013, 09:46 pm Last Edit: Mar 01, 2013, 09:49 pm by Arrch Reason: 1


If your math is all done in the form of

elapsedTime = (timeNow - timeEarlier);  // add elaspedTime to add  rideTime summary
where timeNow is the stopped time, and timeEarlier is the time you started

and all time related variable are unsigned long,
then there will not be a rollover problem.


If timeNow and timeEarlier are values form millis(),
then that will only work if the millis() timer is guaranteed to never rollover
between TimerEarlier and timeNow.

To ensure no rollover during the elapsed time requires zeroing out the millis() timer value.
(which is easy)
And if the millis() timer value has to be zeroed to make it work, why not then just
zero at "timeEarlier" when the elapsed time starts, and then the elapsed time
is merely mills() when you get to timeNow.
This avoids the calculation altogether.
It then simply becomes a matter of when to zero out millis().


--- bill


Incorrect, subtracting two unsigned numbers will result in the difference between the two numbers, including wrap-around. Try it for yourself.

The only caveat is that you can only detect an elapsed time that is less than the maximum amount that can be stored in the n-bits you are working with.

CrossRoads

Arrch, which part are you saying is incorrect?
To  my mind,
time now   -  time earlier  = elapsed time
0x00000020 - 0xFFFFFF20 = 0x00000100

You may get the wrong result if the calculation is made after the time has rolled over for 24+ days or something on the extremes:
But normally it will be correct.
I would expect "ride time" in this case might be on a per trip basis, or added & stored in EEPROM, but millis() would be reset when the engine was shutdown and later restarted.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Arrch


Arrch, which part are you saying is incorrect?


This part:
Quote
If timeNow and timeEarlier are values form millis(),
then that will only work if the millis() timer is guaranteed to never rollover

stoopkid

Code: [Select]
byte timeEarlier = 250;
byte timeNow = 5;
byte timeDifference;
void setup(){
Serial.begin(9600);
timeDifference = timeNow - timeEarlier;
Serial.println(timeDifference);
}

void loop(){
 
 
}


returns 11

That's the difference, despite the fact that the byte rolled over.

Go Up