Question about Example2_Serial

Should the program wait longer than a moment to receive user input regarding how many cheeses? My program jumps very quickly from the question to the follow up response. I can quickly slip in a number (12 in my screenshot), but I figure it ought to wait until I press enter to move forward.

(If you haven’t already guessed, I’m a newbie working through the tutorial and examples… Any help provided is appreciated!)

It should wait but doesn’t (it’s not you), actually I’m a bit annoyed that I couldn’t find exactly why, although printing the offending serial input suggests there are stray line feed/carriage returns being sent by the Arduino Serial Monitor.

Here’s some code that works, with a serial timeout function which may be useful if you want to have the option of skipping past the data entry:

/* Author: Nathan Seidle
  Created: June 12th, 2019
  License: MIT. See SparkFun Arduino Apollo3 Project for more information

  This example demonstrates usage of:
  Serial
  Serial1

  This example works best on the BlackBoard Artemis and BlackBoard Artemis ATP that have
  the TX1 and RX1 pins exposed. See the advanced serial example for setting up a serial
  port on other pins and other carrier boards.
*/

bool waitForSerialIncTimeout(uint32_t timeout = 20000)
{
  uint32_t startTime;
  if(timeout == 0)
    startTime = 0xFFFFFFFF; //Highest possible value, effectively preventing timeout
  else
    startTime = millis();
    
  while(Serial.available()) Serial.read();
  delay(50);
  while (!Serial.available() && millis() < startTime+timeout)
  {
    delay(1000);
    Serial.print(".");
  }  //Wait for input   
  Serial.println();
  if(Serial.available())
    return true;
  else
  {
    Serial.println("Wait for input timed out");
    return false;
  }
}

void setup()
{
  Serial.begin(9600);
  Serial.println("Hello debug window!");

  Serial1.begin(115200);
  Serial1.println("This prints on TX1/RX1 pins");
}

void loop()
{
  Serial.println("Time for a menu:");
  Serial.println("a) Activate the cheese");
  Serial.println("x) Exit");
  Serial.println();

  waitForSerialIncTimeout(0);

  char incoming = Serial.read();

  if (incoming == 'a' || incoming == 'A')
  {
    Serial.println("How many cheeses to dispense?");
    waitForSerialIncTimeout(0);
    delay(100);
        
    unsigned int cheeseCount = Serial.parseInt(); //That's a lot of potential cheese

    //Print to second serial port
    for (unsigned int x = 0; x < cheeseCount; x++)
    {
      Serial1.println("Cheese!");
    }

    Serial.printf("Thank you. %d cheeses dispensed.\n\r", cheeseCount); //Did you see that?! We used printf!
  }
  else if (incoming == 'x' || incoming == 'X')
  {
    Serial.println("Exiting... Have a nice day.");
    while (1)
      ; //Freeze
  }
  else
  {
    Serial.print("Unknown choice: ");
    Serial.write(incoming);
    Serial.println();
  }
}