Hc-05 servo control

Hi i have a question i am trying to control wireless servo with 4 bluetooth hc05 modules (2 receiver and 2 transmitter modules to not overload a single transceiver circuit) and potentiometer because i am going to make a bionic arm and it needs to react instantly but my problem is that sometimes modules not connecting together sometimes the module in the first transceiver circuit controls all the servos

The first transceiver (Coder) module will control 3 servos

The second transceiver (Maker) module will control 2 servos

footnote: I've programmed the hc05s to only connect to each other as AT+BIND=ADDR there may be an error in the code

//TRANSMITTER CODE
#include <SoftwareSerial.h>
#define BT_SERIAL_TX 5
#define BT_SERIAL_RX 4
#define BT_SERIAL0_TX 3
#define BT_SERIAL0_RX 2
SoftwareSerial Coder(BT_SERIAL_TX, BT_SERIAL_RX);
SoftwareSerial Maker(BT_SERIAL0_TX, BT_SERIAL0_RX);

int potpin1 = A1;
int potpin2 = A2;
int potpin3 = A3;
int potpin4 = A4;             
int potpin5 = A5;

  
unsigned int val1;
unsigned int val2;
unsigned int val3;
unsigned int val4;
unsigned int val5;

void setup()
{
 Serial.begin(9600);
 Coder.begin(38400);
 Maker.begin(38400);
}
void loop(){ 

 val1 = analogRead(potpin1);
 val1 = map(val1, 0, 1023, 0, 179);
 Coder.write(val1);
 
 val2 = analogRead(potpin2);
 val2 = map(val2, 0, 1023, 0, 179);
 Coder.write(val2);
  
 val3 = analogRead(potpin3);
 val3 = map(val3, 0, 1023, 0, 179);
 Coder.write(val3);

 val4 = analogRead(potpin4);
 val4 = map(val4, 0, 1023, 0, 179);
 Maker.write(val4);

 val5 = analogRead(potpin5);
 val5 = map(val5, 0, 1023, 0, 179);
 Maker.write(val5);

  

 delay(100);
}
//RECEIVER CODE
#include <SoftwareSerial.h> 
#define BT_SERIAL_TX 5
#define BT_SERIAL_RX 4
#define BT_SERIAL0_TX 3
#define BT_SERIAL0_RX 2
SoftwareSerial Coder(BT_SERIAL_TX, BT_SERIAL_RX);
SoftwareSerial Maker(BT_SERIAL0_TX, BT_SERIAL0_RX);

#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

unsigned int val1 ;
unsigned int val2 ;
unsigned int val3 ;
unsigned int val4 ;
unsigned int val5 ;


void setup()
{
Serial.begin(9600);
Coder.begin(38400);
Maker.begin(38400);

myservo1.attach(8);
myservo2.attach(9);
myservo3.attach(10);
myservo4.attach(11);
myservo5.attach(12);
}

void loop() {
if (Coder.available()>0){
val1 = Coder.read();
myservo1.write(val1);

val2 = Coder.read();
myservo2.write(val2);

val3 = Coder.read();
myservo3.write(val3);
}


if (Maker.available()>0){
  val4 = Maker.read();
  myservo4.write(val4);

  val5 = Maker.read();
  myservo5.write(val5);
}
}

Study the following sketch (taken from web) to know the operational procedures of two software serial ports and then adjust your sketch. More here.

Using Multiple Software Serial

When you need multiple serial devices to be connected, it is possible to create multiple software serial ports. But due to hardware limitation, Arduino UNO can only listen to one software serial at a time. Here provides an example for multiple software serial:

#include <SoftwareSerial.h>
SoftwareSerial serialOne(2, 3); // Software Serial ONE
SoftwareSerial serialTwo(8, 9); // Software Serial TWO
 
void setup() {
  Serial.begin(9600);
  while (!Serial) { // wait till Serial
  }
 
  serialOne.begin(9600);
  serialTwo.begin(9600);
}
 
void loop() {
  serialOne.listen(); // listening on Serial One
 
  Serial.println("Data from port one:");
  while (serialOne.available() > 0) {
    char inByte = serialOne.read();
    Serial.write(inByte);
  }
 
  Serial.println();
 
  serialTwo.listen(); // listening on Serial Two
 
  Serial.println("Data from port two:");
  while (serialTwo.available() > 0) {
    char inByte = serialTwo.read();
    Serial.write(inByte);
  }
 
  Serial.println();
}
1 Like

it may be simpler to move to an Arduino with multiple hardware serial ports, e.g. Mega, Due, etc or even ESP32 which has WiFi, Bluetooth clasic and BLE builtin

1 Like

Why do you think that a single tranceiver-pair can get overloaded?

I don't think that you can overload a single tranceiver-pair
If you need a very fast reaction-time in the range of 10 milliseconds or even faster

The whole approach of using an arduino is wrong. You would need a controller with much more calculation-power than an arduino-uno with hardware serial or even using a different wireless technology than bluetooth

Why do you need a "coder" module and a "maker" module?

Please describe in detail what this means.
I am pretty sure that a solution can be found where a single pair of trancievers is enough for doing it all.

best regards Stefan

1 Like

When I made the system with a single transceiver, the servos were reacting very late, so I'm thinking of dual modules.

I did it with esp32 before, but it gave a 1 second delayed response, I did it with nrf24l01, communication could not be established

they are not coder and maker module i have assigned them as variable name
Sample

SerialOne(8, 9);
SerialTwo(10, 11);

Can you write me a single transceiver code 5servo control with 5potentiometer example?

this sounds as though you are reading from a Serial port which is timing out, i.e. when you call Serial1.read(), etc if nothing is available it waits a second in case something arrives then times out returning -1
this is a common cause of 1 second delays in many programs
upload your code?

Yet, the transmitter code contains this
delay(100);

I do not think you will need two sets of BT modules. A single sender and single receiver will do what you want.
I would recommend you send 5 bytes (mapped analog readings) with start and stop markers.

See the techniques presented in the Serial Input Basics Tutorial.

I also like the suggestion of the ESP32 with the built in Classic Bluetooth.

If you do choose to use a board like the Uno or Nano, you can probably speed things up by connecting the HC05 modules to the hardware serial pins. I don't see you using input from the Monitor. You will have to disconnect the modules when loading code.

1 Like

Two software serials are not the way when it comes to reception. Only one software serial can listen at a time.

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