Hi! This is my first post on the forum, so I hope I'm doing things right.

So I have this "waiting" loop that waits 10 seconds for the user to push a button. If the button is not pushed, it will exit the loop and carry on with the rest of the program.

To achieve that, I used the millis(); function. I'm sure there's many ways out there that I can implement this function but this way seems to work for what I need.

How can I make sure my code accounts for millis(); overflowing?

Edit: I have removed my code because of a misunderstanding that I had. I do not want to cause more confusion.

I've never heard of a "backwards millis()" before. Can you please explain? The millis() in Arduino core always counts up.

Considering your code, it looks really confused. Have you looked at standard examples of millis() usage, such as the Blink Without Delay example sketch?

My apologies, I was confused. I have now edited my post accordingly.

I'm talking about millis(); rollover overflowing and going back to zero after 49.7 days.

For reference:

Essentially, this function is called when an interrupt is detected. For it to pass by, either 10 seconds have to pass or the user has to push a button, whichever comes first.

I hope this clears things up a bit.

You're not absorbing the material in the references. Your code doesn't follow the prescribed design patterns. How can we help? Is there some reason you decided to go off and do it in a totally non-standard way?

arduinomar:
I'm talking about millis(); rollover overflowing and going back to zero after 49.7 days.

Don't you expect the 10 seconds to lapse well before 49.7 days?
When you subtract PreviousTime from CurrentTime you always get a positive answer. Even if CurrentTime is near zero because of a roll-over and PreviouTime is very large because it was just before the roll-over. The subtraction makes PreviousTime act as a small negative number which is subtracted from the small positive number to produce a larger positive number. YOU DON'T HAVE TO DO ANYTHING SPECIAL FOR MEASURING INTERVALS LESS THAN 49.7 DAYS.

49.7 days and then rolls backwards.

The more correct terms are rolls over or overflows. An unsigned long variable (millis()) rolls over from 4,294,967,295 to 0. It starts over at 0 and counts up.
A long (signed long) data type rolls over at 2,147,483,647 to start again at -2,147,483,648 and counts up.

``````(signed long)(startMillis - currentMillis)
``````

Always used unsigned long for time variables.

Thank you everyone, it seems that I need to go back and understand the core of the function. I posted my problem here too soon. I appreciate you taking the time to reply!

arduinomar:
Thank you everyone, it seems that I need to go back and understand the core of the function. I posted my problem here too soon. I appreciate you taking the time to reply!

Well, I'm not sure about that. Most people post here before they solve a problem, not after! I think what you might have done too soon, is coding. However, it's not the core that you need to understand, it's the appropriate usage. 'millis()' itself is a really simple, obvious function. It's the design patterns that use it as an interval timer, etc., that have some subtlety and need some attention to learn. But those are not a mystery, most resources that talk about it, do exemplify the correct approach.

Take a look at my tutorial on millisDelay which covers overflow and wrap around.
How to code Timers and Delays in Arduino

You might also like to clarify what happens in the 10 seconds. Seems to me that everything's supposed to stop, which is not the normal behaviour during a millis()-based "non-delaying-delay". Usually, the rest of the code needs to carry on while you wait for something to happen.

Maybe in this case you would use a while() which would end after the 10s is up, and check for the button inside the while and break if it's pressed.

you shouldn't re-edit postings so strong that given answers make no more sense. correctng typos or deleting empty lines is appreciated. Correcting more should be done in a new posting. in general posting complete sketches is a good idea. So please post your complete sketch.

if variables of type unsigned long are used for millis(). the pre-programmed math how substracting a (rolled over) small value from a big value
example
123 - 2.123.567.789 is done in a way that even in this case the if-condition

if (currentMillis - previousMillis >= myInterval) will work in all cases.
regardless of currentMillis or previousMillis beeing a small or big number

that's the reason why in all tutorials it is used this way

best regards Stefan