Help me with a piece of code i can't grasp

so i am making a project including 3 servos controlled via bluetooth(HC-05) and there is this bit of code i dont understand



heres the entire code;

#include <SoftwareSerial.h>

#include <Servo.h> 
Servo myservo1, myservo2, myservo3;

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  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);
    delay(10);

    }
    
    if (realservo >=2000 && realservo <2180){
      int servo2 = realservo;
      servo2 = map(servo2,2000,2180,0,180);
      myservo2.write(servo2);
      delay(10);
      
    }
    
    if (realservo >=3000 && realservo < 3180){
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180,0,180);
      myservo3.write(servo3);
      delay(10);
   } 
  }   
 }

SendText

The servo position is a number that does not fit in one byte. So we need two bytes to know the position of the servo. The read function reads 1 byte. When there are 2 bytes or more in the buffer, the first byte is written to the variable "servopos", the second byte to the variable "servopos1". The two bytes are then mathematically combined to form the original servo position. And this number is displayed in the serial monitor.

i see how it works now, could you provide an example for it?

thanks in advance :slight_smile:

In the first post I can see that you are using the "Send4ByteNumber" block. Use the "Send2ByteNumber" block instead.

sure, i'll try that

In the slider, set minValue to 1000 to check the conditions in the arduino code.

the first slider?

Oh, I didn't see you had multiple sliders there. So leave it in its original state.

i'm sorry i didn't make that visible

btw i have noticed that the servos are jittery, any solution to that?

and also if i connect the board to dc 5v it doesnt receive bluetooth commands, i need to connect the board to my pc to receive bluetooth signals, any fix for that?..

This is no longer a problem with AppInventor. You need to write good, reliable arduino code. It's best to seek help on the Arduino forums. We don't have the proper equipment to test your code.

Aright then, thanks for the help you have provided me, cheers!