 # Variables that don't make sense

Hi. Long story short, when I set seconds=5 for example, the IF statement becomes true after 5 seconds of restarting the arduino (as it should), but if I set seconds=60 for example, the IF statement never becomes true, even after 60 seconds. Why is that?

Board: Arduino UNO

``````unsigned long timer1=0;

int voltageraw=0;

float voltage=0;

bool relay=0;

void setup()

{

pinMode(2, OUTPUT);

pinMode(A2, INPUT);

Serial.begin(57600);

Serial.println("Begin...");

}

void loop()

{

int seconds=60;

if(millis()>=timer1+1000*seconds)

{

digitalWrite(2, 1);

delay(50);

unsigned long timer2=millis();

while(millis()<timer2+10*1000)

{

}

digitalWrite(2, 0);

voltage=(voltageraw/1024.0)*12.5;

Serial.println(voltage, 4);

timer1=millis();

//Serial.println(timer1);

}

//pin writings

Serial.println(timer1);

delay(500);

}``````

Try:

`if(millis() - timer1 >= 60000ul)`

Or, 'int' or else 'const int'...

and

``````if(millis() - timer1 >= seconds*1000ul)
``````

what is 60000ul? I never saw that before

Thanks! it worked.

60000 is 60 seconds (60,000 ms), ul or UL says to the compiler unsigned long number. And helps document or reminds us we are careful as we are doing unsigned math.

1 Like

I remember seeing 60000.0 for example, as an alternative to 60000ul. Is it the same thing? If yes, which is better? Is it to my choice which variant to use?

The reason is because expressions are evaluated using 'int' arithmetic by default.

No, that defines a floating point number.

Actually, this should work:

``````if(millis() - timer1 >= 60000)
``````

Ah, I got it now. For some reason, I thought long variables were floating point. They're not.

You are dealing with integer (not decimal) math so use (in this case) 60000ul

It's not mandatory. The 'ul', I mean.

Yes.

Suggest adding the ul for a reminder to old memory cells.

Sure, I generally capitalize it, "60000UL" to make it more obvious.

Yes.

Even better for old eyes.

I tested different ways, it seems there's a huge difference between

``millis()-timer1>=60000``

and

``millis()-timer1>=1000*60``

and

``millis()-timer1>=1000*60UL``

well, the first and last are the same,
the middle one doesn't work

Sometimes they are used for scaled fixed point. But that is another discussion.

...for the reason I gave you, it's using integer math by default

1 Like