Multi servo control with bluetooth via potentiometer

Hello we
We are trying to make servo control circuit with hc 05 bluetooth connection with 6 potentiometers.
but I can control a single servo with the codes we have written.
Could you write me a sample transceiver code?

(footnote: there is a single servo control transceiver code attached for your information)

//TRANSMITTER CODE 



#include <SoftwareSerial.h>
#define BT_SERIAL_TX 4
#define BT_SERIAL_RX 3
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
int potpin = A0;
  
unsigned int val;
void setup()
{
 Serial.begin(9600);
 BluetoothSerial.begin(9600);
}
void loop()
{
 val = analogRead(potpin);
 val = map(val, 0, 1023, 0, 179);
 BluetoothSerial.write(val);
  

//RECEIVER CODE



#include <SoftwareSerial.h>
#define BT_SERIAL_TX 4
#define BT_SERIAL_RX 3
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
#include <Servo.h>
Servo myservo;
unsigned int val ;
void setup()
{
Serial.begin(9600);
BluetoothSerial.begin(9600);
myservo.attach(10);
}
void loop() // run over and over
{
if (BluetoothSerial.available()>0){
  
val = BluetoothSerial.read();
  
myservo.write(val);
}
  
}

My starting point would be to create an array of pots, and an array of servos.
That will consolidate your code, then once you have one working, you have them all working.

Do not use blocking delay() or while loops, or you’ll start having problems.

1 Like

Thank you for your reply. Could you send me an example of 5 potentiometers and 5 servo motor control codes via bluetooth?
but our request is like this, we want to control 5 different pots and 5 different servos control with bluetooth

i am a beginner yet

hello again, I wrote the code again, can you check if there are any errors?

//RECEIVER CODE



#include <SoftwareSerial.h>
#include <Servo.h>
#define BT_SERIAL_TX 7
#define BT_SERIAL_RX 6
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);


Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;

int x[4];

void setup()
{
Serial.begin(9600);
BluetoothSerial.begin(9600);
myservo0.attach(8);
myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}

void loop() // run over and over
{
if (BluetoothSerial.available()>0){
   x[0] = BluetoothSerial.read();
   myservo0.write(x[0]);
   
   x[1] = BluetoothSerial.read();
   myservo1.write(x[1]);
   
   x[2] = BluetoothSerial.read();
   myservo2.write(x[2]);
   
   x[3] = BluetoothSerial.read();
   myservo3.write(x[3]);


 }
}
//TRANSMITTER CODE 



#include <SoftwareSerial.h>
#define BT_SERIAL_TX 7
#define BT_SERIAL_RX 6
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);

int potpin0 = A0;
int potpin1 = A1;
int potpin2 = A2;
int potpin3 = A3;

  
int x[4];

void setup()
{
 Serial.begin(9600);
 BluetoothSerial.begin(9600);
}
void loop()
{
 x[0] = analogRead(potpin0);
 x[0] = map(x[0], 0, 1023, 0, 179);
 BluetoothSerial.write(x[0]);


 x[1] = analogRead(potpin1);
 x[1] = map(x[1], 0, 1023, 0, 179);
 BluetoothSerial.write(x[1]);

 
 x[2] = analogRead(potpin2);
 x[2] = map(x[2], 0, 1023, 0, 179);
 BluetoothSerial.write(x[2]);

  
 x[3] = analogRead(potpin3);
 x[3] = map(x[3], 0, 1023, 0, 179);
 BluetoothSerial.write(x[3]);



}

You’ll always be a beginner until you dig in - and try…

Here’s a starting point

Servo myServo[4];
int myPot[4];

Try them out, you could combine them into a structure if that helps your project requirements.

struct myChannel {
  Servo myServo;
  int myPot;
} 
myChannel channel [4];

then use the parts as needed… e,g.

aValue = analogRead(channel[2].myPot);

2 Likes

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