gpt4 book ai didi

带字符的Arduino开关

转载 作者:行者123 更新时间:2023-12-03 20:19:03 26 4
gpt4 key购买 nike

我有一个 switch 语句,但似乎不识别字符 C 总是打印默认值

void setup() {
Serial.begin(9600);
Serial.println("Serial conection started, waiting for instructions...");
}
String serialReceived;
char commandChar[1];

void loop() {

if(Serial.available() > 0) {

serialReceived = Serial.readStringUntil('\n');
serialReceived.substring(0,1).toCharArray(commandChar, 1);


switch (commandChar[0]) {
case 'C':
Serial.print("Arduino Received C");
break;
default:
Serial.print("default");
}

}
}

最佳答案

这段代码似乎做你想做的:

void setup() {
Serial.begin(9600);
Serial.println("Serial conection started, waiting for instructions...");
}

String serialReceived;
char commandChar;

void loop() {

if(Serial.available() > 0) {

serialReceived = Serial.readStringUntil('\n');
commandChar = serialReceived.charAt(0);

switch (commandChar) {
case 'C':
Serial.print("Arduino Received C");
break;
default:
Serial.print("default");
}

}
}

鉴于您只想要一个 char ,我把 commandChar的类型改了并使用了 charAt String的功能类(class)。

如果这有帮助,请告诉我。

关于带字符的Arduino开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com