Pages: [1]   Go Down
Author Topic: Problem with millis(); resets within 60 seconds  (Read 760 times)
0 Members and 1 Guest are viewing this topic.
Eindhoven, The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 17
better your best
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 291
Posts: 25858
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int  != unsigned long
Logged

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26523
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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.
Arduino for Teens available at Amazon.com.

Eindhoven, The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 17
better your best
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Online Online
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  unsigned int x = millis();

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

Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4773
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26523
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 96
Posts: 4773
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
God Member
*****
Karma: 17
Posts: 723
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Eindhoven, The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 17
better your best
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 smiley

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
Jump to: