Stopping Input buffering from skipping over Serial.read() commands WITHOUT setting the Serial Monitor to "No new Lines"

You seem to want to input a number. In that case you should probably use:
int minimum = Serial.parseInt();
or
float minimum = Serial.parseFloat();

The .parseInt()/.parseFloat() methods will throw away characters that aren't part of a number ("+-.0123456789") and then read a number until it finds something that doesn't look like a number or a full second goes by without any input.

Yes, if you have a Serial.parseInt() right after the previous Serial.parseInt() then it will generally read a zero value because the second number was not sent within the one-second timeout. One way to mitigate that problem:

  int intVar = Serial.parseInt();

  // Allow time for any line ending characters to arrive.
  delay(500); 

  // Clear the input buffer
  while (Serial.read() != -1) {}

  // Prompt for the next input
  Serial.println(F("Input a number here (decimals are welcome!): "));

  // Wait for the first character to arrive
  while (Serial.available() == 0) {}

  // Read the value
  float floatVar = Serial.parsefloat();

Sorry-- Didn't see this reply until now. I just got around to testing it and it worked a more impressive miracle than when Moses split the water in the bible. But before I do anything with it, Ima go on a spiritual adventure to figure out the specifics of pointers because they have now caught my limited attention span thanks to your code. Thanks again-- you're the best

Do you think the same problem will occur if I try to use parseInt(), then parsefloat()?

Yes. Definitely.

The pointer used to parse the double value is not really necessary, it’s meant to ensure you actually entered something that could be parsed as a number (at least the start of the string)

The wokwi simulator can do all the line endings settings.

For a few wokwi features, one must brave to edit the *.json file, that which informs the additional parts and wiring.

I could nutshell it, but I'm prone to, well never mind:

Here.

a7

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