Pages: [1]   Go Down
 Author Topic: Problem with millis(); resets within 60 seconds  (Read 427 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 6
 « on: May 26, 2012, 01:41:13 pm » Bigger Smaller Reset

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:
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!
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #1 on: May 26, 2012, 01:44:52 pm » Bigger Smaller Reset

int  != unsigned long
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 249
Posts: 16583
Available for Design & Build services
 « Reply #2 on: May 26, 2012, 02:26:02 pm » Bigger Smaller Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17

Offline
Newbie
Karma: 0
Posts: 6
 « Reply #3 on: May 26, 2012, 03:43:26 pm » Bigger Smaller Reset

So, like this?:
Code:
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...
 Logged

Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
 « Reply #4 on: May 26, 2012, 03:47:22 pm » Bigger Smaller Reset

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?
 Logged

Gosport, UK
Offline
Karma: 19
Posts: 3117
 « Reply #5 on: May 26, 2012, 03:48:14 pm » Bigger Smaller Reset

Code:
unsigned int x = millis();

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

 Logged

Pittsburgh, PA, USA
Offline
Karma: 31
Posts: 2947
I only know some basic electricity....
 « Reply #6 on: May 26, 2012, 04:33:00 pm » Bigger Smaller Reset

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.
 Logged

Examples can be found at Learning in the Main Site and at the Playground

Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 249
Posts: 16583
Available for Design & Build services
 « Reply #7 on: May 26, 2012, 06:06:34 pm » Bigger Smaller Reset

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)
 Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17

Offline
Sr. Member
Karma: 0
Posts: 451
I am a amateur.
 « Reply #8 on: May 26, 2012, 06:44:40 pm » Bigger Smaller Reset

Quote
if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)
Don't you mean:
Code:
if (x>60,000 && x <=120,000)
if (x> 120,000 && x <=300,000)
 Logged

Pittsburgh, PA, USA
Offline
Karma: 31
Posts: 2947
I only know some basic electricity....
 « Reply #9 on: May 26, 2012, 07:13:36 pm » Bigger Smaller Reset

Quote
if (x>60,000 & x <=120,000)
if (x> 120,000 & x <=300,000)
Don't you mean:
Code:
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.

 Logged

Examples can be found at Learning in the Main Site and at the Playground

Offline
Sr. Member
Karma: 6
Posts: 403
 « Reply #10 on: May 26, 2012, 07:22:59 pm » Bigger Smaller Reset

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.
 Logged

Offline
Newbie
Karma: 0
Posts: 6
 « Reply #11 on: May 27, 2012, 02:34:47 am » Bigger Smaller Reset

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:
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:
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);
 Logged

 Pages: [1]   Go Up