Search code examples
while-looparduinoserial-portarduino-uno

arduino: loop until int gets value assigned to it



I am trying to get a value from serial input. The rest of the programm behaves different if the value changes. This value gest assigned once in the ```void setup()``` function.
My goal is to let the program run an infinite loop or something similar until a value (type int) is recived, and only then resume the flow.
I know about ```Serial.parseInt()``` and tried to implement it ``` while (variable == null) variable = Serial.parseInt() ``` but I got an error that 'null' was not declared in this scope.
Any suggestions?

Solution

  • Oke, i was kinda dumb. There is an easy way, simply check

    while (!variable) 
      variable = Serial.parseInt()
    

    This worked for me and should work for you as well