How can I simplify this simple code? (Again...)

Hello,

  • you could use char arrays instead of the String class
  • Serial buffer's size is 64 bytes only (for Arduino Mega, for some other boards it's 16 I believe)
  • do not use delay() or blocking code such as your while loops, especially if the thing is a plane autopilot :slight_smile:

For non blocking Serial reads you can do something like that:

void loop()
{
  if ( Serial.available() > 0 ) 
  {
    static char input[64];
    static uint8_t i;
    char c = Serial.read();

    if ( c != '#' && i < 63)
      input[i++] = c;
    
    else
    {
      input[i] = '\0';
      i = 0;

      //process the input string here
      Serial.println( input );
    }
  }
}