How does Serial.flush() work ?

i'm trying to print a message and then wait for the user to input something before continuing, but after waiting properly the first time, the loop then just continues and ignores the flush(?) the next (and subsequent) time.

i get some kind of ctrl-character ?
ÿ

here's the code:

char choice = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
       Serial.println("");
    Serial.println("Select your choice (1-4)");
  // check Serial input
    Serial.flush(); // FLUSH FIRST
    while (!Serial.available())  {
      choice = Serial.read(); 
//    Serial.print("Debug: choice = ");
  //  Serial.println(choice);
    }
    // this after choice made
    Serial.print("Your Choice was #");
    Serial.println(choice);

    choice = 0;  // reset for the next round
delay(3000);
}

See HERE

I have been looking for a long time at this:

while (!Serial.available())  {
  choice = Serial.read(); 
//  Serial.print("Debug: choice = ");
//  Serial.println(choice);
}
// this after choice made
  Serial.print("Your Choice was #");
  Serial.println(choice);

I think that code is not okay. When nothing is available, the while loop is executed and a character is read. When something is available the data is not read, but the previous illegally read character is printed.

This could be better.

// Wait in while-loop until something is available.
while (!Serial.available()) ;

// One or more characters are available in buffer.
choice = Serial.read(); 
//  Serial.print("Debug: choice = ");
//  Serial.println(choice);

oh my, thanks so much Caltoa !

it's not just "could be better" - it's a matter of the correct way !

my control structure was totally wrong - your sentence put it so clearly;

When something is available the data is not read

looks like i still need to learn how to use the While loops !

Thanks again ! :smiley: