Bluetooth between Arduino boards does not work

Hello ,I'm new to Arduino and I'm attempting to build a RC vehicle using two UNO boards and two Bluetooth HC-05 modules configured as master and slave.
Two joysticks on the master side control the car's throttling and steering.

On the slave side, I'm using a servo motor to steer. I also use two motors for forward and reverse motion, which are controlled by a l911 h bridge motor driver.

Can someone look over the code? It compiles correctly but does not provide the desired output.

Even though the code is uploaded, the motor never turns on when moving the joystick from the master side, and the servo buzzes but never moves.

CODE FOR MASTER

int xAxis, yAxis,xAxis1, yAxis1;

void setup() {
  Serial.begin(38400);
}

void loop() {
  xAxis = analogRead(A0);
  yAxis = analogRead(A1);
  xAxis1 = analogRead(A2);
  yAxis1 = analogRead(A3);
  Serial.write(xAxis/4); 
  Serial.write(yAxis/4);
  Serial.write(xAxis1/4); 
  Serial.write(yAxis1/4);
  Serial.print(xAxis/4); 
  Serial.print(yAxis/4);
  Serial.print(xAxis1/4); 
  Serial.print(yAxis1/4);
  delay(1000);
}

CODE FOR SLAVE

#include <Servo.h>
Servo myservo;

  int motor_left[] = {3, 5};
  int motor_right[] = {9, 6};

  int xAxis, yAxis, xAxis1, yAxis1;
  unsigned int  x = 0;
  unsigned int  y = 0;

  int motorSpeed = 0;

void setup()
{
  int i;
  for (i = 0; i < 2; i++) {
    pinMode(motor_left[i], OUTPUT);
    pinMode(motor_right[i], OUTPUT);


    Serial.begin(38400);

    myservo.attach(10);
  }
}
  void loop()
  {
    x = 510 / 4;
    y = 510 / 4;

    while (Serial.available() >= 2)
    {
      x = Serial.read();
      delay(1000);
      y = Serial.read();
    Serial.print(x);
    Serial.print(y);
    }
    delay(1000);
    xAxis = x * 4;
    yAxis = y * 4;
    xAxis1 = x * 4;
    yAxis1 = y * 4;

    if (yAxis < 470) {
      motorSpeed = map(yAxis, 470, 0, 0, 255);
      digitalWrite(motor_left[0], motorSpeed);
      digitalWrite(motor_left[1], LOW);
      digitalWrite(motor_right[0], motorSpeed);
      digitalWrite(motor_right[1], LOW);
      Serial.print(motorSpeed);
    }

    else if (yAxis > 550)
    {
      motorSpeed = map(yAxis, 550, 1023, 0, 255);
      digitalWrite(motor_left[0], LOW);
      digitalWrite(motor_left[1], motorSpeed);
      digitalWrite(motor_right[0], LOW);
      digitalWrite(motor_right[1], motorSpeed);
      Serial.print(motorSpeed);
    }
  
    else if (xAxis1 != 0)
   {
    int val = map(xAxis, 0, 1023, 0, 179);
    int servoAng = val;
    myservo.write(servoAng);
    Serial.print(servoAng);
    }
  }

Could you edit the title of this topic ?
For example: "No communication between two Bluetooth modules", or "Bluetooth between Arduino boards does not work" or something like that.

Take a step back.
Which Arduino boards do you use ?
If you use Arduino Uno / Nano boards, then you have no spare serial port for the Bluetooth. You need the normal Serial port to send messages to the Serial Monitor.

You can use SoftwareSerial to create another Serial port.
Then send for example "Hello" every second via Bluetooth.

Disconnect the servo motors from the Slave and show the received messaged on the Serial Monitor.

If that works, then you can start another test with only the Slave and read commands from the Serial Monitor to control the servo motors.
If that works, you could try to control the Servo motors with the Master.

A beginner tries to rush to the end-result and if it does not work, then try to fix it. There is not a single advanced software engineer that does that. Do a project step by step. Only if you already have done something in the past, then you can go faster through the steps.

Creating a software serial port (up to 9600 baud) on the receiver and attaching a USB-Serial adapter whilst bench testing will also allow you to see what is being received.

The hardware serial port on your UNO is connected to the on-board USB-Serial interface. Trying to repurpose that serial port for your Bluetooth module may cause a conflict with your USB-Serial chip.

EDIT: Probably ignore this as @Koepel summed it up much better!

The serial input basics tutorial may have information that you can use on reliably sending, receiving and parsing serial data.

+1 on using a software serial port for the HC05 and saving the Serial port for program upload, monitoring program output and debugging.

From looking at the code, you seem to be making a assumption that each and every byte sent over Bluetooth will be received completly error free and the data will always arrive in the correct order.

Completly error free communications seems unlikly for RF based systems, at the very least the receiver would need to know when exactly a valid set of data has arrived and just started.

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