Bluetooth RC Car Suddenly Gets Out of Control (HC 05)

I am using HC05. I drive the car for 1-2 minutes. Then suddenly one of the motors goes into an infinite loop and continues to run. Unable to connect with the mobile app and I have to chase the car. I have to remove the lipo and reset it. Is there a problem with the codes I use? Thanks in advance for your help.

#include <SoftwareSerial.h>
 
// Clockwise and counter-clockwise definitions
#define CW  0
#define CCW 1
 
// Motor definition
#define MOT_A 0
#define MOT_B 1
 
const byte PWMA = 3;
const byte PWMB = 11;
const byte DIRA = 12;
const byte DIRB = 13;

SoftwareSerial mySerial(2,3); // RX,TX


void setup()
{
  pinMode(8, OUTPUT);  // Ön farlar
  pinMode(7, OUTPUT);  // Arka farlar
    // All pins should be setup as outputs:
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  
  // Initialize all pins as low:
  digitalWrite(PWMA, LOW);
  digitalWrite(PWMB, LOW);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
 
  mySerial.begin(9600);
}
void loop()
{
if(mySerial.available()> 0)
{
char veri = mySerial.read() ;// gelen veriyi okuyoruz
  
  if(veri=='F')
  {
    moveMotor(MOT_A, CCW, 70); 
    moveMotor(MOT_A, CCW, 80);  
    moveMotor(MOT_A, CCW, 90);   
    moveMotor(MOT_A, CCW, 100);    
    moveMotor(MOT_A, CCW, 110);    
    moveMotor(MOT_A, CCW, 120);   
    moveMotor(MOT_A, CCW, 130);
    moveMotor(MOT_A, CCW, 135); 
    moveMotor(MOT_A, CCW, 140);
    moveMotor(MOT_A, CCW, 145); 
    moveMotor(MOT_A, CCW, 150);
    }
    else if(veri=='B')
  {
     moveMotor(MOT_A, CW, 80);  
    moveMotor(MOT_A, CW, 90);   
    moveMotor(MOT_A, CW, 100);    
    moveMotor(MOT_A, CW, 110);    
    moveMotor(MOT_A, CW, 120);   
    moveMotor(MOT_A, CW, 130);
    moveMotor(MOT_A, CW, 135); 
    moveMotor(MOT_A, CW, 140);
    moveMotor(MOT_A, CW, 145);
    moveMotor(MOT_A, CW, 150);
    }
    else if(veri=='R')
    {
     moveMotorDireksiyon(MOT_B, CW,80);
     moveMotorDireksiyon(MOT_B, CW,90);
     moveMotorDireksiyon(MOT_B, CW,100);
     moveMotorDireksiyon(MOT_B, CW,110);
     moveMotorDireksiyon(MOT_B, CW,120);
     moveMotorDireksiyon(MOT_B, CW,130);
     moveMotorDireksiyon(MOT_B, CW,135);
     moveMotorDireksiyon(MOT_B, CW,140);
     moveMotorDireksiyon(MOT_B, CW,145);
     moveMotorDireksiyon(MOT_B, CW,150);     
      }
       else if(veri=='L')
    {
      moveMotorDireksiyon(MOT_B, CCW,80);
      moveMotorDireksiyon(MOT_B, CCW,90);
      moveMotorDireksiyon(MOT_B, CCW,100);
      moveMotorDireksiyon(MOT_B, CCW,110);
      moveMotorDireksiyon(MOT_B, CCW,120);
      moveMotorDireksiyon(MOT_B, CCW,130);
      moveMotorDireksiyon(MOT_B, CCW,135);
      moveMotorDireksiyon(MOT_B, CCW,140);
      moveMotorDireksiyon(MOT_B, CCW,145);
      moveMotorDireksiyon(MOT_B, CCW,150);      
      }
    else
    {      
     stopMotor(MOT_A);
     stopMotor(MOT_B);
      }
}
}
void driveArdumoto(byte motor, byte dir, byte spd)
{
  if (motor == MOT_A)
  {
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
  }
  else if (motor == MOT_B)
  {
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
  }  
}
// moveMotor drives motor
void moveMotor(byte motor, byte dir, byte spd)
{
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
}
void moveMotorDireksiyon(byte motor, byte dir, byte spd)
{
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
} 
//stop motor
void stopMotor(byte motor)
{
  driveArdumoto(motor, 0, 0);
}

Perhaps there is a pin conflict here.

const byte PWMA = 3;
SoftwareSerial mySerial(2,3); // RX,TX

I made a lot of edits to the code. First, SoftwareSerial mySerial(0,1); I used as. The problem persisted even though it was 0.1. It got out of control pretty early on my last change. I'm fixing this right now. But this problem will continue. Thanks anyway.

Do not put software serial on the hardware serial pins.

Could it be any empty pin? Or do you have a pin number you recommend? Since I am using Ardumoto, pins 3,11,12,13 are used.

Yes, any unused digital pin, and most, but not all analog pins.

Thanks for all. I will try.

After plugging in the 5v pin this problem was solved. I have read in a few forums that hc 05 should be plugged into 3.3v pin. That's why I did it wrong.

Glad to hear that you have sorted that, but putting software serial on the hardware serial pins was equally sinful, and more likely to be the real problem.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.