# Problem with millis(); resets within 60 seconds

In my code I have a variable: delay(i)
I want to create an irregular time pattern in which variable i changes:
for example:
first 60 seconds, i=1000
then untill 120 seconds, i=2000
then from 120 till 300, i=5000
and so on.

It seems the timer doesn’t move past 60 as i=1000 even after the first minute.
The code I have used is as follows, and did manage to change the variable when I was using intervals of 10 seconds.

``````int i;
int x = millis();

if (x < 60000) {i = 1000;}
else if (x < 120000){i = 2000;}
else if (x < 300000){i = 5000;}

delay(i);
``````

Help is much appreciated!

int != unsigned long

millis() uses unsigned long variables, make your other time elements unsigned long also. also use UL after the big numbers: 60000UL 30000UL etc.

1 Like

So, like this?:

``````int i;
unsigned int x = millis();

if (x < 60000UL) {i = 1000;}
else if (x < 120000UL){i = 2000;}
else if (x < 300000UL){i = 5000;}

delay(i);
``````

Still not working…

Still not working...

Yes, it is. It just isn't doing what you want. So, what do you want? And, what IS it doing?

``````  unsigned int x = millis();
``````

Read the other posts again. Not unsigned int, unsigned long.

Reflex,

int can be -32768 to +32767 unsigned int can be 0 to 65536

unsigned long can be 0 to 4294967295, enough to run 49.71... days before rolling over.

(now into infomercial mode) But Wait! There's MORE! When you use subtraction with unsigned numbers you always get the difference between the two even when subtracting a big value from a small one (like the difference between 50 days and 49 days will still give 24x3600x1000= 1 day) so you never have to reset the clock, so to speak.

If you run Windows and use the included calculator then check the View pulldown for Scientific mode. The calculator will get bigger and allow you to work in hexadecimal (the Hex button) or binary (Bin button) and switch between that and decimal. It's much easier to do bit-math in hex once you get used to it.

Isn’t x <60000 for all 3 cases to start?

if (x < 60000UL) {i = 1000;}
else if (x < 120000UL){i = 2000;}
else if (x < 300000UL){i = 5000;}

So what’s the code to do?

Maybe try changing your conditions to make them unique:

if (x<= 60,000) (commas added for clarity)
if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)

if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)

Don’t you mean:

``````if (x>60,000 && x <=120,000)
if (x> 120,000 && x <=300,000)
``````

dkl65:

if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)

Don’t you mean:

``````if (x>60,000 && x <=120,000)
``````

if (x> 120,000 && x <=300,000)

Either way should get the same answer when TRUE == 1 and FALSE == 0. Only bit 0 is affected.

Isn’t x <60000 for all 3 cases to start?

if (x < 60000UL) {i = 1000;}
else if (x < 120000UL){i = 2000;}
else if (x < 300000UL){i = 5000;}

So what’s the code to do?

Maybe try changing your conditions to make them unique:

if (x<= 60,000) (commas added for clarity)
if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)

I think the else takes care of that. You only go to the second or third conditional if the ones before it weren’t true. So to get into the second conditional there’s no need to retest x > 60000 because you wouldn’t be past the else if it was less than.

Delta_G:

Maybe try changing your conditions to make them unique:

if (x<= 60,000) (commas added for clarity)
if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)

I think the else takes care of that. You only go to the second or third conditional if the ones before it weren’t true. So to get into the second conditional there’s no need to retest x > 60000 because you wouldn’t be past the else if it was less than.

Delta_G: Yes, that is correct, I built in the ‘if else’ so there is no need to fill in every number twice as would be the case in a (X>a && x<=b) construction.

dxw00d:

``````  unsigned int x = millis();
``````

Read the other posts again. Not unsigned int, unsigned long.

GoForSmoke:
Reflex,

int can be -32768 to +32767
unsigned int can be 0 to 65536
unsigned long can be 0 to 4294967295, enough to run 49.71… days before rolling over.

Yes! This is very helpful! Thank you guys very much, this code is now doing what I want ``````int i;
unsigned long x = millis();

if (x < 60000UL) {i = 1000;}
else if (x < 120000UL){i = 2000;}
else if (x < 300000UL){i = 5000;}

delay(i);
``````