Millis overflow?

I’ve got a bit of code that sometimes hangs:

    if(millis()>startMillis+3000){
      gameMode=5;
    }

It doesn’t go past here, but only sometimes. startMillis is set = to millis() by a button press in a previous gameMode.

Is this because millis is overflowing? Is there a elegant way to capture this or should I just use my own counter?

You could look at how the Arduboy2 library nextFrame() function uses the millis() timer. It properly handles timer overflows.

Also look at this:
https://www.norwegiancreations.com/2018/10/arduino-tutorial-avoiding-the-overflow-issue-when-using-millis-and-micros/

1 Like

I knew it was something simple, I was scratching my head on it. The only reason I posted it was I figure other people have run into it before might be worth talking about. Thanks!

1 Like

It would be good to know what datatype you’re using for startMillis. It should be at least unsigned long (uint32_t for AVR).

However, I’m wondering if something millis() does is interfering with the timers the sound libraries are using.

well it was an int, so that would explain the behavior more. :rofl: Is there some way to mark two solutions or should that be the correct one?

I’m still not entirely sure if overflow actually was the source of the problem, but if it is then @MLXXXp’s comment should be the solution because the linked tutorial tackles the problem of overflow in depth.

With great solution button comes great responsibility