Clearing the incoming Serial buffer

The flush() method only clears the transmit buffer, it has no effect on the rx buffer. This code will clear the receive buffer:

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