[英]If statements not working as expected on ESP32
I am having a bit of puzzling situation here with my experiment.我的实验在这里遇到了一些令人费解的情况。 I am simply measuring the soil moisture using a capacitive sensor and reading via analog pin of nodemcu.
我只是使用电容传感器测量土壤湿度并通过 nodemcu 的模拟引脚读取。 Afterwards, I am categorizing soil condition as wet, mid-level or dry.
之后,我将土壤状况分类为潮湿、中等或干燥。 I want to use simple if-else statements for this.
我想为此使用简单的 if-else 语句。 But, for some reason they arent working as intended.
但是,由于某种原因,它们没有按预期工作。 Only first if is executed, the condition is satisfied when it shouldnt...
只有第一个 if 被执行,当它不应该满足条件时...
const int SOIL_MOISTURE_DATA_NOT_FOUND = 0;
const int SOIL_TOO_WET = 1;
const int SOIL_MID_LEVEL_MOISTURE = 2;
const int SOIL_TOO_DRY = 3;
When I use else if statements, it executes only first statement and returns..当我使用 else if 语句时,它只执行第一个语句并返回..
int soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
int rawHumiditySensorValue = analogRead(A0);
Serial.print("Moisture value : ");
Serial.println(rawHumiditySensorValue);
if( 500 < rawHumiditySensorValue <= 750 )
{
Serial.println("SOIL_TOO_WET");
soilStatus = SOIL_TOO_WET;
}
else if( 750 < rawHumiditySensorValue <= 820 )
{
soilStatus = SOIL_MID_LEVEL_MOISTURE;
Serial.println("SOIL_MID_LEVEL_MOISTURE");
}
else if( 820 < rawHumiditySensorValue <= 920 )
{
soilStatus = SOIL_TOO_DRY;
}
// Invalid reading / no reading cases:
else if( rawHumiditySensorValue < 500 || rawHumiditySensorValue > 1000 )
{
soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
}
Serial.print("Soil Status :");
Serial.println(soilStatus);
Output from serial monitor: Output 来自串行监视器:
I removed if else, all the if statements are getting executed regardless of the variable value我删除了 if else,无论变量值如何,所有 if 语句都将被执行
int soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
int rawHumiditySensorValue = analogRead(A0); //readAnalogDataFromHydroSensor(pinID);
Serial.print("Moisture value : ");
Serial.println(rawHumiditySensorValue);
if( 500 < rawHumiditySensorValue <= 750 )
{
Serial.println("SOIL_TOO_WET");
soilStatus = SOIL_TOO_WET;
}
if( 750 < rawHumiditySensorValue <= 820 )
{
soilStatus = SOIL_MID_LEVEL_MOISTURE;
Serial.println("SOIL_MID_LEVEL_MOISTURE");
}
if( 820 < rawHumiditySensorValue <= 920 )
{
soilStatus = SOIL_TOO_DRY;
}
// Invalid reading / no reading cases:
if( rawHumiditySensorValue < 500 || rawHumiditySensorValue > 1000 )
{
soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
}
Serial.print("Soil Status :");
Serial.println(soilStatus);
I get following output on serial monitor:我在串行监视器上关注 output:
What am I missing here?我在这里想念什么? I checked the datatype of all local vars, all are set to int.
我检查了所有本地变量的数据类型,都设置为 int。
The relational operators are binary operators with left-to-right associativity.关系运算符是具有从左到右关联性的二元运算符。 The result is 0 or 1.
结果为 0 或 1。
The expression 500 < rawHumiditySensorValue <= 750
is equivalent to (500 < rawHumiditySensorValue) <= 750
.表达式
500 < rawHumiditySensorValue <= 750
等价于(500 < rawHumiditySensorValue) <= 750
。 Since the result of (500 < rawHumiditySensorValue)
will be 0 or 1, and since both those values are less than or equal to 750, the result of the overall expression is 1.由于
(500 < rawHumiditySensorValue)
的结果将为 0 或 1,并且由于这两个值都小于或等于 750,因此整体表达式的结果为 1。
What you probably wanted is 500 < rawHumiditySensorValue && rawHumiditySensorValue <= 750
.您可能想要的是
500 < rawHumiditySensorValue && rawHumiditySensorValue <= 750
。
You could simplify the use of relational operators in your code using else if
:您可以使用
else if
来简化代码中关系运算符的使用:
// Invalid reading / no reading cases:
if( rawHumiditySensorValue < 500 || rawHumiditySensorValue > 1000 )
{
soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
}
else if( rawHumiditySensorValue <= 750 )
{
soilStatus = SOIL_TOO_WET;
}
else if( rawHumiditySensorValue <= 820 )
{
soilStatus = SOIL_MID_LEVEL_MOISTURE;
}
else if( rawHumiditySensorValue <= 920 )
{
soilStatus = SOIL_TOO_DRY;
}
else
{
soilStatus = SOIL_FAR_TOO_DRY; /* Original code did not handle this case! */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.