0

I send a command to move the servo through Android, then the servo sends the position to Android via Bluetooth. How to read the servo position and send it to Android for a TextView?

This is the code I use. Value 100 to turn on and 150 to turn off the led because 1-90 is used for servo. How to post servo position when first connected with Android?

#include <SoftwareSerial.h>
#include <Servo.h>

Servo servo;

int bluetoothTx = 10;
int bluetoothRx = 11;
int data ;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() 

{
  pinMode(13, OUTPUT);
  servo.attach(9);
  bluetooth.begin(9600); 
  delay(100);
}


void loop() 
{
  if(bluetooth.available() > 0)  
  { 
    data = bluetooth.read();

    int servopos;
    int servopos1;
    servopos = data;
    servopos1 = data + 9;

    if(data == 100){            
    digitalWrite(13, HIGH); 
    }else if(data == 150){       
    digitalWrite(13, LOW);                               
    }else if (data > 100){ 
    Serial.println(servopos);
    servo.write(' ');
    }else if (data < 100){
    Serial.println(servopos);
    servo.write(servopos1);

}
  }
} 
Floern
  • 33,559
  • 24
  • 104
  • 119
Rifki Arif
  • 13
  • 2

1 Answers1

0

What kind of servo are you using?
If it's an analog servo, you cannot get the servo position, but you can save the servo state to a variable you can then provide to your android app.

Also, the closest way to store a correct value of the (analog) servo position is to send it to a min or max position (assuming it's not a continuously and freely rotating servo) and then store any change made to the servo position.
It's rather empyrical but it should do the trick.

To get information from the arduino, have you tried the SoftwareSerial.print() method?

peyo
  • 442
  • 3
  • 9