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);
}
}
}