# bluetooth.available()

**URL:** <https://forum.arduino.cc/t/bluetooth-available/601037>\
**Category:** Software\
**Created:** [July 11, 2019, 2:34am UTC](https://forum.arduino.cc/t/bluetooth-available/601037 "2019-07-11T02:34:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hop99](https://avatars.discourse-cdn.com/v4/letter/h/898d66/32.png) [@hop99](https://forum.arduino.cc/u/hop99)\
**Post date:** [July 11, 2019, 2:34am UTC](https://forum.arduino.cc/t/bluetooth-available/601037/1 "2019-07-11T02:34:33Z")

</div>

Hola.  
Quisiera saber que es lo que se entiende por una parte del siguiente código.(cabe resaltar que es para el control de servomotores por comunicación bluetooth por celular). GRACIAS.

PARTE DEL CÓDIGO:

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

CÓDIGO TOTAL:

#include \<SoftwareSerial.h\> // TX RX software library for bluetooth

#include \<Servo.h\> // servo library  
Servo myservo1, myservo2, myservo3; // servo name

int bluetoothTx = 2; // bluetooth tx to 10 pin  
int bluetoothRx = 5; // bluetooth rx to 11 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()  
{  
myservo1.attach(6); // attach servo signal wire to pin 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);  
Serial.println("Servo 1 ON");  
delay(10);  
}  
if (realservo \>= 2000 && realservo \<2180) {  
int servo2 = realservo;  
servo2 = map(servo2, 2000, 2180, 0, 180);  
myservo2.write(servo2);  
Serial.println("Servo 2 ON");  
delay(10);  
}  
if (realservo \>= 3000 && realservo \<3180) {  
int servo3 = realservo;  
servo3 = map(servo3, 3000, 3180, 0, 180);  
myservo3.write(servo3);  
Serial.println("Servo 3 ON");  
delay(10);  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Kike\_GL](https://avatars.discourse-cdn.com/v4/letter/k/94ad74/32.png) [@Kike\_GL](https://forum.arduino.cc/u/Kike_GL)\
**Post date:** [July 18, 2019, 11:14pm UTC](https://forum.arduino.cc/t/bluetooth-available/601037/2 "2019-07-18T23:14:54Z")

</div>

Por favor edita tu mensaje y mete todo tu código entre las etiquetas **code**.

Luego te recomiendo leer sobre [Serial.read()](https://arduwiki.perut.org/index.php/Serial.read()) y veras que lee un byte a la vez, asi que lo que intentas hacer aquí es construir un int (2 bytes) a partir de 2 bytes separados:

```cpp
char servopos = bluetooth.read();
char servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 * 256) + servopos;

```

---

<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:** [May 7, 2021, 12:36am UTC](https://forum.arduino.cc/t/bluetooth-available/601037/3 "2021-05-07T00:36:02Z")

</div>

Closed on May 7, 2021, 12:36am UTC


