Change millis() value

I've done a project with an ESP8266 board but after approximately 2 month it stops working correctly, I suspect the problem is related to the millis() value overflow but since the board it's mounted in an inconvenient position I would like to do the debugging process on another board at my desk and most importantly I don't want to wait 59 days.

My question is: can I start the program with the Millis value set so that there are only a few minutes left before the overflow? I swear I've read an answer to this exact problem but I can't find it anymore, can someone help me?

Thanks

1 Like

When used correctly the millis() overflow will not cause a problem

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

1 Like

Post your code. It's pretty easy to spot millis code that will/won't overflow.

if (currentMillis - previsouMillis > duration) // Won't overflow 

if (currentMillis > previousMillis + duration) // Will overflow

I’ve followed that guide and I’ve already done some research on the millis overflow subject. My code seems structured correctly but before creating a topic where I ask for help I would like to to provide more information about the program behavior so that’s why I wanted to know what happens when the value overflows

Can you put a test together using micros? it overflows a lot faster obviously.

1 Like

We could walk you through every possible millis permutation, but your answer will appear within two or three replies if you post your code (in code tags).

Please post your complete sketch here so that we can see what you have done

Basically, if you use unsigned longs for timing variables, subtraction to test for end of period and the period is not longer than 49 days then the millis() overflow will not cause a problem

If you are using variables for current, previous, and duration then these are typically unsigned long (32 bit), and logic will/can overflow in approximately 49 days.

If you use unsigned int (16 bit), you will overflow in about 66 seconds.

The example below demonstrates this.

unsigned int current;
unsigned int previous;
unsigned int duration = 10000; // 10 seconds


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

  previous = millis();
}

void loop()
{
  current = millis();

  if (current - previous > duration) // Won't overflow
//  if (current > previous + duration) // Will overflow
  {
    Serial.println(current);
    previous = current;
  }
}

Yeah. Don't do that. I already have. The original poster can search the forum for that.

Please read

a7

The ESP8266 has a Timer API that could be scaled to timeout simulating a millis() overflow for the debugging process.

Yes, this is one reason I like micros() better than millis(). If something is going to fail, you want it to fail fast so that you know about it and can fix it.

I was hoping that it was as simple as doing timer0_millis = 4294367295 as I seemed to have read but at this point I should just open another topic to ask the problem directly

NO - don't do that

If you want to experiment with rollover then test it using micros() or write your own myMillis() function that increments a variable and use that as a substitute for the value returned by millis() in your sketch

As previously requested, please post your full problem sketch here

That would result in your new topic getting flagged up to the moderators for cross posting, which is against forum rules.

Continue on this topic. If you want to change the topic title, you can do that by editing the first post. If you want to move the topic to another forum section, flag the first post to the moderators and ask them to move it, or just ask. Some forum members, like me for example, can move posts even though we are not moderators.

If you handle time differences through unsigned subtraction, rollover is a non-issue that confuses beginners.

Unsigned time is a round clock. If it is now 5 and I want a single simple 1-step formula to know how many hours elapsed since any start hour in the last 12 hours, I start at 5 and move the hour hand left start-hour times. 5 - 10 = 7, 5 - 4 = 1. Always works.

I could unsigned add 7 to 10 on my base 12 clock and get 5 but then how to test for time's up when 11 > 5? Use an if for rollover makes 2 steps necessary where 1 step will do. I tried that stuff myself long ago, haven't forgotten why it's no good.

I swear it's almost obscene how fast and firmly people can f-up a sure thing!

1 Like

Thanks, I hope I have enough time during my lunch break to post the code or else I have to wait until tonight

unsigned longs are unsigned longs

so assigning
currentTime = millis();
or
currentTime = millis() + 4294967295 - 1000; // rollover in 1 second

uses the exact same math

best regards Stefan

const long interval = 7000;
void setup() {
  Serial.begin(115200);
  unsigned long currentMillis = 3000;
  Serial.println(currentMillis - interval);
}
void loop() {
}