Go Down

### Topic: Problem with millis(); resets within 60 seconds (Read 1 time)previous topic - next topic

#### Reflex

##### May 26, 2012, 08:41 pm
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.

Code: [Select]
`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!

#### AWOL

#1
##### May 26, 2012, 08:44 pm
int  != unsigned long
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### CrossRoads

#2
##### May 26, 2012, 09:26 pm
millis() uses unsigned long variables, make your other time elements unsigned long also.
also use UL after the big numbers:
60000UL
30000UL etc.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Reflex

#3
##### May 26, 2012, 10:43 pm
So, like this?:
Code: [Select]
`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...

#### PaulS

#4
##### May 26, 2012, 10:47 pm
Quote
Still not working...

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

#### dxw00d

#5
##### May 26, 2012, 10:48 pm
Code: [Select]
`  unsigned int x = millis();`

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

#### GoForSmoke

#6
##### May 26, 2012, 11:33 pm
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.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### CrossRoads

#7
##### May 27, 2012, 01:06 am
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)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### dkl65

#8
##### May 27, 2012, 01:44 am
Quote
if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)

Don't you mean:
Code: [Select]
`if (x>60,000 && x <=120,000)if (x> 120,000 && x <=300,000)`
Visit http://www.glenof.webs.com/!

#### GoForSmoke

#9
##### May 27, 2012, 02:13 am

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

Don't you mean:
Code: [Select]
`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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### Delta_G

#10
##### May 27, 2012, 02:22 am

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.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

#### Reflex

#11
##### May 27, 2012, 09:34 am

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.

Code: [Select]
`  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.

Yes! This is very helpful! Thank you guys very much, this code is now doing what I want

Code: [Select]
`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);`

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy