Bluetooth arduino car problem

Hello everyone

I'm making my own bluetooth rc-car. But my code is not really working well. I can't steer left or right. I can only go forward and backwards but after 1 min the arduino freezes our something and keeps repeating the last input. I don't understand why it's not working because I looked at other codes and they are almost the same and they work perfect. I would appreciate it if someone could help me with this. thanks in advance!

code:

// right motors
int Input_MC_1A = 6;
int Input_MC_1B = 9;

//left motors
int Input_MC_2A = 10;
int Input_MC_2B = 11;


char commando;

void setup() {
  pinMode(Input_MC_1A, OUTPUT);
  pinMode(Input_MC_1B, OUTPUT);
  pinMode(Input_MC_2A, OUTPUT);
  pinMode(Input_MC_2B, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);

Serial.begin(9600);


 pinMode(7, OUTPUT);
 digitalWrite(7, LOW);
}

void loop() {
  if(Serial.available()>0)
{
  
  commando = Serial.read();
  switch(commando)
  {
    case 'F':
    forward();
    break;
    case 'B':
    backward();
    break;
    case 'L':
    left();
    break;
    case 'R':
    right();
    break;
    case 'a': digitalWrite(7, HIGH);
    break; 
    case 'b': digitalWrite(7, LOW);
    break;
    default: break;
  }
}
}
void forward()
{
  analogWrite(3, 100);
  analogWrite(5, 100);
  
  //RightMotor 
  digitalWrite(Input_MC_1A, HIGH); // clockwise
  digitalWrite(Input_MC_1B, LOW); // against the clock
  //LeftMotor
  digitalWrite(Input_MC_2A, HIGH); // clockwise
  digitalWrite(Input_MC_2B, LOW); //against the clock
}

void backward()
{
  analogWrite(3, 100);
  analogWrite(5, 100);
  //RightMotor 
  digitalWrite(Input_MC_1A, LOW); // clockwise
  digitalWrite(Input_MC_1B, HIGH); //against the clock
  //LeftMotor
  digitalWrite(Input_MC_2A, LOW); // clockwise
  digitalWrite(Input_MC_2B, HIGH); // against the clock
}
void left()
{
  analogWrite(3, 100);
  analogWrite(5, 100);
  //RightMotor
  digitalWrite(Input_MC_1A, HIGH); // clockwise
  digitalWrite(Input_MC_1B, LOW); // against the clock
  //LeftMotor
  digitalWrite(Input_MC_2A, LOW); // clockwise
  digitalWrite(Input_MC_2B, HIGH); // against the clock
}
void right()
{
  analogWrite(3, 100);
  analogWrite(5, 100);
  //RightMotor 
  digitalWrite(Input_MC_1A, LOW); // clockwise
  digitalWrite(Input_MC_1B, HIGH); //against the clock
  //LeftMotor
  digitalWrite(Input_MC_2A, HIGH); // clockwise
  digitalWrite(Input_MC_2B, LOW); //against the clock
}
const byte Input_MC_1A = 6;// right motors
const byte Input_MC_1B = 9;
const byte Input_MC_2A = 10;//left motors
const byte Input_MC_2B = 11;

void setup() {
  pinMode(Input_MC_1A, OUTPUT);
  pinMode(Input_MC_1B, OUTPUT);
  pinMode(Input_MC_2A, OUTPUT);
  pinMode(Input_MC_2B, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
}

void loop() {
  if (Serial.available() > 0) {
   char commando = Serial.read();
    switch (commando) {
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'F':
        forward();
        break;
      case 'B':
        backward();
        break;
      case 'a':
        digitalWrite(7, HIGH);
        break;
      case 'b':
        digitalWrite(7, LOW);
        break;
      default: break;
    }
  }
}
void forward() {
  analogWrite(3, 100);
  analogWrite(5, 100);
  digitalWrite(Input_MC_1A, HIGH); 
  digitalWrite(Input_MC_1B, LOW);
  digitalWrite(Input_MC_2A, HIGH);
  digitalWrite(Input_MC_2B, LOW);
}
void backward() {
  analogWrite(3, 100);
  analogWrite(5, 100);
  digitalWrite(Input_MC_1A, LOW);
  digitalWrite(Input_MC_1B, HIGH);
  digitalWrite(Input_MC_2A, LOW);
  digitalWrite(Input_MC_2B, HIGH);
}
void left() {
  analogWrite(3, 100);
  analogWrite(5, 100);
  digitalWrite(Input_MC_1A, HIGH);
  digitalWrite(Input_MC_1B, LOW);
  digitalWrite(Input_MC_2A, LOW);
  digitalWrite(Input_MC_2B, HIGH);
}
void right() {
  analogWrite(3, 100);
  analogWrite(5, 100);
  digitalWrite(Input_MC_1A, LOW);
  digitalWrite(Input_MC_1B, HIGH);
  digitalWrite(Input_MC_2A, HIGH);
  digitalWrite(Input_MC_2B, LOW);
}

Thank you for answering my left and right works now but when I click 1 time forward, backward, left or right on the controller app it executes but then it blocks and loses connection with my HC-05. Do you know why this is happening?

Your code works properly with serial input from the Serial monitor and with an HC05 using a Serial Bluetooth Terminal app on a phone.

What controller app are you using? My testing indicates the issue lies there.

I did not use any actual motors, but there maybe power supply issues with your hardware.

I'am using the 'Bluetooth RC Controller' app and the 'Arduino bluetooth controller' app

When I use the Arduino Bluetooth controller app in Terminal mode the code does not lock up or lose connection with repeated commands.

A loss of connection would indicate that the HC05 is losing power during some brownout event with the Arduino when the motors run.

How is everything powered and wired?

Here is my wiring diagram. I know it's not the prettiest because I made it in paint. the 4 input needs to be 5

As previously stated, the only way the phone can disconnect from the HC05 is if the module Vcc drops too low when the motors run.

You can not power an Arduino with 5v on the Vin pin. That pin is before the voltage regulator and requires at least 7 volts. Using the 5v output from the LN298 to power through the Vin is not correct. Better would be to take the 9v of the battery directly to Vin.

Explain more about the 9v battery. What kind is it and what current can it supply.

With an adequate power supply to the Arduino, the 5v pin should work fine as Vcc for the HC05 module.

My HC-05 no longer loses the connection because I connected my arduino directly to the 9v battery. Forward and backward now works perfectly but if I want to go left and right my motors need more power to turn but if I give my motors more power by increasing my analog power to 200 it still loses the connection. The battery I use is a Panasonic Pro Power 9v battery. Do you recommend another battery? Thank you so much for all the help

Anything but the PP3 9v battery.

What are the specifications on the motors? Unless you know what the current draw of the motors is you are working blind.

How long does the car need to run before recharging the batteries?

Can you change the LN298 to a more modern motor controller which less of a voltage drop across it? You could use a 7.2v NiMh battery pack instead of 9v.

I am not very knowledgeable about battery selection, but I would encourage you to look at Robot Shop and Pololu websites for inspiration. You can find better quad drivers there as well.
https://www.robotshop.com/community/tutorials/show/basics-how-do-i-choose-a-battery

If you do a forum search for battery power for rc car you might find references in previous threads.

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