我在尝试编写一个 Arduino 程序以读取通过串行连接从处理发送的命令时遇到一个小问题。处理应该通过串行连接向 Arduino 发送“H”或“L”。该值在我的程序中存储为 char,但是当我尝试执行 Serial.write("Value: %c",val) 时,我收到“从 'const char*' 到 'const uint8_t*' 的无效转换”错误。如果有人可以帮助我解决这个问题,那就太好了。我真的需要弄清楚这个值是什么,这样我才能重新编写我的程序。谢谢!
代码如下:
char val; // variable to receive data from the serial port
int ledpin = 8; // 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
}
void loop() {
while (Serial.available()>0){
val=Serial.read();
}
//Serial.write("Value: %c",(char)val);
if( val == 'H' ) // if 'H' was received
{
Serial.write("Setting Value to High \n");
digitalWrite(ledpin, HIGH); // turn ON the LED
} else {
Serial.write("Setting Value to Low \n");
digitalWrite(ledpin, LOW); // otherwise turn it OFF
}
Serial.flush();
delay(100); // wait 100ms for next reading
}