C++ Arduino编程如果出现其他错误
我的代码在else块中出现问题。在没有明显原因的情况下,它先执行第一个模块为继电器供电,然后跳到其他模块C++ Arduino编程如果出现其他错误,c++,arduino,C++,Arduino,我的代码在else块中出现问题。在没有明显原因的情况下,它先执行第一个模块为继电器供电,然后跳到其他模块 int relayValue = 0; #define delays 10 void loop() { if(relayValue = 1) { analogWrite(relay, 255); delay(delays); relayValue = 0; } if (relayValue = 0) { analogWrite(relay, 0)
int relayValue = 0;
#define delays 10
void loop() {
if(relayValue = 1) {
analogWrite(relay, 255);
delay(delays);
relayValue = 0;
}
if (relayValue = 0) {
analogWrite(relay, 0);
delay(delays);
relayValue = 1;
} else {
analogWrite(fan, 255);
relayValue = 0;
}
}
任何建议都很好。要比较if语句中的两个值,请使用两个等号 错误
if(relayValue = 1)
正确
if(relayValue == 1)
一个“=”符号是给变量赋值
int x = 5 //Variable x with type 'Integer' has value 5
x == 6 //Comparison: Does x equals to 6?? Will return false because x is 5
这是因为语法错误。 你已经使用了If-then-If。它应该是:If..else If..else 此外,“=”应替换为“=”。符号“=”指定变量值,如果变量值相等,则进行“==”测试 更改: 致: 语法() C++中if if…否则if…语句的语法−
if(boolean_expression 1) {
// Executes when the boolean expression 1 is true
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true
} else {
// executes when the none of the above condition is true.
}
=
是赋值,=
是比较。啊,好吧,修复起来真他妈的简单,我没有做:)如果你将警告级别设置得足够高,编译器可能会为你捕获它。使用g++,-Wall
应该可以做到这一点。描述得非常糟糕。例如,//当布尔表达式2为真时执行
忽略了一个非常重要的细节:如果布尔表达式1为真,则不会执行。这是tutorailspoint示例中的内容。这并不是说我编造了他的if语句是“正确的”,在这种情况下,他不需要使用else。如果relayValue=0,则仅执行If Station块,而不执行其他块。
else if (relayValue = 0)
if(boolean_expression 1) {
// Executes when the boolean expression 1 is true
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true
} else {
// executes when the none of the above condition is true.
}