title: Reading serial port faster
body: <p>I have a computer software that sends RGB color codes to Arduino using USB. It works fine when they are sent slowly but when tens of them are sent every second it freaks out. What I think happens is that the Arduino serial buffer fills out so quickly that the processor can't handle it the way I'm reading it.</p>
  
  <pre><code>#define INPUT_SIZE 11
  
  void loop() {
    if(Serial.available()) {
      char input[INPUT_SIZE + 1];
      byte size = Serial.readBytes(input, INPUT_SIZE);
      input[size] = 0;
  
      int channelNumber = 0;
  
      char* channel = strtok(input, " ");
      while(channel != 0) {
        color[channelNumber] = atoi(channel);
  
        channel = strtok(0, " ");
        channelNumber++;
      }
  
      setColor(color);
    }
  }
  </code></pre>
  
  <p>For example the computer might send <code>255 0 123</code> where the numbers are separated by space. This works fine when the sending interval is slow enough or the buffer is always filled with only one color code, for example <code>255 255 255</code> which is 11 bytes (<code>INPUT_SIZE</code>). However if a color code is not 11 bytes long and a second code is sent immediately, the code still reads 11 bytes from the serial buffer and starts combining the colors and messes them up. How do I avoid this but keep it as efficient as possible?</p>