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
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().