Multi Servo control via Bluetooth HC-05

Hello,

I am a bit stuck here and was hoping someone may be able to help. I am trying to control 5 servos with 5 pots via bluetooth but i am having no luck!

i got this program working with 1 pot and servo so I know the bluetooth is working but I must be doing something wrong trying to scale it up.

Here’s the code i’m trying;

////Master Code////

#define ledPin 9

int state = 0;
int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;

int potValue = 0;
int potValue1 = 1;
int potValue2 = 2;
int potValue3 = 3;
int potValue4 = 4;

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
state1 = Serial.read(); // Reads the data from the serial port
state2 = Serial.read(); // Reads the data from the serial port
state3 = Serial.read(); // Reads the data from the serial port
state4 = Serial.read(); // Reads the data from the serial port
}

// Reading the potentiometer
potValue = analogRead(A0);
potValue1 = analogRead(A1);
potValue2 = analogRead(A2);
potValue3 = analogRead(A3);
potValue4 = analogRead(A4);

int potValueMapped = map(potValue, 0, 1023, 0, 180);
Serial.write(potValueMapped); // Sends potValue to servo motor
delay(10);
int potValueMapped1 = map(potValue1, 0, 1023, 0, 180);
Serial.write(potValueMapped1); // Sends potValue to servo motor
delay(10);
int potValueMapped2 = map(potValue2, 0, 1023, 0, 180);
Serial.write(potValueMapped2); // Sends potValue to servo motor
delay(10);
int potValueMapped3 = map(potValue3, 0, 1023, 0, 180);
Serial.write(potValueMapped3); // Sends potValue to servo motor
delay(10);
int potValueMapped4 = map(potValue4, 0, 1023, 0, 180);
Serial.write(potValueMapped4); // Sends potValue to servo motor
delay(10);

}

///Slave Code////

#include <Servo.h>
#define button 8

Servo myServo;
int state = 20;

Servo myServo1;
int state1 = 20;

Servo myServo2;
int state2 = 20;

Servo myServo3;
int state3 = 20;

Servo myServo4;
int state4 = 20;

void setup() {
pinMode(button, INPUT);
myServo.attach(9);
myServo1.attach(10);
myServo2.attach(11);
myServo3.attach(12);
myServo4.attach(13);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read();
state1 = Serial.read();
state2 = Serial.read();
state3 = Serial.read();
state4 = Serial.read();
// Reads the data from the serial port
}
// Controlling the servo motor
myServo.write(state);
delay(10);
myServo1.write(state1);
delay(10);
myServo2.write(state2);
delay(10);
myServo3.write(state3);
delay(10);
myServo4.write(state4);
delay(10);
}

Any help anyone could supply would be great,

Thanks!!!

It's not always about duplicating lines

 if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
    state1 = Serial.read(); // Reads the data from the serial port
    state2 = Serial.read(); // Reads the data from the serial port
    state3 = Serial.read(); // Reads the data from the serial port
    state4 = Serial.read(); // Reads the data from the serial port
  }

What do you send on Serial and how many bytes do you have in the buffer to enter this if? what do you think this is doing??

What are states used for in the master?

the simplest, most brutal hack to get things moving would be to do the following in your slave

if (Serial.available() >= 5 )

but what this does NOT do is make sure that the first value of each batch of five is for the first servo. I'll let YOU think about how to do it. there are several ways.

but sir how to control this, any app & devices????

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. Look especially at the 3rd example and the parse example.

You should send all your data as a single message with code something like this

Serial.print('<');  // the start marker
Serial.print(potVal1);
Serial.print(',');  // comma delimiter
Serial.print(potVal2);
Serial.print(','); 
Serial.print(potVal3);
Serial.print('>');  // end marker

That way the receiving code will know which item is for which servo.

Sending the data using print() rather than write() makes debugging much easier

…R