Bluetooth module(HC05) isn´t communicating correctly due to code or app related problems

Hello,
I have built an Arduino robotic arm following this link...

However instead of using an Arduino Uno as in the link, I used an Elegoo Mega. Accordingly I initialized the Bluetooth module (HC05) on pins 12 and 13 instead of pins 3 and 4.
I also adjusted the wiring on the board as required( not in 3 and 4).
The code compiles and uploads successfully, but when I check the communication using the serial monitor , I only receive trash.
Other people have suggested that the issue might not be with the Arduino Code but with the corresponding MIT App.
When I use the app and move the sliders to control the servo motors, nothing happens- even though the HC05 module is receiving data.
It seems like the arduino cannot interpret the data being sent correctly.
I am not very experienced in programming, so troubleshooting the issue is challenging for me.
The wiring matches the circuit diagram precisely, so I am unsure where the problem lies.
If someone could help me, I would greatly appreciate it.
(ideally with an explanation that is easy for a beginner to follow)
Thank you very much!!!!


Here is my code:

#include <SoftwareSerial.h>
#include <Servo.h>

Servo servo01;
Servo servo02;
Servo servo03;
Servo servo04;
Servo servo05;
Servo servo06;

SoftwareSerial Bluetooth(13 , 12); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)

int servo1Pos, servo2Pos, servo3Pos, servo4Pos, servo5Pos, servo6Pos; // current position
int servo1PPos, servo2PPos, servo3PPos, servo4PPos, servo5PPos, servo6PPos; // previous position
int servo01SP[50], servo02SP[50], servo03SP[50], servo04SP[50], servo05SP[50], servo06SP[50]; // for storing positions/steps
int speedDelay = 20;
int index = 0;
String dataIn = "";
void setup() {

Serial.begin(38400);

  // put your setup code here, to run once:
 servo01.attach(5);
  servo02.attach(6);
  servo03.attach(7);
  servo04.attach(8);
  servo05.attach(9);
  servo06.attach(10);
  Bluetooth.begin(38400); // Default baud rate of the Bluetooth module
  Bluetooth.setTimeout(1);
  delay(20);
  // Robot arm initial position
  servo1PPos = 90;
  servo01.write(servo1PPos);
  servo2PPos = 90;
  servo02.write(servo2PPos);
  servo3PPos = 90;
  servo03.write(servo3PPos);
  servo4PPos = 90;
  servo04.write(servo4PPos);
  servo5PPos = 90;
  servo05.write(servo5PPos);
  servo6PPos = 90;
  servo06.write(servo6PPos);
  Serial.println("init ok");
}

