Problem with time based if condition

I have this code:

#include <Arduino.h>

unsigned long lastRun	= 0;
unsigned long now	= 0;

const int b = 10;

void setup(){
	Serial.begin(9600);
}

void loop(){
        now = millis();
	//Run every UPDATE_EVERY value (in minutes)
	if( now - lastRun > ((b*60)*1000) || lastRun == 0 ){
	
                Serial.print("OUT=");Serial.println(now);
                 Serial.print("millis-lastRun=");Serial.println(now - lastRun); 
                 Serial.print("UPDATE=");Serial.println((b*60)*1000);
                 lastRun = millis();
                 Serial.print("IN=");Serial.println(lastRun);
	}
}

That return this:

OUT=0
millis-lastRun=0
UPDATE=10176
IN=1
OUT=10178
millis-lastRun=10177
UPDATE=10176
IN=10179

The strange thing is (b*60)*1000 result value is 10176 instead of 600000

What's wrong?

b is an int - therefore it cannot store more than 32767. As it's the only variable in the calculation at that point, the result is also an int.

Change it to an unsigned long.

majenko:
b is an int - therefore it cannot store more than 32767. As it's the only variable in the calculation at that point, the result is also an int.

Change it to an unsigned long.

Don't have to change the variable 'b' to get the right result. The "B*60" happens first so changing 60 to 60L or 60UL will cause all the math to be done with 'long' or 'unsigned long' values.

600,000 in hex is 0x927C0. A 16-bit integer can only store the last 4 digits: 0x27C0 which in decimal is 10,176. Using long (32-bit) integers allows for all 5 digits to be kept.

johnwasser:

majenko:
b is an int - therefore it cannot store more than 32767. As it's the only variable in the calculation at that point, the result is also an int.

Change it to an unsigned long.

Don't have to change the variable 'b' to get the right result. The "B*60" happens first so changing 60 to 60L or 60UL will cause all the math to be done with 'long' or 'unsigned long' values.

600,000 in hex is 0x927C0. A 16-bit integer can only store the last 4 digits: 0x27C0 which in decimal is 10,176. Using long (32-bit) integers allows for all 5 digits to be kept.

johnwasser:

majenko:
b is an int - therefore it cannot store more than 32767. As it's the only variable in the calculation at that point, the result is also an int.

Change it to an unsigned long.

Don't have to change the variable 'b' to get the right result. The "B*60" happens first so changing 60 to 60L or 60UL will cause all the math to be done with 'long' or 'unsigned long' values.

600,000 in hex is 0x927C0. A 16-bit integer can only store the last 4 digits: 0x27C0 which in decimal is 10,176. Using long (32-bit) integers allows for all 5 digits to be kept.

millis() returns an unsigned long. You should try and keep all your data types in a comparison the same so as to avoid any problems. unsigned takes no more space than signed, so it's a no-brainer.