Go Down

Topic: Stopwatch with Pause/Lap  6 digit 7 seg LED. HELP! (Read 123 times) previous topic - next topic


CrossRoads

Thanks Paul.
I didn't think it was possible for that little bit of code take more than 10mS - I mean that's
.01/(1/16,000,000) which is 160,000 clock cycles - altho I couldn't estiate pi recently :-) . I'll run the code with an oscilloscope tomorrow and see how long it really is.
(I check every 250ms in my 10:00, 3:00, 1:00 count down timer).

I see your point about only incrementing by 1 hundredth  when the elapsed interval may have been 10.001 mS, and then losing that .001.
It seems that perhaps previousMillis should be increased by the amount of (currentMillis-previousMillis) to catch the full amount if time elapsed, vs just interval, which is fixed at 10mS. Unless that is what you are suggesting and I just didn't read it that way. I suppose we could check to see if the difference is more than 2x interval and then throw in the occasional increment by 2 to catch up also.
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.

CrossRoads

Thanks also Coding, I think you snuck that in while I was typing my other response and looking up what += did.
Got some testing to try tomorrow ...
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.

Coding Badly

Quote
Thanks Paul.

Paul?  Who's Paul?

Quote
I didn't think it was possible for that little bit of code take more than 10mS - I mean that's
.01/(1/16,000,000) which is 160,000 clock cycles

Well, I could easily be wrong.  For example: I don't know what shiftOut does or how it's coded.  I assumed that it may wait for an acknowledgement.

Quote
altho I couldn't estiate pi recently

Ouch!

Quote
I'll run the code with an oscilloscope tomorrow and see how long it really is.

Definately a good plan before changing anything.

Quote
(I check every 250ms in my 10:00, 3:00, 1:00 count down timer).

Do you have a timer that's showing an error?

Quote
I see your point about only incrementing by 1 hundredth  when the elapsed interval may have been 10.001 mS, and then losing that .001.
It seems that perhaps previousMillis should be increased by the amount of (currentMillis-previousMillis) to catch the full amount if time elapsed, vs just interval, which is fixed at 10mS.

Work through by hand using a large loop time (like 20 ms).  I believe you'll find the error is still there.

Quote
I suppose we could check to see if the difference is more than 2x interval and then throw in the occasional increment by 2 to catch up also.

That's essentially how the second suggestion works...

Code: [Select]
 currentmillis = millis();  // read the time.

 while (currentmillis - previousmillis >= interval)
 {
   // 10 milliseconds (or more) have gone by.

   // Your code to update the counters goes here.

   previousmillis += interval;
 }

CrossRoads

"Paul?  Who's Paul?"

Sorry about that coding,  I had PaulS on my mind, it was 2:00AM, had been reading stuff both of you had posted. Mostly just tired I think.
My pi estimate - I was thinking 22/7, but had 3, 22, 7 going around in my head, somehow got 7/3 = 2.x so it seemed to make sense in distracted way :-)

I didn't check my timer against anything - it only does minutes, seconds, and I fixed things like when time started it didn't immediately jump down 1 second, but took a second to get there. Or if you stopped time as soon as it changed a second, it didn't immediately drop to the next second when restarted. Those were obvious. I didn't get down into the 10s of mS like this code is doing.

Now, where'd my USB scope end up ...
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.

Go Up