void loop() {
  // put your main code here, to run repeatedly:
  // Check for incoming data
  if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.readString();  // Read the data as string
    Serial.println("bluetooth ok");
    Serial.println(dataIn);

    // If "Waist" slider has changed value - Move Servo 1 to position
    if (dataIn.startsWith("s1")) {
      String dataInS = dataIn.substring(2, dataIn.length()); // Extract only the number. E.g. from "s1120" to "120"
      servo1Pos = dataInS.toInt();  // Convert the string into integer
      Serial.print("Servo 1: ");
      Serial.println(servo1Pos);
      // We use for loops so we can control the speed of the servo
      // If previous position is bigger then current position
      if (servo1PPos > servo1Pos) {
        for ( int j = servo1PPos; j >= servo1Pos; j--) {   // Run servo down
          servo01.write(j);
          delay(20);    // defines the speed at which the servo rotates
        }
      }
      // If previous position is smaller then current position
      if (servo1PPos < servo1Pos) {
        for ( int j = servo1PPos; j <= servo1Pos; j++) {   // Run servo up
          servo01.write(j);
          delay(20);
        }
      }
      servo1PPos = servo1Pos;   // set current position as previous position
    }
      // Move Servo 2
    if (dataIn.startsWith("s2")) {
      String dataInS = dataIn.substring(2, dataIn.length());
      servo2Pos = dataInS.toInt();
      Serial.print("Servo 2: ");
      Serial.println(servo2Pos);
      if (servo2PPos > servo2Pos) {
        for ( int j = servo2PPos; j >= servo2Pos; j--) {
          servo02.write(j);
          delay(50);
        }
      }
      if (servo2PPos < servo2Pos) {
        for ( int j = servo2PPos; j <= servo2Pos; j++) {
          servo02.write(j);
          delay(50);
        }
      }
      servo2PPos = servo2Pos;
    }
      // Move Servo 3
    if (dataIn.startsWith("s3")) {
      String dataInS = dataIn.substring(2, dataIn.length());
      servo3Pos = dataInS.toInt();
      Serial.print("Servo 3: ");
      Serial.println(servo3Pos);
      if (servo3PPos > servo3Pos) {
        for ( int j = servo3PPos; j >= servo3Pos; j--) {
          servo03.write(j);
          delay(30);
        }
      }
      if (servo3PPos < servo3Pos) {
        for ( int j = servo3PPos; j <= servo3Pos; j++) {
          servo03.write(j);
          delay(30);
        }
      }
      servo3PPos = servo3Pos;
    }
     // Move Servo 4
    if (dataIn.startsWith("s4")) {
      String dataInS = dataIn.substring(2, dataIn.length());
      servo4Pos = dataInS.toInt();
      Serial.print("Servo 4: ");
      Serial.println(servo4Pos);
      if (servo4PPos > servo4Pos) {
        for ( int j = servo4PPos; j >= servo4Pos; j--) {
          servo04.write(j);
          delay(30);
        }
      }
      if (servo4PPos < servo4Pos) {
        for ( int j = servo4PPos; j <= servo4Pos; j++) {
          servo04.write(j);
          delay(30);
        }
      }
      servo4PPos = servo4Pos;
    }
    // Move Servo 5
    if (dataIn.startsWith("s5")) {
      String dataInS = dataIn.substring(2, dataIn.length());
      servo5Pos = dataInS.toInt();
      Serial.print("Servo 5: ");
      Serial.println(servo5Pos);
      if (servo5PPos > servo5Pos) {
        for ( int j = servo5PPos; j >= servo5Pos; j--) {
          servo05.write(j);
          delay(30);
        }
      }
      if (servo5PPos < servo5Pos) {
        for ( int j = servo5PPos; j <= servo5Pos; j++) {
          servo05.write(j);
          delay(30);
        }
      }
       servo5PPos = servo5Pos;
    }
    // Move Servo 6
    if (dataIn.startsWith("s6")) {
      String dataInS = dataIn.substring(2, dataIn.length());
      servo6Pos = dataInS.toInt();
      Serial.print("Servo 6: ");
      Serial.println(servo6Pos);
      if (servo6PPos > servo6Pos) {
        for ( int j = servo6PPos; j >= servo6Pos; j--) {
          servo06.write(j);
          delay(30);
        }
      }
      if (servo6PPos < servo6Pos) {
        for ( int j = servo6PPos; j <= servo6Pos; j++) {
          servo06.write(j);
          delay(30);
        }
      }
      servo6PPos = servo6Pos; 
    }
     // If button "SAVE" is pressed
    if (dataIn.startsWith("SAVE")) {
      Serial.println("SAVE");
      servo01SP[index] = servo1PPos;  // save position into the array
      servo02SP[index] = servo2PPos;
      servo03SP[index] = servo3PPos;
      servo04SP[index] = servo4PPos;
      servo05SP[index] = servo5PPos;
      servo06SP[index] = servo6PPos;
      index++;                        // Increase the array index
    }
    // If button "RUN" is pressed
    if (dataIn.startsWith("RUN")) {
      Serial.println("RUN");
      runservo();  // Automatic mode - run the saved steps 
    }
    // If button "RESET" is pressed
    if ( dataIn == "RESET") {
      Serial.println("RESET");
      memset(servo01SP, 0, sizeof(servo01SP)); // Clear the array data to 0
      memset(servo02SP, 0, sizeof(servo02SP));
      memset(servo03SP, 0, sizeof(servo03SP));
      memset(servo04SP, 0, sizeof(servo04SP));
      memset(servo05SP, 0, sizeof(servo05SP));
      memset(servo06SP, 0, sizeof(servo06SP));
      index = 0;  // Index to 0
    }
  }
}
// Automatic mode custom function - run the saved steps
void runservo() {
  while (dataIn != "RESET") {   // Run the steps over and over again until "RESET" button is pressed
    for (int i = 0; i <= index - 2; i++) {  // Run through all steps(index)
      if (Bluetooth.available() > 0) {      // Check for incomding data
        dataIn = Bluetooth.readString();
        if ( dataIn == "PAUSE") {           // If button "PAUSE" is pressed
          while (dataIn != "RUN") {         // Wait until "RUN" is pressed again
            if (Bluetooth.available() > 0) {
              dataIn = Bluetooth.readString();
              if ( dataIn == "RESET") {     
                break;
              }
            }
          }
        }
        // If speed slider is changed
        if (dataIn.startsWith("ss")) {
          String dataInS = dataIn.substring(2, dataIn.length());
          speedDelay = dataInS.toInt(); // Change servo speed (delay time)
        }
      }
      // Servo 1
      if (servo01SP[i] == servo01SP[i + 1]) {
      }
      if (servo01SP[i] > servo01SP[i + 1]) {
        for ( int j = servo01SP[i]; j >= servo01SP[i + 1]; j--) {
          servo01.write(j);
          delay(speedDelay);
        }
      }
      if (servo01SP[i] < servo01SP[i + 1]) {
        for ( int j = servo01SP[i]; j <= servo01SP[i + 1]; j++) {
          servo01.write(j);
          delay(speedDelay);
            }
      }

      // Servo 2
      if (servo02SP[i] == servo02SP[i + 1]) {
      }
      if (servo02SP[i] > servo02SP[i + 1]) {
        for ( int j = servo02SP[i]; j >= servo02SP[i + 1]; j--) {
          servo02.write(j);
          delay(speedDelay);
        }
      }
      if (servo02SP[i] < servo02SP[i + 1]) {
        for ( int j = servo02SP[i]; j <= servo02SP[i + 1]; j++) {
          servo02.write(j);
          delay(speedDelay);
        }
      }

      // Servo 3
      if (servo03SP[i] == servo03SP[i + 1]) {
      }
      if (servo03SP[i] > servo03SP[i + 1]) {
        for ( int j = servo03SP[i]; j >= servo03SP[i + 1]; j--) {
          servo03.write(j);
          delay(speedDelay);
        }
      }
      if (servo03SP[i] < servo03SP[i + 1]) {
        for ( int j = servo03SP[i]; j <= servo03SP[i + 1]; j++) {
          servo03.write(j);
          delay(speedDelay);
        }
      }

      // Servo 4
      if (servo04SP[i] == servo04SP[i + 1]) {
      }
      if (servo04SP[i] > servo04SP[i + 1]) {
        for ( int j = servo04SP[i]; j >= servo04SP[i + 1]; j--) {
          servo04.write(j);
          delay(speedDelay);
        }
      }
      if (servo04SP[i] < servo04SP[i + 1]) {
        for ( int j = servo04SP[i]; j <= servo04SP[i + 1]; j++) {
          servo04.write(j);
          delay(speedDelay);
        }
      }

      // Servo 5
      if (servo05SP[i] == servo05SP[i + 1]) {
      }
      if (servo05SP[i] > servo05SP[i + 1]) {
        for ( int j = servo05SP[i]; j >= servo05SP[i + 1]; j--) {
          servo05.write(j);
          delay(speedDelay);
        }
      }
      if (servo05SP[i] < servo05SP[i + 1]) {
        for ( int j = servo05SP[i]; j <= servo05SP[i + 1]; j++) {
          servo05.write(j);
          delay(speedDelay);
        }
      }

      // Servo 6
      if (servo06SP[i] == servo06SP[i + 1]) {
      }
      if (servo06SP[i] > servo06SP[i + 1]) {
        for ( int j = servo06SP[i]; j >= servo06SP[i + 1]; j--) {
          servo06.write(j);
          delay(speedDelay);
        }
      }
      if (servo06SP[i] < servo06SP[i + 1]) {
        for ( int j = servo06SP[i]; j <= servo06SP[i + 1]; j++) {
          servo06.write(j);
          delay(speedDelay);
        }
      }
    }
  }
}

