Also asked in forum.arduino.cc

bool receive_command(char answer[]);
void serial_flush(void);

void setup() {
  // put your setup code here, to run once:
  for(byte i=0;i<3;i++){
if(receive_command("OK")) Serial.println("OK found");

void loop() {}

void serial_flush(void) {
  while (Serial.available()) Serial.read();

bool receive_command(char answer[]) {
 unsigned long _time = 0;
  const int max_time = 10000;
  bool flag = 0;

Serial.print("in Serial buffer=");
  Serial.println(" left in serial buffer");}

  while (Serial.available() < 2) {
if ((millis() - _time) > max_time) {
      Serial.println("waited too long nothing received");
  return flag;
  do {
    if (Serial.findUntil(answer, '\n')) return flag = 1;
  } while (Serial.available());

return flag;


via serial monitor with CR+LF line ending I`m sending this string: abcdefghijklmnopqrstuvwxyzOKabcdefghijklmnopqrstuvwxyz 26chars+OK+26chars+CR+LF=56 chars


in Serial buffer=0
OK found
in Serial buffer=1
b left in serial buffer
in Serial buffer=0
waited too long nothing received

after OK is found serial_flush() is executed but after second run there serial.available() shows there is something in serial buffer. right at that moment it was char 'b' but it also can show char 'd' and 'c' depending how long it will run.

why serial_flush() is not clearing the buffer?


  • your serial_flush() code does not wait for CRLF .... once the "OK found" is printed, the input buffer is probably receiving the b or c or d ... serial_flush() exits before e is received, since the buffer is empty ( e has not yet arrived) ................ imagine that you are the Arduino and you receive one character every hour ... what do you do after you have received the O K ?
    – jsotola
    Commented May 13, 2018 at 3:10

2 Answers 2


The processor is somewhat faster than serial input. If you really want to wait for nothing more to arrive you should probably add a short delay (although jsotola's suggestion of looking for a newline is sensible). Something like:

void serial_flush(void) {
 while (true)
   delay (20);  // give data a chance to arrive
   if (Serial.available ())
     // we received something, get all of it and discard it
     while (Serial.available ())
       Serial.read ();
     continue;  // stay in the main loop
    break;  // nothing arrived for 20 ms

the second parameter of findUntil(target, terminator) is of type char* not char

your Serial.findUntil(answer, '\n') doesn't work and you patched it with do while.

use Serial.findUntil(answer, "\n")

'a' is read by readUntil, then it returns because of wrong parameter. serial_flush sees no new character. and next you read 'b' from buffer. the sketch runs fast compared to Serial speed. it sees always no or one new character in buffer if the previous was removed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.