Slave HC-05 not receiving data from Master HC-05

Hi, I've been trying to create a remote controlled RC Car using Arduino Uno, Arduino Nano, 2 HC-05 Bluetooth Modules, a L298n motor controller, and a ps2 joystick.

When I try to control the car using the Master HC-05, the wheels stutter due to the data it receives: it alternates between receiving the value 0 (turn wheel) and 508 (stop wheel). (See pictures below).

I don't know what seems to be causing the issue. Let me know if more information is needed. Any help is greatly appreciated!

//MASTER
int xAxis, yAxis;
#define yPin 2
#define xPin 3
#define swPin 8

void setup() {
  Serial.begin(38400);
  pinMode(7, OUTPUT);
  pinMode(swPin, OUTPUT);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  
  digitalWrite(swPin, HIGH);
}

void loop() {
  if (Serial.available() > 1){
    digitalWrite(7, HIGH);
    }
  //xAxis = analogRead(xPin);
  yAxis = analogRead(yPin);
  //Serial.write(xAxis/4); Serial.print("x: "); Serial.println(xAxis/4);
  Serial.write(yAxis/4); Serial.print("y: "); Serial.println(yAxis/4);
  delay(20);
}
//SLAVE
#include <SoftwareSerial.h>

#define lm1 4
#define lm2 5
#define rm1 6
#define rm2 7

#define enA 2
#define enB 3

#define lm3 10
#define lm4 11
#define rm3 8
#define rm4 9

int x, y, motorSpeedA, motorSpeedB, tempMap, xAxis, yAxis;

void setup() {

  pinMode(lm1, OUTPUT);
  pinMode(lm2, OUTPUT);
  pinMode(rm1, OUTPUT);
  pinMode(rm2, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);

  pinMode(lm3, OUTPUT);
  pinMode(lm4, OUTPUT);
  pinMode(rm3, OUTPUT);
  pinMode(rm4, OUTPUT);
  
  Serial.begin(38400);
}

void loop() {
    x = 510/4;
    y = 510/4;
  
  while (Serial.available() > 1){ // Checks whether data is coming from the serial port
    x = Serial.read();
    delay(10);
    y = Serial.read();
  }
  delay(10);

  xAxis = x*4;
  yAxis = y*4;
  
  if (yAxis < 470){ //backward
    backward();
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
    }
  else if (yAxis > 550){ //forward
    forward();
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
    }
  else{
    idle();
    motorSpeedA = 0;
    motorSpeedB = 0;
    }
    
  /*if (xAxis < 470){ //right
    tempMap = map(xAxis, 470, 0, 0, 255);
    motorSpeedA += tempMap;
    motorSpeedB -= tempMap;
    bound();
    }
  else if (xAxis > 550){ //left
    tempMap = map(xAxis, 550, 1023, 0, 255);
    motorSpeedA -= tempMap;
    motorSpeedB += tempMap;
    bound();
    }*/
    
//Serial.print("X: "); Serial.println(xAxis);
Serial.print("Y: "); Serial.println(yAxis); 
  analogWrite(enA, motorSpeedA); //Serial.print("A: "); Serial.println(motorSpeedA);
  analogWrite(enB, motorSpeedB); //Serial.print("B: "); Serial.println(motorSpeedB);
}
  void bound(){
    if (motorSpeedA > 255) {motorSpeedA = 255;}
    else if (motorSpeedA < 0) {motorSpeedA = 0;}
    if (motorSpeedB > 255) {motorSpeedB = 255;}
    else if (motorSpeedB < 0) {motorSpeedB = 0;}
    }
  void forward(){
    digitalWrite(lm1, HIGH);
    digitalWrite(lm2, LOW);
    digitalWrite(rm1, HIGH);
    digitalWrite(rm2, LOW);
  }
  void backward(){
    digitalWrite(lm1, LOW);
    digitalWrite(lm2, HIGH);
    digitalWrite(rm1, LOW);
    digitalWrite(rm2, HIGH);
  }
  void idle(){
    digitalWrite(lm1, LOW);
    digitalWrite(lm2, LOW);
    digitalWrite(rm1, LOW);
    digitalWrite(rm2, LOW);
    }

When you use serial.print() of the sending message and of the receiving message, is there a difference?

What should happen to yAxis between 470 and 550? (and to xAxis when it is uncommented)

When you use serial.print() of the sending message and of the receiving message, is there a difference?

Do you mean the number that was sent and received? Yes, they are different. The Master sends 126 and the Slave receives 0.

Now that you ask that, I realized that the 508 means that no data was received, and the 0 is the received and transmitted data. At the beginning of the void loop(), x and y are set to be 510/4, which is stored as 127. If data is not received, x and y become 127*4, which is 508. What do you think is causing the 126 to become 0? And why is the Slave not receiving data sometimes?

What should happen to yAxis between 470 and 550? (and to xAxis when it is uncommented)

If between 470 and 550, the wheels should not turn. the xAxis controls the turning, which I commented out to just focus on forwards/backwards for now.

Looks like you are sending control data and debug output data to the same port? Hence the "~" in front of the 'y' (126 is the ASCII character value for the tilde'~' character). Indeed looking at your photos, you have the HC05 connected to the Rx0/Tx0 pins. That is going to confuse things for sure.

