0

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

14
  • "I've tried to debug my issue..." Obviously not seriously enough. Commented Feb 19, 2015 at 21:11
  • What does notBlockingBlink(); do? Commented Feb 19, 2015 at 21:12
  • @NathanOliver It makes an LED blink, but I guess It's irrelevant... Commented Feb 19, 2015 at 21:13
  • @πάνταῥεῖ Why? I can't find the error I made...Help? Commented Feb 19, 2015 at 21:14
  • Since the outer while statement's condition is !flag (while (!flag), it is impossible for flag to be true at the next statement. So the "next iteration" must be later, after flag has somehow gotten changed. (Although I don't know how that happens, since flag is static and local to the function.) Commented Feb 19, 2015 at 21:14

1 Answer 1

1

Since your condition for the while loop is !flag, logically there is no possibility of having an iteration where flag is true. Therefore the behavior you believe should happen can not happen.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay... I thought so and so and I got really confused when it did... The variable seems to be resetting itself
Your function is called a second time. The variable is in fact local to the function, so it's not reset, it's just declared a second time (and initialised false).
I believe it should exit the loop... But it doesn't...That's the issue
@rainbowgoblin: Except that it's declared static.
You might want to update your question with what the problem actually was... Ignacio Vazquez-Abrams is completely right, the fact that it's static means that some line OTHER THAN THE DECLARATION must be making the value of flag false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.