How to know what BT module baud rate to use for your application?

Not sure if this is the right place to post this but, As the subject stated, How do I know when to set up HC05 and HC06 modules on 115600 or stick with the default of 9600? What’s the difference… I mean what are the benefits of the different baud rates?

I’m trying to diagnose momentary loss of control of my bot. My sketch works perfectly on one bot that has two dc motors and one joystick, but intermittently loose BT connection on a different bot that has 3 dc motors and 1 and a half joysticks.

I’m wondering if the baud rate (set to 9600) is not fast enough to pass the extra data from the second joystick?

HC06 Sketch

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <SoftwareSerial.h>

SoftwareSerial BTserial(2, 13); // RX | TX
// Connect the HC-06 TX to Arduino pin 2 RX. 
// Connect the HC-06 RX to Arduino pin 13 TX through a voltage divider.
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
boolean newData = false;
    // variables to hold the parsed data
    
int BladeVal = 0;
int RightvalY = 0;
int RightvalX = 0;
int Xspeed;
int Yspeed;
int BladeSpeed = 0;


const int DeadBand = 30;

Adafruit_DCMotor *BladeMotor = AFMS.getMotor(3);
Adafruit_DCMotor *LeftMotor = AFMS.getMotor(2);
Adafruit_DCMotor *RightMotor = AFMS.getMotor(1);

void setup()
{
    AFMS.begin();
    BTserial.begin(9600);

BladeMotor->setSpeed(0);
LeftMotor->setSpeed(0);
RightMotor->setSpeed(0);
BladeMotor->run(RELEASE);
LeftMotor->run(RELEASE);
RightMotor->run(RELEASE);
}

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        newData = false;
        Blade();
        Move();
   }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (BTserial.available() > 0 && newData == false) {
        rc = BTserial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index
    
    strtokIndx = strtok(tempChars,","); // get the first part - the string
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    RightvalY = atoi(strtokIndx);     // convert this part to an integer
    
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    RightvalX = atoi(strtokIndx);     // convert this part to an integer
    
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    BladeVal = atoi(strtokIndx);     // convert this part to an integer

}

//============

void Blade()
{
     
 if(BladeVal > 512 + DeadBand){
    BladeSpeed = map(BladeVal, 540, 1023, 0, 120);
      BladeMotor->run(BACKWARD);
      BladeMotor->setSpeed(BladeSpeed);
      
  }
  else if(BladeVal < 512 - DeadBand){
    BladeSpeed = map(BladeVal, 480, 0, 0, 120);
      BladeMotor->run(FORWARD);
      BladeMotor->setSpeed(BladeSpeed);
      
  }
  else {
       BladeMotor->run(RELEASE);
  }
}

void Move(){
  //Reverse
  if(RightvalY > 512 + DeadBand){  
          Yspeed = map(RightvalY, 540, 1023, 0, 200);
      
    if(RightvalX > (512 + DeadBand)){ //Adjusts Left Motor Speed for Turning
          Xspeed = map(RightvalX, 540, 1023, 0, 200);
          Xspeed = constrain(Xspeed, 0, Yspeed);
          LeftMotor->run(BACKWARD);
          RightMotor->run(BACKWARD);
          LeftMotor->setSpeed(Yspeed - Xspeed);
          RightMotor->setSpeed(Yspeed);

      }
    else if(RightvalX < (512 - DeadBand)){ //Adjusts Right Motor Speed for Turning
          Xspeed = map(RightvalX, 480, 0, 0, 200);
          Xspeed = constrain(Xspeed, 0, Yspeed);
          LeftMotor->run(BACKWARD);
          RightMotor->run(BACKWARD);
          LeftMotor->setSpeed(Yspeed);
          RightMotor->setSpeed(Yspeed - Xspeed);

    }
    else{
      
          LeftMotor->run(BACKWARD);
          RightMotor->run(BACKWARD);
          LeftMotor->setSpeed(Yspeed);
          RightMotor->setSpeed(Yspeed);
        
    }
  }
//-----------------------------
  //FORWARD
  else if(RightvalY < 512 - DeadBand){  
          Yspeed = map(RightvalY, 480, 0, 0, 200);
      
    if(RightvalX > (512 + DeadBand)){ //Adjusts Left Motor Speed for Turning
          Xspeed = map(RightvalX, 540, 1023, 0, 200);
          Xspeed = constrain(Xspeed, 0, Yspeed);
          LeftMotor->run(FORWARD);
          RightMotor->run(FORWARD);
          LeftMotor->setSpeed(Yspeed);
          RightMotor->setSpeed(Yspeed - Xspeed);

      }
    else if(RightvalX < (512 - DeadBand)){ //Adjusts Right Motor Speed for Turning
          Xspeed = map(RightvalX, 480, 0, 0, 200);
          Xspeed = constrain(Xspeed, 0, Yspeed);
          LeftMotor->run(FORWARD);
          RightMotor->run(FORWARD);
          LeftMotor->setSpeed(Yspeed - Xspeed);
          RightMotor->setSpeed(Yspeed);

    }
    else{
      
          LeftMotor->run(FORWARD);
          RightMotor->run(FORWARD);
          LeftMotor->setSpeed(Yspeed);
          RightMotor->setSpeed(Yspeed);
        
    }
  }
//------------------------------
  else{ 
    
// Zero Point Turning  
    if(RightvalX > (512 + DeadBand)){ // zero point turn Left
         Xspeed = map(RightvalX, 540, 1023, 0, 180);
      
          LeftMotor->run(FORWARD);
          RightMotor->run(BACKWARD);
          LeftMotor->setSpeed(Xspeed);
          RightMotor->setSpeed(Xspeed);

    }
    else if(RightvalX < (512 - DeadBand)){// zero point turn Right
         Xspeed = map(RightvalX, 480, 0, 0, 180);
         
          LeftMotor->run(BACKWARD);
          RightMotor->run(FORWARD);
          LeftMotor->setSpeed(Xspeed);
          RightMotor->setSpeed(Xspeed);
    }
    else{
          LeftMotor->run(RELEASE);
          RightMotor->run(RELEASE);

    } 
  }
}