In the slave sketch, I notice you have included SofwareSerial, but not configured or used it. Instead, you are both reading control data and sending text output using the same port. You also appear to be sending only y data, but the receiving code seems to be trying to read both x and y.

Your loop starts with assigning variables x and y with the value 510/4. Because they are variables of type int, this will be rounded to 127. After the while (Serial.available()>1) loop, you then multiply them *4 which comes to 508. I suspect the while loop is randomly returning y=0 so you end up with a random jump between one value and the other.

I would suggest including and configuring SoftwareSerial in both sketches and connecting the HC05 module to both boards using your chosen pair of GPIO pins. The control data can then be sent between the two HC05 modules using the SoftwareSerial port and kept completely separate from the debug data being sent over USB using the default Serial port.

I would suggest including and configuring SoftwareSerial in both sketches

I tried this, and it removed the 0s. However, the slave doesn't seem to be receiving data because now all it receives is 508. Here's my code with the changes marked with asterisks:

//MASTER
#include <SoftwareSerial.h>

int xAxis, yAxis;
#define yPin 2
#define xPin 3
#define swPin 8

**#define RX 10**
**#define TX 11**

**SoftwareSerial mySerial(RX, TX);**
void setup() {
  Serial.begin(38400);
  pinMode(swPin, OUTPUT);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  **mySerial.begin(38400);**
}

void loop() {
  if (Serial.available() > 1){
    }
  //xAxis = analogRead(xPin);
  yAxis = analogRead(yPin);
  //Serial.write(xAxis/4); Serial.print("x: "); Serial.println(xAxis/4);
  **mySerial.write(yAxis/4);** Serial.print("y: "); Serial.println(yAxis/4);
  delay(20);
}
//SLAVE
#include <SoftwareSerial.h>

#define lm1 4
#define lm2 5
#define rm1 6
#define rm2 7

#define enA 2
#define enB 3

**#define RX 10**
**#define TX 11**

**SoftwareSerial mySerial(RX, TX);**
int x, y, motorSpeedA, motorSpeedB, tempMap, xAxis, yAxis;

void setup() {

  pinMode(lm1, OUTPUT);
  pinMode(lm2, OUTPUT);
  pinMode(rm1, OUTPUT);
  pinMode(rm2, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  
  Serial.begin(38400);
  **mySerial.begin(38400);**
}

void loop() {
    x = 510/4;
    y = 510/4;
  
  while (Serial.available() > 1){ // Checks whether data is coming from the serial port
    //x = Serial.read();
    delay(10);
    **y = mySerial.read();** Serial.print(y);
  }
  delay(10);

  xAxis = x*4;
  yAxis = y*4;
  
  if (yAxis < 470){ //backward
    backward();
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
    }
  else if (yAxis > 550){ //forward
    forward();
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
    }
  else{
    idle();
    motorSpeedA = 0;
    motorSpeedB = 0;
    }
    
  /*if (xAxis < 470){ //right
    tempMap = map(xAxis, 470, 0, 0, 255);
    motorSpeedA += tempMap;
    motorSpeedB -= tempMap;
    bound();
    }
  else if (xAxis > 550){ //left
    tempMap = map(xAxis, 550, 1023, 0, 255);
    motorSpeedA -= tempMap;
    motorSpeedB += tempMap;
    bound();
    }*/
    
//Serial.print("X: "); Serial.println(xAxis);
Serial.print("Y: "); Serial.println(yAxis); 
  analogWrite(enA, motorSpeedA); //Serial.print("A: "); Serial.println(motorSpeedA);
  analogWrite(enB, motorSpeedB); //Serial.print("B: "); Serial.println(motorSpeedB);
}
  void bound(){
    if (motorSpeedA > 255) {motorSpeedA = 255;}
    else if (motorSpeedA < 0) {motorSpeedA = 0;}
    if (motorSpeedB > 255) {motorSpeedB = 255;}
    else if (motorSpeedB < 0) {motorSpeedB = 0;}
    }
  void forward(){
    digitalWrite(lm1, HIGH);
    digitalWrite(lm2, LOW);
    digitalWrite(rm1, HIGH);
    digitalWrite(rm2, LOW);
  }
  void backward(){
    digitalWrite(lm1, LOW);
    digitalWrite(lm2, HIGH);
    digitalWrite(rm1, LOW);
    digitalWrite(rm2, HIGH);
  }
  void idle(){
    digitalWrite(lm1, LOW);
    digitalWrite(lm2, LOW);
    digitalWrite(rm1, LOW);
    digitalWrite(rm2, LOW);
    }

You are sure that the two HC05 modules are paired?

You are sure that the two HC05 modules are paired?

Yes, I configured them to pair with each other and their LEDs turn on twice every 2 seconds.

I knew there was a reason why the above code snippet was bothering me. It is checking the debug port which doesn't receive anything, instead of the one receiving the control data. I think it might need to be:

  while (mySerial.available() > 1){ // Checks whether data is coming from the swserial port
    //x = mySerial.read();
    delay(10);
    **y = mySerial.read();** Serial.print(y);
  }
  delay(10);

Incidentally, I presume that "mySerial.available() > 1" is meant to check for at least two bytes being received?

Incidentally, I presume that "mySerial.available() > 1" is meant to check for at least two bytes being received?

Oh wait, that is true... In this case its only sending one byte per cycle so it doesn't execute the code within the while condition. I changed the Slave to have " while (mySerial.available()>0) " but y still stays as 508...

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