Maybe I`m misunderstanding the way Serial.flush works..
When you type something into the serial monitor and press return or send, a newline char is added.
So, whats in the buffer is whats been typed plus a newline char.
I would have expected to read x number of chars and discard the rest using the Serial.flush function, but thats not what happens, I read two chars, but using Serial.flush leaves the newline char in the buffer?
So, instead of using Serial.flush, I had to add an extra serial.read (wait_read function in my code)to discard the nl char that is added.
Here`s the test code I have now.
Type in two chars and press send or return.
char hrs[3];
char mins[3];
void setup()
{
Serial.begin(57600);
Serial.println("Serial Connected");
// Input time
Serial.println("Input time hh");
hrs[3] = 0; //null char
for (byte x= 0; x < 2; x++)
{
hrs[x] = wait_read();
Serial.println("serial read char is ");
Serial.print(hrs[x],DEC);
Serial.print("\t");
Serial.println(hrs[x]);
}
Serial.print("Hours are set to ");
Serial.println(hrs);
wait_read(); // needed for the nl char received - nl char is discarded
// Serial.flush(); // <<======= Not working as expected, leaves nl char in buffer?
Serial.println("Input time mm");
mins[3] = 0; //null char
for (byte x= 0; x < 2; x++)
{
mins[x] = wait_read();
Serial.println("serial read char is ");
Serial.print(mins[x],DEC);
Serial.print("\t");
Serial.println(mins[x]);
}
Serial.print("Minutes are set to ");
Serial.println(mins);
}
void loop()
{
// Do Stuff
}
// Wait for Serial Input
char wait_read ()
{
while(!Serial.available())
{ }
return Serial.read () ;
}