Calculation or type error: 32000 is fine, 33000 gets crazy!

Hi all,

I get a weird error with the following code (I simplified it and took the GSM functions out of the way, and only put the relevant and related bits).

In the following code, in the SendSmsPeriodically() function, I have my int interval variable.
Well, with the value actually put in the following code, everything is fine.
But if I have a number above 32 (like 33 * 1000), then instead of 33000, Interval gets “4294934760” as a result…

Does anyone have an idea of where I messed up?
I tried by changing Interval type from int to unsigned long but no change :confused:

const int LED = 6;
bool LED_State = false;
bool LED_PreviousState = LED_State;
unsigned long millis_LED_Previous = 0;

const int Sec = 1000;
unsigned long millis_Previous = 0;
unsigned long millis_Actual;

unsigned long millis_SmsActual;
unsigned long millis_SmsPrevious = 0;

void setup(){
	pinMode(LED, OUTPUT);

	Serial.begin(9600);
	Serial.println("***************");
	Serial.println("***  Start  ***");
	Serial.println("***************");

	millis_Actual = millis();
	millis_Previous = millis_LED_Previous = millis_Actual;

	millis_SmsActual = millis();
	millis_SmsPrevious = millis_SmsActual;

}

void loop(){	
	SendSmsPeriodically();
	BlinkLED(500);

	delay(Sec/10);
}

void BlinkLED (int blinking_Lenght){
	millis_Actual = millis();
	if(millis_Actual - millis_LED_Previous >= blinking_Lenght){
		if(LED_State == LOW){
			LED_State = HIGH;		
			LED_PreviousState = LED_State;
		} else {
			LED_State = LOW;
		}
		digitalWrite(LED, LED_State);
		millis_LED_Previous = millis_Actual;
	}
}

void SendSmsPeriodically(){
	millis_SmsActual = millis();
	int Interval = 32 * 1000;
	Serial.println(Interval);
	if(millis_SmsActual - millis_SmsPrevious >= Interval){
		millis_SmsPrevious = millis_SmsActual;
		Serial.println(F("\n** SEND SMS"));
	}
}

Btw, I have no issue at all with the LED blinking part.

One other thing: does anyone knows why something like the following wouldn’t work

void loop(){
	int Sec = 1000;
	Function(1/2*Sec);
}

void Function (int Param){
	digitalRead(LED, HIGH);
	delay(Param);
	digitalRead(LED, LOW);
	delay(Param);
}

whereas the following would?

void loop(){
	int Sec = 1000;
	Function(Sec/2);
}

void Function (int Param){
	digitalRead(LED, HIGH);
	delay(Param);
	digitalRead(LED, LOW);
	delay(Param);
}

It’s actually how I realized the bug explained above, because at first I had my function parameters set int the loop() part, and nothing was working so I put Interval directly withing the SendSmsPeriodically() function instead of setting it from the loop().

Google "16 bit signed integer arithmetic"

int Interval = 32 * 1000;

AWOL:
Google "16 bit signed integer arithmetic"

... perhaps in association with this.

Thanks guys. Indeed I realize that I read this documentation too quickly yesterday as I read the part for the DUE... and thought then that int would be fine. But then why is the issue identical when I change to unsigned long?

Because the calculation is on the right hand side of the = sign, which is done in “int” arithmetic, unless you specify otherwise. 33UL * 1000UL

AWOL:
Because the calculation is on the right hand side of the = sign, which is done in "int" arithmetic, unless you specify otherwise. 33UL * 1000UL

Ok so basically I was doing this (http://forum.arduino.cc/index.php?topic=190752.msg1410259#msg1410259) right? Mixing types in the calc?

I had never heard of L/UL, thanks a lot for your help.

Two quick questions:
"Because the calculation is on the right hand side of the = sign"
...which is the only way to do a calc, isn't it? So my only option is to append the UL suffix right?

Also, do you think that the second part of my original post is linked to the problem you've pointed (the fact that Sec/2 will work contrary to 1*2/Sec, which I can't understand) ?

Regarding UL and other integer arithmetic, I got some good info here