Regarding your project wiring, you might double check it against a bluetooth terminal app from the Play Store, to rule out AI2 problems.

I don't see you using any end of message delimiters like \n (decimal 10).

They are especially important when you are sending variable length message suffixes like thumb positions.

Check your development language library for verbs like ReadUntil to help grab only one message at a time.

This would require you to add '\n' to the ends of all messages sent either way.

Because of Bluetooth buffering, grabbing the entire conrtents of the incoming buffer might leave you with multiple messages in your String variable, and result in failure of any exact match attempts against your variable (unless you scan letter by letter.)

Make a test application, send some value, then show what the monitor displays using this function. We want to know exactly what you send and exactly what you receive.

This is not enough, but if these are strange characters, you may have the incorrect data transfer speed set here:

Check what speed your BT module supports.

The baud rate seems to be set correctly. I tested it again and would like to explain the current status with a few images below:
1, Image 1 shows the output in the serial monitor when I try to control the robot via app.

2, the robot now responds to the movement of the sliders in the app(image 2). However, only one servo motor moves, specifically the Wrist Roll.

3, I tested all the other servos using a predefined Arduino code, and they all work perfectly. Still, they do not respond when I move the corresponding sliders in the app. Only the wrist roll moves.

4, I also tried using the readuntil() method. The result is shown in image 3 .
However, I am unsure whether I inserted the string correctly, as I have no prior knowledge of programming.


