title: Arduino clear buffer
body: <pre><code>#include &lt;stdio.h&gt;
  
  #define LED 13
  
  void setup() {
    pinMode(LED, OUTPUT);
    Serial.begin(9600);
  }
  
  void loop() {
    if (Serial.available() == 4) {
      char command[5];
  
      for (int i = 0; i &lt; 4; i++) command[i] = Serial.read();
      command[4] = '\0';
  
      Serial.println(command);
  
      if (strcmp(command, "AAAA") == 0) {
        digitalWrite(LED, HIGH);
        Serial.println("LED13 is ON");
      } else if (strcmp(command, "BBBB") == 0) {
        digitalWrite(LED, LOW);
        Serial.println("LED13 is OFF");
      }
    }
  }
  </code></pre>
  
  <p>I have that code, that reads 4 characters long strings. However, I need it to ignore any string that is not 4 characters long.</p>
  
  <p>So, imagine this input:</p>
  
  <pre><code>AAAA
  BBBB
  BBB
  AAAA
  </code></pre>
  
  <p>Right now, it reads {"AAAA", "BBBB", "BBBA"}.</p>
  
  <p>I need it to read {"AAAA", "BBBB", "AAAA"}.</p>
  
  <p>Any idea? Thank you.</p>