HC-05 Serial Comms - 2 way not working in SoftwareSerial

#include <SoftwareSerial.h>
#define tx 11
#define rx 10
SoftwareSerial bt(rx, tx); //RX, TX
void setup()
{
  Serial.begin(9600);
  bt.begin(9600);
}
void loop()
{
  if(bt.available()>0)
  {
    Serial.println(bt.readString());
  }
}

reciever

transmitter


#include <SoftwareSerial.h>
#define tx 11
#define rx 10
SoftwareSerial bt(rx,tx); //RX, TX
void setup() 
{
  Serial.begin(9600);
  bt.begin(9600);
}
void loop() 
{
  bt.write("12333");
}

so this is not working.....hardware serial is working but we can not use hardware serial in our project. wiring will send later. PLEASE Help

what microcontroller boards are you using ? how many serial ports do you require?
have a look at AltSoftSerial

Take a search engine of your choice and ask the WWW for 'bluetooth +arduino' to collect some data to be sorted out to get the needed information.

sender is uno
reciever is uno ( nano for testing )
code is from www.
hardware serial is working but we need software serial as we are creating a user product.

you say hardware serial is working - do you mean the USB Serial to the serial monitor?
the UNO does not have any other available hardware ports?
if you are building a user product using Bluetooth or BLE I recommend you move to a ESP32 which has onboard WiFi and Bluetooth

1 Like

tx pins as in 0 and 1 is working in comms like i have code to send 123 every second through hc05 and the reciever is picking it up and displaying it

but when using software serial library, the input is just garbage

from arduino-uno-pins-a-complete-practical-guide
Note that the Serial used by USB is the same as the one used with pins 0 and 1. So, if you want to connect another device to the RX/TX pins of your board, remember not to use the Serial through USB.

try AltSoftSerial

does not work for my needs, i need all digital pins as i am making a bluetooth controller robot.....and using motor driver shield v1 which uses almost all digital pins except 0,1,2,10,9

Your code is spitting out 5 bytes as fast as possible and on the receiving end you print what you get but the baud rate for printing out is the same as the reception so your buffers probably end up full and blocking code / missing data

Does the code work if you do this?

void loop() 
{
  bt.write("12333");
  delay(500);
}

Also change the Serial baud rate to 115200 on the receiver side

i will try but i did not understand what u meant

it is working but sometimes there are 2 texts instead of 1


also, it is very slow and i need data transmission instantly


right handside is transmission and left is recieving....notice the errors

please, please don't post pictures of text... Not only they are not readable nor usable directly for copy&paste but they use up lots of storage and internet bandwidth which contributes to polluting the planet.

well you send the message only twice per second.

Also readString has a builtin timeout at 1s so that's what you wait for.

if you change the receiving end code to:

#include <SoftwareSerial.h>
#define tx 11
#define rx 10
SoftwareSerial bt(rx, tx); //RX, TX
void setup()
{
  Serial.begin(115200);
  bt.begin(9600);
}

void loop()
{
  if(bt.available()>0)
  {
    Serial.write((char) bt.read());
  }
}

and open your Serial monitor at 115200 bauds, it will work much better

I would suggest to study Serial Input Basics to handle this

this is actual reciever code

#include <AFMotor.h>
#include <Servo.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
Servo servo1;
Servo servo2;
Servo servoClaw;

String command = "";

void setup()
{
  // Set initial speed of the motors & stop
  motor1.setSpeed(200);
  motor1.run(RELEASE);
  motor2.setSpeed(200);
  motor2.run(RELEASE);
  motor3.setSpeed(200);
  motor3.run(RELEASE);
  motor4.setSpeed(200);
  motor4.run(RELEASE);

  servo1.attach(9);     // Attach servo1 to pin 9
  servo2.attach(6);     // Attach servo2 to pin 6
  servoClaw.attach(5);  // Attach servoClaw to pin 5

  Serial.begin(9600);  // Initialize hardware serial communication
}

void loop()
{
  if (Serial.available())
  {
    char c = Serial.read();

    // Check for end of command
    if (c == '\n')
    {
      processCommand(command);
      command = "";  // Clear the command string
    }
    else
    {
      command += c;  // Append character to command string
    }
  }

  delay(100);
}

void processCommand(const String& command)
{
  Serial.print("Received command: ");
  Serial.println(command);

  // Compare the received command with predefined commands and take action accordingly
  if (command.equals("MF"))
  {
    moveForward();
  }
  else if (command.equals("MR"))
  {
    moveRight();
  }
  else if (command.equals("MB"))
  {
    moveBackward();
  }
  else if (command.equals("ML"))
  {
    moveLeft();
  }
  else if (command.equals("serleft"))
  {
    moveLeftServo();
  }
  else if (command.equals("serright"))
  {
    moveRightServo();
  }
  else if (command.equals("serup"))
  {
    moveUpServo();
  }
  else if (command.equals("serdown"))
  {
    moveDownServo();
  }
  else if (command.equals("serclaw"))
  {
    openCloseClaw();
  }
  else
  {
    stopMotors();
  }
}

void moveForward()
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  Serial.println("Moving forward");
}

void moveRight()
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  Serial.println("Moving right");
}

void moveBackward()
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  Serial.println("Moving backward");
}

void moveLeft()
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  Serial.println("Moving left");
}

void moveLeftServo()
{
  int pos = servo1.read();
  if (pos < 180)
  {
    pos += 5;
    servo1.write(pos);
  }
  Serial.println("Moving left servo");
}

void moveRightServo()
{
  int pos = servo1.read();
  if (pos > 0)
  {
    pos -= 5;
    servo1.write(pos);
  }
  Serial.println("Moving right servo");
}