2

3


1


3

This can't be diagnosed without aia and ino files.

To me it looks like a transmission speed problem, then such bushes appear. If I were you I would set it to 9600 instead of 38400. I think the default speed may depend on the manufacturer and may be different for different modules of the same type.

See this topic on how to adjust the speed to make sure it is that way.

I was advised to use a messager delimiter, such as \n(decimal 10), at the end of the message. I tried implementing the limiter to my code, but I think I did it incorrectly because it still doesnt work as expected. I also adjusted the baud rate, hoping it might help, but that didnt solve the problem either.
The main issue is that I dont fully understand how to program these features correctly or how to debug my code to find the root of the problem. I lack the necessary background in programming to handle this on my own, and I dont know anyone to help me.
My goal is to demonstrate this project to someone, but it needs to function reliable for that to happen.
Is there anyone who could review my code, identify the issue, and correct it? I would greatly appreciate it .
Thank you for your help!

Have you tried my suggestion of changing the baudrate for BT to a few other typical speeds? I would definitely try 9600.

Bluetooth.begin(9600);

Apart from that, to start with, to check the correctness of the electrical connections and the communication speed, create a simple program that does nothing but read messages from BT and display them in the serial monitor.

Without going into the code, just looking at this part of the code:

if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.readString();  // Read the data as string
    Serial.println("bluetooth ok");
    Serial.println(dataIn);

I can confirm that Serial monitor should show some numeric values, among them S1, S2 etc. But you don't see it, instead you see strange characters. Your code is correct, your blocks are correct. The problem is with the speed or the electrical connection, or the bt module itself.

Can you show us photos of the electrical connections and the BT module itself?

I think this is a confirmation that hc05 can have baudrate = 9600:

And this:

The default baud rate of HC-05 in command mode is 38400bps and 9600 in data mode .

It's a pity you didn't read the comments section on this site, it would have saved you a lot of time looking for the source of the problem.

Yes I tried 9600 and other baud rates.
I will send you photos of the electrical connections










I am sorry for not answering as fast as possible. I have forgotten my login credentials. That won't happen again. But I really want to thank you very much for your help.
Forget what I said before. Today it worked with a baud rate of 9600. The motors moved with the app. Only the gripper is not working quite as it should, an the shoulder as well. It could also be because the SG90 servo doesnt have enough power. In any case, this one doesnt respond at all. The others do, but not always precisely. However, its already progress that some are responding to the app. Only the gripper- basically, all SG90 servos- arent working quite as they should.

The basic principle works, which I already consider progress. However, the servos work much better when they operate individually, meaning only one servo is connected at a time, or when I unplug the shoulder servo.

Does anyone have a suggestion for solving this problem? I can’t keep unplugging and replugging the cables. Is there a way to disable the unused servos via code? For example, when one is active, all others are off?

Right now, when I control one servo, the upper ones move along, which shouldn't happen.