HC-05 sends endless println messages and does not respond

I guess the previous upload was canceled somehow so I’m re-uploading this.

Hi. I’m making an RC car for my school project, and this is my first time writing codes.

I’m using Arduino Uno, L293D motor shield, and HC-05 for this project.

I was following the guide from https://blog.miguelgrinberg.com/post/building-an-arduino-robot-part-ii-programming-the-arduino, and I couldn’t properly communicate with the Arduino through bluetooth connection.

Everything works fine with the sketch below:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(6,7);

void setup()
{
   BTSerial.begin(9600);
}

void loop()
{
   if (BTSerial.available() > 0) {
       char ch = BTSerial.read();
       BTSerial.print("Received: ");
       BTSerial.println(ch);
       if (ch == 'a') {
           digitalWrite(9, HIGH);
       }
       else {
           digitalWrite(9, LOW);
       }
   }
}

I can turn the LED on and off with my android phone with this sketch, but if I include definitions for motors in the sketch, the arduino continuously sends println messages to my phone and it does not respond to my command.

#include <SoftwareSerial.h>
#include <AFMotor.h>

SoftwareSerial BTSerial(6,7);
AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

void setup()
{
   BTSerial.begin(9600);
}

void loop()
{
   if (BTSerial.available() > 0) {
       char ch = BTSerial.read();
       BTSerial.print("Received: ");
       BTSerial.println(ch);
       if (ch == '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);
       }
       if (ch == '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);
       if (ch == 'E') {
           digitalWrite(9, HIGH);
       }
       if (ch == 'F') {
           digitalWrite(9, LOW);
       }
       }
   }
}

Why is this happening and how do I fix it? Please help.

(For attachments) I did not connect to the motors; they work fine with serial connection to my laptop with modified sketch, but apparently it does not work with bluetooth commands.

Here we go again with a poster not reading and following the advice given in read this before posting a programming question about how to post code

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;
      }
   }
  }
}

GolamMostafa: When I have changed your if-else structure into switch-case structure

On its own changing between IF-ELSE and SWITCH-CASE could not possibly make any difference to a program.

...R

On its own changing between IF-ELSE and SWITCH-CASE could not possibly make any difference to a program.

Sometimes, the erratic behaviors are changed, which are called side affects.

I think that the motorshield and AFmotor.h are using the bluetooth Serial pins for one of the motors.

Try putting the BT software serial pins on two of the analog pins. They can be used as digital pins, and they are not used by AFMotor.h.

GolamMostafa: Sometimes, the erratic behaviors are changed, which are called side affects.

That is not helpful for a newbie. You need to point out what behaviour needed to be changed.

If you have not considered whether something needed to be changed and made a conscious effort to change it then you have what I call the scatter-gun approach to debugging which, IMHO, is a complete waste of time and is a very poor example to put before a newbie.

...R

I tried the switch-case structure as GolamMostafa suggested, but there was not much progress except the arduino stopped sending tons of "Received: " messages.

Instead, I switched the BT pins to the analog pins as cattledog suggested, and I included “else” part, and it’s working.

By working I mean the "Received: " messages disappeared and I now can control the LED via bluetooth. But motors are not moving. :frowning:

Any idea why they are not moving?

#include <SoftwareSerial.h>
#include <AFMotor.h>
SoftwareSerial BTSerial(14,15);
AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

void setup()
{
    BTSerial.begin(9600);
}

void loop()
{
    if (BTSerial.available() > 0) {
        char ch = BTSerial.read();
        BTSerial.print("Received: ");
        BTSerial.println(ch);
        if (ch == '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);
        }
        if (ch == '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);
        }
        if (ch == 'E') {
            digitalWrite(16, HIGH);
        }
        if (ch == 'F') {
            digitalWrite(16, LOW);
        }
        else {
          
        }
    }
}

exningen: Why is this happening and how do I fix it? Please help.

The } that is on line 46 should be between lines 39 and 40. If you use the AutoFormat tool to indent your code that sort of problem should be easier to see.

...R