[Solved] control servo using android


  1. Read the data as string using: string input = bluetooth.readString();
  2. Then convert string to int using: int servopos = int(input);
  3. Then write the position to the servo:servo.write(servopos);

Now depending on the data you send from android, you could need to :
Trim it: input = input.trim();
Or constrain it : servopos = constrain(servopos,0,180);

Your corrected code:

#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo;
int bluetoothTx = 10;
int bluetoothRx = 11;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
  servo.attach(9);
  Serial.begin(9600);
  bluetooth.begin(9600);
} 

void loop() {
  //read from bluetooth and wrtite to usb serial
  if (bluetooth.available() > 0 ) {
    String s = bluetooth.readString();
    s.trim();
    float servopos = s.toFloat();
    servopos = constrain(servopos, 0, 180);
    Serial.println("Angle: "+String(servopos));
    servo.write(servopos);
  }
}

3

solved control servo using android