I'm facing a weird issue and I can't really understand what I'm doing wrong... There must be something!
I'll explain it shortly... I'm developing for the Arduino platform (As far as I know it's heavily based on C++) and in a section of my actual code I have a while loop...
So where's the issue?
If I set a variable (a flag in my case) inside the while loop it gets resetted every time, this code is in a function... (may be useful)
Here it is:
void waitResponse(char* text) {
static bool flag = false;
count = 0;
digitalWrite(GREEN_PIN, HIGH); //Turn OFF Green LED
while (!espSerial.available()) { //Wait until we get some data in...
Serial.println("No data");
notBlockingBlink();
}
while (!flag) { //We got sth
Serial.print("Flag value 1: ");
Serial.println(flag);
while (espSerial.available()) {
Serial.println("In");
if (search(text)) {
flag = true;
Serial.println("Found");
Serial.print("Flag value 2: ");
Serial.println(flag);
break;
} else {
notBlockingBlink();
}
}
Serial.print("Flag value 3: ");
Serial.println(flag);
notBlockingBlink();
}
}
I've tried to debug my issue...
After entering the if block where there is "Found", flag gets set to true, indeed "Flag value 2:" is true just like "Flag value 3:", but at the next iteration "Flag value 1:" is actually false...
What's wrong? Please enlighten me! Thanks
notBlockingBlink();do?whilestatement's condition is!flag(while (!flag), it is impossible forflagto be true at the next statement. So the "next iteration" must be later, afterflaghas somehow gotten changed. (Although I don't know how that happens, sinceflagis static and local to the function.)