DFRobot Romeo BLE Mini - Code runs perfectly, but only once serial monitor has been opened

Link to the board: Romeo BLE mini - Small Control Board for Robot - Arduino Compatible - Bluetooth 4.0 - DFRobot

Hello everybody! I saw many others with this issue in a search of the forums, but the solutions tended to be in pieces of code that I don't have. I'm very new to Arduino, and modified some DFRobot supplied sample code to try to make a bluetooth-controllable robot, which can also actuate two separate motors. As the title says, the code functions perfectly, through the bluetooth connection, provided that you have opened the serial monitor first while connected to a computer. The serial monitor is not relevant for controlling the robot (although it can also be controlled through the serial monitor). Once disconnected, the robot functions properly through the app.

Oddly enough, there was a week during testing where it would connect to the phone (no laptop required) and function properly. But my luck there seems to have run out, and this bug has returned. When disconnected from the laptop, the phone can connect to the board still, and this is reflected by a blue light on the board itself. A small green LED blinks when buttons are pressed, to show me that it received data. But the motors do not turn!

For reference, I have tried this with a fresh 9v battery. Once running, the batteries function well and have more than enough power to move the robot. Code is attached,

//This code is adapted from DFRobot sample code, available at https://wiki.dfrobot.com/Romeo_BLE_mini_V2.0_SKU_DFR0351

//Standard PWM DC control
int E1 = 5;     //M1 Speed Control
int E2 = 6;     //M2 Speed Control
int M1 = 4;     //M1 Direction Control
int M2 = 7;     //M2 Direction Control
int E3 = 3;     //S1 Speed Control (Spinner)
int E4 = 11;     //S2 Speed Control (Spinner)
int M3 = 9;     //S1 Direction Control (Spinner)
int M4 = 13;     //S2 Direction Control (Spinner)


void stop(void)                    //Stop
{
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
}
void advance(char a,char b)          //Move forward
{
  analogWrite (E1,a);      //PWM Speed Control
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}
void back_off (char a,char b)          //Move backward
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
}
void turn_L (char a,char b)             //Turn Left
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}
void turn_R (char a,char b)             //Turn Right
{
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
}
void spinner_on(char c)                 //Turn Spinner On
{
  analogWrite (E3,c);      //PWM Speed Control
  digitalWrite(M3,HIGH);
  analogWrite (E4,c);
  digitalWrite(M4,HIGH);
}
void spinner_off(void)                  //Turn Spinner Off
{
  digitalWrite(E3,LOW);
  digitalWrite(E4,LOW);
}
void setup(void)
{
  int i;
  for(i=4;i<=7;i++)
    pinMode(i, OUTPUT);
  Serial.begin(9600);      //Set Baud Rate
  Serial.println("Run keyboard control");
}
void loop(void)
{
  if(Serial.available()){
    char val = Serial.read();
    if(val != -1)
   {
      switch(val)
      {
      case 'w'://Move Forward
        advance (100,100);
        Serial.println("Move Forward");
        delay(200);
        stop();
        break;
      case 's'://Move Backward
        back_off (100,100);
        Serial.println("Move Backward");
        delay(200);
        stop();
        break;
      case 'a'://Turn Left
        turn_L (100,100);
        Serial.println("Turn Left");
        delay(200);
        stop();
        break;
      case 'd'://Turn Right
        Serial.println("Turn Right");
        turn_R (100,100);
        delay(200);
        stop();
        break;
      case 'z'://Print something in serial terminal for testing
        Serial.println("Hello");
        break;
      case 'x':
        stop();
        break;
      case 'q'://Turn Spinner On
        spinner_on (100);
        Serial.println("Spinner on");
        break;
      case 'e'://Turn Spinner Off
        spinner_off();
        Serial.println("Spinner off");
        break;
      }
    }
    else stop();
  }
}

Thanks

looks like the posted code processes commands thru the "Serial" interface. I don't see any bluetooth code in the listing.

That is correct, the bluetooth app only forms a serial connection with the board. I could not get any other bluetooth app to connect to this board. But via the serial connection, it was functional previously without needing to connect to a laptop.

when i've used a bluetooth serial interface, i've need to include a bluetooth library and create a bluetooth interface

#include <BluetoothSerial.h>
BluetoothSerial     serialBT;

    serialBT.begin (name);

Awesome, that's certainly worth testing. Does it otherwise work as a normal serial connection?

yes, for example

    if (serialBT.available ())  {
        char c = serialBT.read ();
        ...
    }