I have done some experiments with your program codes with a HC-05 and an UNO; I could not arrive at any concrete conclusion. When I have changed your if-else structure into switch-case structure and moved the SRX, STX pins to 10, 11, I have seen some improvements. Given below the program codes; you may play around.
#include <SoftwareSerial.h>
#include <AFMotor.h>
SoftwareSerial BTSerial(10, 11); //SRX, STX of UNO
AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);
//char ch = ' ';
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// BTSerial.flush();
}
void loop()
{
if (BTSerial.available() > 0)
{
char ch = BTSerial.read();
// BTSerial.print("Received: ");
BTSerial.write(ch);
Serial.println(ch);
//while(1);
switch (ch)
{
case 'C':
{
Motor1.setSpeed(255);
Motor1.run(BACKWARD);
Motor2.setSpeed(255);
Motor2.run(BACKWARD);
Motor3.setSpeed(255);
Motor3.run(BACKWARD);
Motor4.setSpeed(255);
Motor4.run(BACKWARD);
break;
}
case 'D':
{
Motor1.setSpeed(0);
Motor1.run(BRAKE);
Motor2.setSpeed(0);
Motor2.run(BRAKE);
Motor3.setSpeed(0);
Motor3.run(BRAKE);
Motor4.setSpeed(0);
Motor4.run(BRAKE);
break;
}
case 'E':
{
digitalWrite(13, HIGH);
break;
}
case 'F':
{
digitalWrite(13, LOW);
break;
}
}
}
}