If you are using SoftwareSerial stay with 9600 baud. It won't work at 115200.

Make sure you don't send any unnecessary characters. Post an example of a typical message.

If that is not fast enough then you probably need an Arduino with an extra HardwareSerial port such as a Mega, Leonardo or Micro. With HardwareSerial you can use high baud rates.

...R

A serial print on the terman would look something like this if the joystick are centred.

512512512
512512512
512512512
512512512
512512512
512512512
512512512

Blade up max

1023512512
1023512512
1023512512

Blade down max

0512512
0512512
0512512

Zero point turn left would be something like

51201023
51201023
51201023

And zero point turn right

51210230
51210230
51210230

The master sketch is sending the values with a "," between each value. I'll post the master sketch when I get home

That data seems to be as sparse as it can be, short of sending binary data which would be more efficient and also more difficult to debug.

You did not say how often the data needs to be sent?

Assuming a worst case message of <1023,512,512> or 14 characters the transmission time would be about 14 millisecs at 9600 baud so you could easily send a message every 100 millisecs - i.e. 10 times per second.

…R

Well that’s a bit embarrassing. Thank you once again Robin for pointing that out to me. I thought I saved the delay(100); in the Master sketch from last time but I guess not. It’s all running perfectly now.

On a slightly different note,
In the slave sketch I feel like there is unnecessary code with this

strtokIndx = strtok(tempChars,",");

I don’t need any letters to be sent but if I try to delete that line I just get zero’s on the monitor. And in the master sketch, if I don’t send a character first then my joysticks get mixed up ie; RightValY is over looked, RightValX becomes LeftValY and LeftValY becomes LeftValX or something to that effect.

I don’t really understand how your serial communication example works so it’s hard for me to make changes.

Here is the Master Sketch. I wanted to make it like a universal remote so it includes all 4 joystick axis.

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX. 
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.

int RightjoyYPin = A0;
int RightjoyXPin = A1;
int LeftjoyYPin = A2;
int LeftjoyXPin = A3;

void setup(){
  
  BTserial.begin(9600);
  Serial.begin(9600);
  Serial.println("<Arduino is ready>"); 
}

void loop(){
  
   int RightvalY = analogRead(RightjoyYPin);
   int RightvalX = analogRead(RightjoyXPin);
   int LeftvalY = analogRead(LeftjoyYPin);
   int LeftvalX = analogRead(LeftjoyXPin);
  
       
  BTserial.print("<");
       BTserial.print(0);
    BTserial.print(",");
       BTserial.print(RightvalY);
    BTserial.print(",");
       BTserial.print(RightvalX);
    BTserial.print(",");
       BTserial.print(LeftvalY);
    BTserial.print(",");
      BTserial.print(LeftvalX);
    BTserial.println(">");
    

    
//==============================

 /* Serial.print("RightY ");
       Serial.print(RightvalY);
    Serial.print("   RightX ");
       Serial.print(RightvalX);
    Serial.print("   LeftY ");
       Serial.print(LeftvalY);
    Serial.print("   LeftX ");
       Serial.print(LeftvalX);
    Serial.println(">");*/
    
    delay(100);

}

gyrojeremy:
On a slightly different note,
In the slave sketch I feel like there is unnecessary code with this

strtokIndx = strtok(tempChars,",");

You need to post the slave sketch that you are using.

Is the master sketch in Reply #4 is the latest version?

...R