Go Down

Topic: Problem with millis(); resets within 60 seconds (Read 882 times) previous topic - next topic

Delta_G


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. 

Reflex



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