Go Down

Topic: Millis comparison, am I missing something? (Read 108 times) previous topic - next topic

septillion

Hi,

I wanted to test something so I started printing millis() every period.

Simple piece of code:
Code: [Select]
const unsigned long Interval = 10;
unsigned long millisLast = -Interval;

void setup(){
  Serial.begin(115200);
}

void loop(){
  const unsigned long MillisNow = millis();
  if(millis() - millisLast >= Interval){
    Serial.print(millisLast);
    Serial.print("\t\t"); 
    Serial.print(MillisNow);
    Serial.println();

    millisLast = MillisNow;
  }
}


But the output is:
Code: [Select]
4294967286 0
0 10
10 20
20 30
30 40
40 50
50 60
60 69
69 79
79 89
89 99
99 109
109 118
118 128
128 137
137 147
147 157
157 167
167 177
177 187
187 197
197 206
206 216

Up to 60 it's what I expected. And I was expecting maybe some periods to be longer because of to slow serial or unlucky interrupt firing or something. What I didn't expect was what is shown at 60, 109, 128 and 197. The period is SHORTER than I set. But that would make the comparison 69 - 60 >= 10 false...

So what am I missing. Why is the comparison true (otherwise it would not print) but if I calculate it, it should have been false... ::) Must be something I'm missing.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Romonaga

#1
Aug 22, 2019, 09:40 pm Last Edit: Aug 22, 2019, 09:43 pm by Romonaga
I suspect that the first millis where you are storing millis into MillisNow is the issue.

Your check for millis is calling millis again, chances are this value is different from the one captured in const unsigned long MillisNow = millis();

Change your if to if(MillisNow - millisLast >= Interval)

Code: [Select]

const unsigned long MillisNow = millis();
  if(millis() - millisLast >= Interval)

septillion

#2
Aug 22, 2019, 09:53 pm Last Edit: Aug 23, 2019, 09:38 am by septillion
THANK YOU! Literally looked at it for half an hour... As you can see, I made a millis "snapshot" just to not have this problem. But I forgot to use it, DOH!

Maybe it's time to go to bed ::)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Romonaga

#3
Aug 22, 2019, 10:02 pm Last Edit: Aug 22, 2019, 10:03 pm by Romonaga
THANK YOU! Literally looked at it for half an hour... As you can see, I made a millis "snapshot" just to not have this problem. But I forgot to use it, DOH!

Maybe it's time to go bed ::)
:) Been there done that!  Have a good nights sleep.  They funny thing is, when I saw who posted this question I was like, why would he need help for something so simple.  ROFL.

septillion

#4
Aug 23, 2019, 09:41 am Last Edit: Aug 23, 2019, 09:41 am by septillion
Yeah, I was really scratching my head. Especially because I thought I ensured to use the same timestamp during a loop cycle. I was sure it must be something I did wrong (not blaming the compiler like newbies ::)) But now I'm fresh (uhum, coffee) in the morning :) Thanks for helping me out! Sometimes you just go blind on your own (very simple) code.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Robin2

#5
Aug 23, 2019, 10:00 am Last Edit: Aug 23, 2019, 10:02 am by Robin2
I you do it like this you will avoid small accumulating errors if the test is occasionally a little late (i.e. on the wrong side of a millis() update
Code: [Select]
if (currentMillis - prevMillis >= interval) {
   prevMillis += interval;



}

However if you are not careful that can bite you in the bum if the sequence starts with (for example) prevMillis = 0 and actual millis() = (say) 9473 and the interval = 100.

So in most cases I reckon this is sufficient
Code: [Select]
[code]if (millis() - prevMillis >= interval) {
   prevMillis = currentMillis;


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up