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

**URL:** <https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334>\
**Category:** Programming\
**Created:** [June 9, 2023, 7:34pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334 "2023-06-09T19:34:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kratos\_119](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kratos_119/32/636545_2.png) [@kratos\_119](https://forum.arduino.cc/u/kratos_119)\
**Post date:** [June 9, 2023, 7:34pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/1 "2023-06-09T19:34:48Z")

</div>

#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 help`Use code tags to format code for the forum`

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [June 9, 2023, 7:40pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/2 "2023-06-09T19:40:28Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [June 9, 2023, 7:48pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/3 "2023-06-09T19:48:53Z")

</div>

> [@kratos\_119](#):
>
> int bluetoothTx = 0;  
> int bluetoothRx = 1;

Why use software serial on hardware serial pins?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 9, 2023, 7:53pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/4 "2023-06-09T19:53:44Z")

</div>

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)&nbsp;to make it easier to read and copy for examination   
  
[https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum)

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [June 9, 2023, 8:07pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/5 "2023-06-09T20:07:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kratos\_119](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kratos_119/32/636545_2.png) [@kratos\_119](https://forum.arduino.cc/u/kratos_119)\
**Post date:** [June 9, 2023, 8:14pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/6 "2023-06-09T20:14:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 9, 2023, 8:16pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/7 "2023-06-09T20:16:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kratos\_119](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kratos_119/32/636545_2.png) [@kratos\_119](https://forum.arduino.cc/u/kratos_119)\
**Post date:** [June 9, 2023, 8:17pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/8 "2023-06-09T20:17:01Z")

</div>

yes

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 9, 2023, 8:19pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/9 "2023-06-09T20:19:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 9, 2023, 9:14pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/10 "2023-06-09T21:14:15Z")

</div>

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).

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

```

Here is the simulation:

> **[kratos - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/367090023812727809)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

And the sketch:

```arduino
#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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 9, 2023, 9:53pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/11 "2023-06-09T21:53:13Z")

</div>

> [@build\_1971](#):
>
> bluetooth receives 2 numbers

1. Servopos1 = Servo
2. Servopos = Angle

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 6, 2023, 9:53pm UTC](https://forum.arduino.cc/t/i-dont-understand-this-code-properly-its-main-application-is-to-control-4-servomotors-and-1-bldc-motor/1136334/12 "2023-12-06T21:53:30Z")

</div>

Closed on December 6, 2023, 9:53pm UTC

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