用android和arduino控制伺服

时间:2013-09-26 21:14:57

标签: android bluetooth controls arduino

我正在尝试使用Android手机和arduino通过蓝牙控制伺服,但arduino中的数据作为char接收,并且arduino不知道何时接收到所有数据并且伺服没有移动得当。 arduino代码是这样的:     #include

Servo myservo;

char val; // variable to receive data from the serial port
int ledpin = 13; // LED connected to pin 48 (on-board LED)

void setup() {

  pinMode(ledpin, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  Serial.begin(9600);       // start serial communication at 9600bps
    myservo.attach(9);  
}

void loop() {

  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }
   if( val - 0 >= 0 && val - 0 <= 180){
 }
   // Serial.print("Recibido");

  delay(100);                    // wait 100ms for next reading
}    

如何从“val”中获取正确的值来控制伺服?

1 个答案:

答案 0 :(得分:0)

如果需要,您可以发送多个字符。将每个新char简单存储到一个数组中,并编程你的android程序,发送一个特定的字节,表示val读数的结束。

每次执行从串行读取时检查此“结束”字节将告诉您此char是要存储还是将先前存储的字符转换为整数或您需要的任何数据类型。

简单地转换该数组将告诉您正确的val。