我正在尝试通过蓝牙用安卓手机和 arduino 控制伺服,但是 arduino 中的数据以字符形式接收,而 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”中获得正确的值来控制伺服?