I am now working on a project which is expected to last at least 5 years from the time it is completed. In that project, i can not use delay() for some purposes so I am using millis() method

unsigned long Then = 0;
unsigned long Threshold = 1000;

void setup(){
    // Codes

void loop(){
    unsigned long Now = millis();
        // Execute a function or functions
        Then = Now;

Now in this code wee can see the variable 'Then' and 'Now' are getting updated whenever threshold is met.

So now, suppose after a certain period of time (without any switch off), if any one of the two ever-updating variables which are 'Now' or 'Then' gets a value which is out of range for 'unsigned long' type

Will the ic continue to behave normally or will it malfunction?

Thank you!

  • 1
    let say millis() rolled over to 5 and the last count was 12345678: (5-12345678) > 1000; no problem.
    – dandavis
    Jun 19 at 8:34
  • In the unsigned arithmetics it results into distance between those two numbers, so it'll be fine. (negative numbers are stored in two's complement)
    – KIIV
    Jun 19 at 9:49
  • Well I don't care about rollovers or overflows until my system starts malfunctioning. Jun 19 at 10:03

Browse other questions tagged or ask your own question.