void moveUpServo()
{
  int pos = servo2.read();
  if (pos < 180)
  {
    pos += 5;
    servo2.write(pos);
  }
  Serial.println("Moving up servo");
}

void moveDownServo()
{
  int pos = servo2.read();
  if (pos > 0)
  {
    pos -= 5;
    servo2.write(pos);
  }
  Serial.println("Moving down servo");
}

void openCloseClaw()
{
  int pos = servoClaw.read();
  if (pos == 0)
  {
    servoClaw.write(90);
    Serial.println("Opening claw");
  }
  else
  {
    servoClaw.write(0);
    Serial.println("Closing claw");
  }
}

void stopMotors()
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  Serial.println("Stopping motors");
}

which is needed to work with this transmitter code

#include <SoftwareSerial.h>

SoftwareSerial bluetoothSerial(8, 7); // RX, TX pins of HC-05

const int joystick1XPin = A0; // Analog input pin for Joystick 1 X-axis
const int joystick1YPin = A1; // Analog input pin for Joystick 1 Y-axis
const int joystick2XPin = A2; // Analog input pin for Joystick 2 X-axis
const int joystick2YPin = A3; // Analog input pin for Joystick 2 Y-axis
const int joystickButtonPin = 2; // Digital input pin for Joystick button

const int joystickThreshold = 50; // Threshold value to consider joystick movement

void setup()
{
  Serial.begin(9600);
  bluetoothSerial.begin(9600); // Initialize Bluetooth serial communication
  pinMode(joystickButtonPin, INPUT_PULLUP); // Set the joystick button pin as INPUT_PULLUP
}

void loop()
{
  int joystick1XValue = analogRead(joystick1XPin);
  int joystick1YValue = analogRead(joystick1YPin);
  int joystick2XValue = analogRead(joystick2XPin);
  int joystick2YValue = analogRead(joystick2YPin);
  int joystickButtonValue = digitalRead(joystickButtonPin);

  int joystick1XCommand = getJoystickCommand(joystick1XValue);
  int joystick1YCommand = getJoystickCommand(joystick1YValue);
  int joystick2XCommand = getJoystickCommand(joystick2XValue);
  int joystick2YCommand = getJoystickCommand(joystick2YValue);

  // Print the raw joystick values to the serial monitor
  //Serial.print("Joystick 1 X: ");
  //Serial.print(joystick1XValue);
  //Serial.print("\tJoystick 1 Y: ");
  //Serial.print(joystick1YValue);
  //Serial.print("\tJoystick 2 X: ");
  //Serial.print(joystick2XValue);
  //Serial.print("\tJoystick 2 Y: ");
  //Serial.println(joystick2YValue);

  // Print the joystick commands to the serial monitor
  //Serial.print("Joystick 1 X Command: ");
  //Serial.print(joystick1XCommand);
  //Serial.print("\tJoystick 1 Y Command: ");
  //Serial.print(joystick1YCommand);
  //Serial.print("\tJoystick 2 X Command: ");
  //Serial.print(joystick2XCommand);
  //Serial.print("\tJoystick 2 Y Command: ");
  //Serial.println(joystick2YCommand);

  // Check Joystick 1 movement
  if (joystick1XCommand == -1)
  {
    Serial.println("Move Right");
    sendBluetoothCommand("MR"); // Move Right
  }
  else if (joystick1XCommand == 1)
  {
    Serial.println("Move Left");
    sendBluetoothCommand("ML"); // Move Left
  }
  else if (joystick1YCommand == -1)
  {
    Serial.println("Move Backward");
    sendBluetoothCommand("MB"); // Move Backward
  }
  else if (joystick1YCommand == 1)
  {
    Serial.println("Move Forward");
    sendBluetoothCommand("MF"); // Move Forward
  }
  else 
  {
    Serial.println("STOP");
    sendBluetoothCommand("gasd");
  }

  // Check Joystick 2 movement
  if (joystick2XCommand == -1)
  {
    Serial.println("Left Servo Movement");
    sendBluetoothCommand("serleft"); // Left servo movement
  }
  else if (joystick2XCommand == 1)
  {
    Serial.println("Right Servo Movement");
    sendBluetoothCommand("serright"); // Right servo movement
  }
  else if (joystick2YCommand == -1)
  {
    Serial.println("Up Servo Movement");
    sendBluetoothCommand("serup"); // Up servo movement
  }
  else if (joystick2YCommand == 1)
  {
    Serial.println("Down Servo Movement");
    sendBluetoothCommand("serdown"); // Down servo movement
  }

  // Check Joystick button
  if (joystickButtonValue == LOW)
  {
    Serial.println("Joystick Button Pressed");
    sendBluetoothCommand("serclaw"); // Claw movement
  }

  delay(100); // Delay for 1 second
}

int getJoystickCommand(int value)
{
  if (value < (512 - joystickThreshold))
  {
    return -1; // Negative command for left or up movement
  }
  else if (value > (512 + joystickThreshold))
  {
    return 1; // Positive command for right or down movement
  }
  else
  {
    return 0; // No movement
  }
}

void sendBluetoothCommand(const char* command)
{
  bluetoothSerial.print(command);
}

so will it work with this....deadline is monday

so this is working but how do i integrate it into this
when it is working, it is showing like

exampleexampleesampleexample

when i want it to be

example
example
example
example

you need the sender to send an end marker, so that you know the message is complete. you can make that a line feed '\n' or carriage return '\r' or both

with both, it can be achieve simply with println() instead of print()

void sendBluetoothCommand(const char* command)
{
  bluetoothSerial.println(command);
}

otherwise you can include the end marker in the message

sendBluetoothCommand("serleft\n");
//                           ^^ end of line character

can you please send me the code if you have time

just change the sendBluetoothCommand function with what I gave you in post 20

This is showing example example when it should show

Example
Example
Example1
Servo1 etc