serial communication stops

I have a project were i’m using two mega 2560s, two xbees series 2, a motor shield v2.3, and a bluetooth module hc-06 to control a rc boat from my android phone (bluetooth rc controller app for android). So the phone sends to one mega with a xbee and the hc-06, then to the boat with a mega, xbee, and motor shield. I hook up both megas and xbees to my computer to use the serial monitor to test if it is working correctly. When i start the connection between my phone and the hc-06, the serial monitor looks like this
s
s
s
s
s

s = stop for the app. If i hit the forward button on the app it looks like this

s
s
f
f
f
s
s

It seems the code i have written seems to work. But then the serial monitor will stop. The first couple of times it worked for a minute or so then stopped. I used a delay which made it work for a couple of mins but it still stopped. Now when i try it only lasts a couple of seconds. I’m very new to writting code so any help would be much appreciated. So i have a couple of questions.

Is it because the buffers are getting full that is stopping the serial communication?

Is there a way so that when a start up that i only see one “s” and only one other command when i push a button on the app like this?
s
f
r
l

Is there any suggestion on the code i wrote?

Here is the code for the mega that gets the info my phone and sends it to the boat.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SoftwareSerial.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *LeftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *RightMotor = AFMS.getMotor(3);

char command;          // Character used from app to move boat


void setup() 
{
  pinMode(30,INPUT);
  digitalWrite(30,LOW);
  pinMode(40,OUTPUT);
  digitalWrite(40,LOW);
  
  Serial.begin(115200);           // Connect to bluetooth
  

  Serial.begin(9600);             // Connect to other arduino

}

void loop() 
{
  if(Serial.available() > 0){
    command = Serial.read();
  }

  switch (command)
  {
    case 'F':{                    // Move boat forwards
    Serial.println(command);
    break;}

    case 'B':{                    // Move boat backwards
    Serial.println(command);
    break;}

    case 'S':{                    // Stop boat
    Serial.println(command);
    break;}

    case 'L':{                    // Move boat left
    Serial.println(command);
    break;}

    case 'R':{                    // Move boat right
    Serial.println(command);
    break;}

    case 'G':{                    // Move boat forward left
    Serial.println(command);
    break;}

    case 'I':{                    // Move boat forward right
    Serial.println(command);
    break;}

    case 'H':{                    // Move boat back left
    Serial.println(command);
    break;}

    case 'J':{                    // Move boat back right
    Serial.println(command);
    break;}

    case 'W':{                    // Turn on front lights
    digitalWrite(30, HIGH);
    Serial.println(command);       
    break;}

    case 'w':{                    // Turn off front lights
    Serial.println(command);
    break;}

    case 'U':{                    // Turn on back lights
    Serial.println(command);
    break;}

    case 'u':{                    // Turn of back lights
    Serial.println(command);
    break;}
  }

   delay(10);

}

Here is the code for the mega on the boat.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SoftwareSerial.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *LeftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *RightMotor = AFMS.getMotor(3);

char command;          // Character used from app to move boat
int headlights = 13;
int taillights = 12;
int i = 0;

void setup() 
{
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);
  
  Serial.begin(9600);             // Connect to other arduino

  AFMS.begin();

  LeftMotor->setSpeed(150);
  LeftMotor->run(FORWARD);
  LeftMotor->run(BACKWARD);
  LeftMotor->run(RELEASE);
  
  RightMotor->setSpeed(150);
  RightMotor->run(FORWARD);
  RightMotor->run(BACKWARD);
  RightMotor->run(RELEASE);

}

void loop() 
{
    if(Serial.available() > 0){
    command = Serial.read();
  }


    
    if (command == 'S'){               // Stop boat
    LeftMotor->run(RELEASE);
    RightMotor->run(RELEASE);
    Serial.println("S");
    }
    
    else if (command == 'F'){          // Move boat forwards
    LeftMotor->run(FORWARD);
    RightMotor->run(FORWARD);
    Serial.println("F");
    }

    else if (command == 'B'){          // Move boat backwards
    LeftMotor->run(BACKWARD);
    RightMotor->run(BACKWARD);
    Serial.println("B");
    }

    else if (command == 'L'){         // Move boat left
    LeftMotor->run(BACKWARD);
    RightMotor->run(FORWARD);
    Serial.println("L");
    }

    else if (command == 'R'){         // Move boat right
    LeftMotor->run(FORWARD);
    RightMotor->run(BACKWARD);
    Serial.println("R");
    }

    else if (command == 'G'){         // Move boat forward left
    LeftMotor->run(RELEASE);
    RightMotor->run(FORWARD);
    Serial.println("G");
    }

    else if (command == 'I'){        // Move boat forward right
    LeftMotor->run(FORWARD);
    RightMotor->run(RELEASE);
    Serial.println("I");
    }

    else if (command == 'H'){        // Move boat back left
    LeftMotor->run(RELEASE);
    RightMotor->run(BACKWARD);
    Serial.println("H");
    }

    else if (command == 'J'){        // Move boat back right
    LeftMotor->run(BACKWARD);
    RightMotor->run(RELEASE);
    Serial.println("J");
    }

    else if (command == 'W'){        // Turn on front lights
      if (i == '0'){
    digitalWrite(headlights,HIGH);
    Serial.println("Front Lights ON");
    i = '1';}
    }

    else if (command == 'w'){       // Turn off front lights
      if (i == '1'){
    digitalWrite(headlights,LOW);
    Serial.println("Front Lights OFF");
    i = '0';}
    }

    else if (command == 'U'){       // Turn on back lights
      if (i == '0'){
    digitalWrite(taillights,HIGH);
    Serial.println("Back Lights ON");
    i = '1';}
    }

    else if (command == 'u'){       // Turn off back lights
      if (i == '1'){
    digitalWrite(taillights,LOW);
    Serial.println("Back Lights OFF");
    i = '0';}
    }

    delay(10);

  }
  Serial.begin(115200);           // Connect to bluetooth
  Serial.begin(9600);             // Connect to other arduino

Um....

In addition to what @aarg pointed out …

It looks as if your Android is sending a stream of commands - maybe that is overwhelming the Mega?

Why not just send a single ‘S’ - or better yet send “” and use the 3rd example in Serial Input Basics to receive the data with much greater reliability.

If you can’t use “” than maybe you could use the 2nd example that looks for a linefeed to indicate the end of the command message.

…R