I don't understand this code properly its main application is to control 4 servomotors and 1 BLDC motor

#include <SoftwareSerial.h>

#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3, myservo4, myservo5;

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
myservo1.attach(3);
myservo2.attach(5);
myservo3.attach(6);
myservo4.attach(9);
myservo5.attach(10);

Serial.begin(9600);

bluetooth.begin(9600);
}

void loop()
{
if(bluetooth.available()>= 2 )
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);

if (realservo >= 1000 && realservo <1180) {
  int servo1 = realservo;
  servo1 = map(servo1, 1000, 1180, 0, 180);
  myservo1.write(servo1);
  Serial.println("Servo 1 Rotate");
  delay(10);
}
if (realservo >= 2000 && realservo <2180) {
  int servo2 = realservo;
  servo2 = map(servo2, 2000, 2180, 0, 180);
  myservo2.write(servo2);
  Serial.println("Servo 2 Rotate");
  delay(10);
}
if (realservo >= 3000 && realservo <3180) {
  int servo3 = realservo;
  servo3 = map(servo3, 3000, 3180, 0, 180);
  myservo3.write(servo3);
  Serial.println("Servo 3 Rotate");
  delay(10);
}
if (realservo >= 4000 && realservo <4180) {
  int servo4 = realservo;
  servo4 = map(servo4, 4000, 4180, 0, 180);
  myservo4.write(servo4);
  Serial.println("Servo 4 Rotate");
  delay(10);
}
if (realservo >= 5000 && realservo <5180) {
  int servo5 = realservo;
  servo5 = map(servo5, 5000, 5180, 1000, 2000);
  myservo5.write(servo5);
  Serial.println("Servo 5 Rotate");
  delay(10);
}

}
}

I am unable to figure out why use bluetooth.available()>= 2 and unsigned int realservo = (servopos1 *256) + servopos; pls helpUse code tags to format code for the forum

The bluetooth receives 2 numbers (two 8 bit numbers).
They are combined to make a 16 bit number (the first number is now the high byte).

Your code first derives the servo number and then the position for that servo... (so basically the two numbers are combined and then split again...

At least that is the idea.
But 1000 should be 0b1000. And all the other numbers should also be in binary.
So my guess is that this code does not what you want...

Basically it makes little sense to combine and split the numbers. Also the mapping is not needed (you could use a mask).

Why use software serial on hardware serial pins?

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

The code could be greatly simplified with some arithmetic and some refactoring.

well no the code does work the way I want but the problem is every time I run the code all servos rotate by 90 degrees I am able to control the servo with the Bluetooth

Do you mean that they rotate to 90 degrees whenever the sketch starts or at some other time ?

yes

90 degrees is the default start position. If you want the servos to start at a different position then write() the position to them before attach()ing them

Hello @kratos_119 -

Your servo routines work. I have disabled bluetooth in your sketch and substituted this bit to choose random servos move to random positions (and a delay at the end to slow things down).

 int motor = (random(5)+1);
 int angle = (random(180));
 realservo = motor * 1000 + angle;

Here is the simulation:

And the sketch:

#include <SoftwareSerial.h>

#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3, myservo4, myservo5;

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo1.attach(3);
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(9);
  myservo5.attach(10);

  Serial.begin(9600);

  bluetooth.begin(9600);
}

void loop()
{
  // if (bluetooth.available() >= 2 ) // disabled for simulation
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();

    unsigned int realservo = (servopos1 * 256) + servopos;
    // Serial.print(realservo); // disabled for simulation

// for simulation
 int motor = (random(1, 6));
 int angle = (random(180));
 realservo = motor * 1000 + angle;
 // for simulation

    if (realservo >= 1000 && realservo < 1180) {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
      myservo1.write(servo1);
      Serial.print("Servo 1 Rotate ");
      Serial.println(servo1); // for simulation
      delay(10);
    }
    if (realservo >= 2000 && realservo < 2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.print("Servo 2 Rotate ");
      Serial.println(servo2); // for simulation
      delay(10);
    }
    if (realservo >= 3000 && realservo < 3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.print("Servo 3 Rotate ");
      Serial.println(servo3); // for simulation
      delay(10);
    }
    if (realservo >= 4000 && realservo < 4180) {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 0, 180);
      myservo4.write(servo4);
      Serial.print("Servo 4 Rotate ");
      Serial.println(servo4); // for simulation
      delay(10);
    }
    if (realservo >= 5000 && realservo < 5180) {
      int servo5 = realservo;
      servo5 = map(servo5, 5000, 5180, 0, 180);
      // servo5 = map(servo5, 5000, 5180, 1000, 2000); // this line is not right
      myservo5.write(servo5);
      Serial.print("Servo 5 Rotate ");
      Serial.println(servo5); // for simulation
      delay(10);
    }
delay(500);
  }
}
  1. Servopos1 = Servo
  2. Servopos = Angle

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