Millis comparison, am I missing something?

Hi,

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

Simple piece of code:

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:

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... ::slight_smile: Must be something I'm missing.

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)

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

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 ::slight_smile:

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 bed ::slight_smile:

:slight_smile: 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.

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 :slight_smile: Thanks for helping me out! Sometimes you just go blind on your own (very simple) code.

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

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]if (millis() - prevMillis >= interval) {
   prevMillis = currentMillis;

...R