【发布时间】:2013-09-26 21:14:56
【问题描述】:
我正在尝试通过蓝牙用安卓手机和 arduino 控制伺服,但 arduino 中的数据以 char 形式接收,而 arduino 不知道何时收到所有数据而伺服不'正确移动。 arduino 代码是这样的: #包括
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”中获取正确的值来控制伺服?
【问题讨论】:
-
val的预期范围是多少?是 0 到 255(一个 8 位字节)吗? -
@Robert Harvey val 是以度为单位的伺服位置,我应该减去 - '0' 在任何字符上得到数字,但显然代码是错误的
标签: android bluetooth controls arduino