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);
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.