为什么 Arduino 在通过串行接收字节时没有按预期通过 if/then/elseif?
Why is Arduino not going through the if/then/elseif as expected when receiving bytes over serial?
我正在做一个项目,Arduino 应该根据通过串行接收的不同字符(a
、b
等)做不同的事情。我已经让它做出回应,但我遇到了逻辑错误。当我输入 a
时,它会按照要求执行软复位程序(通过电阻将复位引脚接地以远程复位自身)。但是,当我执行 b
(或 30 多个其他字符中的任何其他字符,其中 none 是相同的)时,它会执行 a
命令。根据我的逻辑,自 incomingByte != 'a'
以来,它应该不会执行 a
的代码,但它会执行 = b
。为什么是 运行 a
而不是其他?感谢您的帮助。
P.S。我知道还有其他方法可以做到这一点,可能更多 'proper,' 但我想尽可能坚持这种 if/then/elseif 逻辑。
我附上了下面的一些代码,还有很多所以我知道最后某处可能缺少 }
,但它在代码中。
void loop() {
char incomingByte;
//wait for commands
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
bool alarmState = false;
//variable for storing status of the alarm system for use during unsilence controls, to ensure operator cannot
//unsilence a non-alarmed system and cause audible signals to activate prematurely
//use incomingByte as the command byte in ain if/then/elseif
if (incomingByte = "a") {
//code to soft-reset
//output reset confirmation
tone(buzzer, 600, 500);
Serial.println("System reset in progress...");
delay(100);
digitalWrite(powerLED, LOW);
delay(500);
digitalWrite(powerLED, HIGH);
delay(500);
digitalWrite(powerLED, LOW);
delay(500);
digitalWrite(powerLED, HIGH);
delay(500);
digitalWrite(powerLED, LOW);
delay(500);
digitalWrite(powerLED, HIGH);
delay(500);
digitalWrite(powerLED, LOW);
delay(250);
digitalWrite(resetPin, LOW);
digitalWrite(troubleLED, HIGH);
delay(100);
digitalWrite(resetPin, HIGH); //this is needed to complete the reset
}
else if (incomingByte = "b") {
//code to silence audible signals
if (alarmState = true) {
digitalWrite(NAC1, LOW);
digitalWrite(silenceLED, HIGH);
digitalWrite(silence1, HIGH);
Serial.println("Audible signals silenced");
}
else if (alarmState = false) {
Serial.println("The system is not in alarm and the NAC state could not be modified. If you need to manually override NAC states without system alarm, use the System menu.");
}
}
else if (incomingByte = "c") {
//code to unsilence audible signals
if (alarmState = true) {
digitalWrite(NAC1, HIGH);
digitalWrite(silenceLED, LOW);
digitalWrite(silence1, LOW);
Serial.println("Audible signals unsilenced");
}
else if (alarmState = false) {
Serial.println("The system is not in alarm and the NAC state could not be modified. If you need to manually override NAC states without system alarm, use the System menu.");
}
您在这里所做的不是比较,而是始终 return 为真的赋值。
if (incomingByte = "a") {
c++中的比较应该按以下方式进行
if (incomingByte == 'a') {
这也适用于您正在比较的其他字符和布尔数据。
进行此更改,您的代码将进入正确的 if else 条件。
我正在做一个项目,Arduino 应该根据通过串行接收的不同字符(a
、b
等)做不同的事情。我已经让它做出回应,但我遇到了逻辑错误。当我输入 a
时,它会按照要求执行软复位程序(通过电阻将复位引脚接地以远程复位自身)。但是,当我执行 b
(或 30 多个其他字符中的任何其他字符,其中 none 是相同的)时,它会执行 a
命令。根据我的逻辑,自 incomingByte != 'a'
以来,它应该不会执行 a
的代码,但它会执行 = b
。为什么是 运行 a
而不是其他?感谢您的帮助。
P.S。我知道还有其他方法可以做到这一点,可能更多 'proper,' 但我想尽可能坚持这种 if/then/elseif 逻辑。
我附上了下面的一些代码,还有很多所以我知道最后某处可能缺少 }
,但它在代码中。
void loop() {
char incomingByte;
//wait for commands
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
bool alarmState = false;
//variable for storing status of the alarm system for use during unsilence controls, to ensure operator cannot
//unsilence a non-alarmed system and cause audible signals to activate prematurely
//use incomingByte as the command byte in ain if/then/elseif
if (incomingByte = "a") {
//code to soft-reset
//output reset confirmation
tone(buzzer, 600, 500);
Serial.println("System reset in progress...");
delay(100);
digitalWrite(powerLED, LOW);
delay(500);
digitalWrite(powerLED, HIGH);
delay(500);
digitalWrite(powerLED, LOW);
delay(500);
digitalWrite(powerLED, HIGH);
delay(500);
digitalWrite(powerLED, LOW);
delay(500);
digitalWrite(powerLED, HIGH);
delay(500);
digitalWrite(powerLED, LOW);
delay(250);
digitalWrite(resetPin, LOW);
digitalWrite(troubleLED, HIGH);
delay(100);
digitalWrite(resetPin, HIGH); //this is needed to complete the reset
}
else if (incomingByte = "b") {
//code to silence audible signals
if (alarmState = true) {
digitalWrite(NAC1, LOW);
digitalWrite(silenceLED, HIGH);
digitalWrite(silence1, HIGH);
Serial.println("Audible signals silenced");
}
else if (alarmState = false) {
Serial.println("The system is not in alarm and the NAC state could not be modified. If you need to manually override NAC states without system alarm, use the System menu.");
}
}
else if (incomingByte = "c") {
//code to unsilence audible signals
if (alarmState = true) {
digitalWrite(NAC1, HIGH);
digitalWrite(silenceLED, LOW);
digitalWrite(silence1, LOW);
Serial.println("Audible signals unsilenced");
}
else if (alarmState = false) {
Serial.println("The system is not in alarm and the NAC state could not be modified. If you need to manually override NAC states without system alarm, use the System menu.");
}
您在这里所做的不是比较,而是始终 return 为真的赋值。
if (incomingByte = "a") {
c++中的比较应该按以下方式进行
if (incomingByte == 'a') {
这也适用于您正在比较的其他字符和布尔数据。
进行此更改,您的代码将进入正确的 if else 条件。