While statement not working as expected

Hello, I have a program that runs in the void loop. I want to halt the loop whilst a GPIO input remains HIGH.

int inputValue = digitalRead(inputPin);
  digitalWrite(outputPin,inputValue);

  while (inputValue == LOW) {
    // Wait for the input pin to go low
  }

  delay(500); // Delay before the next loop runs.
}

The above does not stop the program flow running whilst the IO Port is HIGH. It just continues as if it's not checking.
I have tested the Input port state by outputing its state to another output port and that works fine. But the while statement fails. I am confused.

I'm confused. If inputValue is HIGH, the while condition is false and the loop won't be executed. If inputValue is LOW, the while loop will be entered and executed forever, as there's nothing inside the loop to change inputValue.

1 Like

The inputValue never changes inside the while-loop; so once you are in there, it will stay in there. You will need to read the input pin inside the while-loop and assign the value of the read to inputValue.

Note that your code is blocking code; so e.g. blinking a LED while waiting for the button is tricky.

1 Like

If you are waiting for the input value to go low (what the comment in the while loop said) you would do:

while (inputValue == HIGH) {}

That will never exit, unless inputValue changes within the while loop.

Sorry, try this:

int inputValue = digitalRead(inputPin);
  digitalWrite(outputPin,inputValue);

  while (inputValue == HIGH) {
    inputValue = digitalRead(inputPin);
  }

  delay(500); // Delay before the next loop runs.
}

Assuming HIGH is active for both the button and LED, the interaction of the delay() and while() would make the LED turn off 500ms after you release the button, but turn on 0-500ms after you press the button.

The original code would (randomly) hang in the infinite loop. Randomly activated depending how long you pushed the button and where in the delay(500) cycle you pushed it.

Both behaviors don't seem like behaviors anyone would intend to expect.

Oh dear, I feel so stupid :slight_smile: thanks for the external view on this. I've jigged things around and now its working.
Thank you!

Many times I use a line like Serial.print(".") to diagnose unexpected behavior of loops and ifs, etc... It can show you if things are happening as you expect, and it's easy duplicate with different letters to test other features.

const byte inputPin = 2;
const byte outputPin = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);
}

void loop(void) {
  int inputValue = digitalRead(inputPin);
  digitalWrite(outputPin, inputValue);

  while (inputValue == LOW) {
    // Wait for the input pin to go low
    Serial.print(".");
  }
  Serial.print("d");
  delay(500); // Delay before the next loop runs.
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.