Serial buffer?

I've run into the problem where Arduino skips an input. I did some research, and my best guess is that it's a serial buffer problem. I found Serial.flush();, tried it, and it didn't work. I also tried Serial.read(); in a while loop from Serial Input Basics. That also didn't work. The only thing that's worked is calling a method, but it seems a little overkill just to clear a serial buffer. This is method:
void serialFlush(){
while(Serial.available() > 0) {
char t = Serial.read();
}
}

This is the pertinent part of my code:
void setup() {
Serial.begin(9600);

pinMode(redPower, OUTPUT);
pinMode(yellowPower, OUTPUT);
pinMode(greenPower, OUTPUT);

Serial.println("How bright do you want the bulbs (1-255)?");
while (Serial.available() == 0) { }
voltValueHigh = Serial.parseInt();
serialFlush();

Serial.println("How dark do you want the bulbs (1-255)?");
while (Serial.available() == 0) { }
voltValueLow = Serial.parseInt();
}

Am I right about it being a serial buffer issue? Any thoughts or suggestions?

Welcome to the forum! Please Read How to use the forum before posting again.

Serial.flush() just waits until the send buffer is empty.

If you want to empty the read buffer without doing anything with it (don't know why you would).

while(Serial.available()){
  Serial.read();
}

acharyzay:
I also tried Serial.read(); in a while loop

That would is the Serial.flush() equivalent for input.

acharyzay:
The only thing that's worked is calling a method, but it seems a little overkill just to clear a serial buffer.

I think it's fine.

acharyzay:
Am I right about it being a serial buffer issue? Any thoughts or suggestions?

The problem is that Serial.parseInt() only reads the input up to the end of the integer representation of the incoming characters. That's fine if you have the line ending menu near the bottom right corner of Serial Monitor set to "No line ending". However, if you have any other line ending selected, what happens is the first Serial.parseInt() reads the int part but leaves the line ending in the input buffer. The next Serial.parseInt() then immediately finds that the input buffer contains characters that don't represent an int so it returns right away. Solutions would be to require that "No line ending be selected, or use your input flush function, or use some other method to dispose of the extra input.

if you end() the Serial connection and begin() it again, then it will reset buffers pointers.

But in practical terms and ideal world, your input parser would need to be able to detect the start (and end) of a proper input and thus adjust automatically to garbage at a given point in time in the incoming buffer - meaning you don't really need to reset the buffer. Meaningless or out of sync data will just get ignored by your parser.

septillion:
Welcome to the forum! Please Read How to use the forum before posting again.

Serial.flush() just waits until the send buffer is empty.

Apparently since v1.0 before it would clear the input buffer

septillion:
If you want to empty the read buffer without doing anything with it (don't know why you would).

while(Serial.available()){

Serial.read();
}

if you wnat to make really sure you should wait the minimum byte length if there is nothing available

while(Serial.available()){
  Serial.read();
  if (!Serial.available()) delayMicroseconds(bytelength);
}

Deva_Rishi:
Apparently since v1.0 before it would clear the input buffer

I believe it was BEFORE v1.0 (or up to and including)

...R

:smiley:

Thanks @septillion. I had not actually noticed the word "before" in Reply #4

...R