如何独立检查多个变量?
How to independently check more than one variable?
这是代码中有错误的部分:
int value = analogRead(LM351);
float Temperature = value * 500.0 / 1023.0;
lcd.setCursor(6,0);
lcd.print(Temperature);
lcd.setCursor(11,1);
int value1 = analogRead(LM352);
float Humidity = value1 * 500.0 / 1023.0;
lcd.setCursor(10,1);
lcd.print(Humidity);
if (Temperature > 24){
digitalWrite(motor, HIGH);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
}
else {
digitalWrite(motor, LOW);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
lcd.print("");
}
if (Humidity > 10){
digitalWrite(motor, HIGH);
lcd.print("");
}
else {
digitalWrite(motor, LOW);
lcd.print("");
}
delay(1000);
}
按道理,如果温度超过24或湿度超过10,电机应该旋转。但是当我运行这段代码时,电机只有在湿度超过10时才会旋转。但是湿度小于10,温度大于24,电机不转
这是因为我正在检查一个又一个变量,有没有一种方法可以让我独立地检查每个变量?
您可以在第一个 if-else 语句中包含这两个条件:
if (Temperature > 24 || Humidity > 10){
digitalWrite(motor, HIGH);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
}
else {
digitalWrite(motor, LOW);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
lcd.print("");
}
这里的问题是两个语句都在 if...else 子句中。
让我们以温度高于 24 度且湿度低于 10 度为例。
程序进入温度if块,检查条件,通过,所以执行if子句内的块,如下:
if (Temperature > 24){
digitalWrite(motor, HIGH);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
接下来它检查湿度 if 子句中的条件,它没有通过,因为湿度低于 10,所以它执行 else 块:
else {
digitalWrite(motor, LOW);
lcd.print("");
}
因此电机确实开始旋转,但几乎立即被湿度 if...else 子句中的 else 块停止。
要解决此问题,您需要重构代码以使用 OR ( || ) 检查这两个条件。
If ( Temperature > 24 || Humidty > 10 )
{
//spin
if ( Temperature > 24 )
{
//set LEDs
}
}
else
{
// do not spin
}
您还需要一个嵌套的 if 子句来仅在温度 >24 的情况下设置 LED。
这样每次都会执行代码
Temparature > 24 or Humidty > 10
你的逻辑检查一件事并做出反应,然后检查另一件事。
您可以同时检查两件事,确定 4 种情况中的一种,然后做出反应,
或检查一件接一件,但只做决定而不是立即做出反应,然后再做出反应。
我想你问的是单独检查,所以去做决定吧。
// no decisions yet
bool NeedMotor = false;
if (Temperature > 24)
{
NeedMotor = true;
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
} else
{
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
lcd.print("");
}
if (Humidity > 10){
NeedMotor = true;
lcd.print("");
} else
{
lcd.print("");
}
if (NeedMotor)
{
digitalWrite(motor, HIGH);
} else
{
digitalWrite(motor, LOW);
}
这样,湿度决定不会覆盖温度决定,如果温度尚未引起,它可能会决定使用电机。如果需要电机将打开,否则将关闭。
请注意,我不确定液晶打印的目的。我把它留在了你的代码中。
我将从反转逻辑开始,条件的两个共同点是当它们低于阈值时它们都关闭,如果满足条件但两者都不低于阈值则打开风扇,并且然后有一个嵌套的条件语句来检查它是否是温度。
免责声明:可能有更简洁的方法。
//If the temperature or humidity are below threshold then
//turn off the motor and the red LED, and turn on the green LED
if(Temperature <= 24 && Humidity <= 10)
{
digitalWrite(motor, LOW);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
}
else //the temperature or humidity are above threshold
{
//turn on the motor
digitalWrite(motor, HIGH);
//if the temperature is above threshold turn off the Green LED
//and turn on the red led
if (Temperature > 24)
{
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
}
//if the humidity and temperature are both above threshold and the
//temperature falls back below threshold, invert the LEDs.
else
{
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
}
//clear the LCD
lcd.print("");
}
您从字面上描述了所需的代码,但没有在实际代码中实现它。
bool MotorRequired = (Temperature > 24) || (Humidity >10);
或分开:
bool MotorRequired = (Temperature > 24);
MotorRequired = MotorRequired || (Humidity >10);
到达可以使用电机的点时
if(MotorRequired)
// power motor on
else
// power motor off
哪个可以更短无分支:
digitalWrite(motor, (MotorRequired) ? HIGH : LOW );
这和
一样
if(Temperature > 24 || Humidity >10)
所以整个逻辑看起来像
digitalWrite(motor, (Temperature > 24 || Humidity >10) ? HIGH : LOW );
这些条件是独立的,但你的行动取决于它们,所以你必须对两者进行逻辑运算。
这是代码中有错误的部分:
int value = analogRead(LM351);
float Temperature = value * 500.0 / 1023.0;
lcd.setCursor(6,0);
lcd.print(Temperature);
lcd.setCursor(11,1);
int value1 = analogRead(LM352);
float Humidity = value1 * 500.0 / 1023.0;
lcd.setCursor(10,1);
lcd.print(Humidity);
if (Temperature > 24){
digitalWrite(motor, HIGH);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
}
else {
digitalWrite(motor, LOW);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
lcd.print("");
}
if (Humidity > 10){
digitalWrite(motor, HIGH);
lcd.print("");
}
else {
digitalWrite(motor, LOW);
lcd.print("");
}
delay(1000);
}
按道理,如果温度超过24或湿度超过10,电机应该旋转。但是当我运行这段代码时,电机只有在湿度超过10时才会旋转。但是湿度小于10,温度大于24,电机不转
这是因为我正在检查一个又一个变量,有没有一种方法可以让我独立地检查每个变量?
您可以在第一个 if-else 语句中包含这两个条件:
if (Temperature > 24 || Humidity > 10){
digitalWrite(motor, HIGH);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
}
else {
digitalWrite(motor, LOW);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
lcd.print("");
}
这里的问题是两个语句都在 if...else 子句中。
让我们以温度高于 24 度且湿度低于 10 度为例。
程序进入温度if块,检查条件,通过,所以执行if子句内的块,如下:
if (Temperature > 24){
digitalWrite(motor, HIGH);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
接下来它检查湿度 if 子句中的条件,它没有通过,因为湿度低于 10,所以它执行 else 块:
else {
digitalWrite(motor, LOW);
lcd.print("");
}
因此电机确实开始旋转,但几乎立即被湿度 if...else 子句中的 else 块停止。
要解决此问题,您需要重构代码以使用 OR ( || ) 检查这两个条件。
If ( Temperature > 24 || Humidty > 10 )
{
//spin
if ( Temperature > 24 )
{
//set LEDs
}
}
else
{
// do not spin
}
您还需要一个嵌套的 if 子句来仅在温度 >24 的情况下设置 LED。
这样每次都会执行代码
Temparature > 24 or Humidty > 10
你的逻辑检查一件事并做出反应,然后检查另一件事。
您可以同时检查两件事,确定 4 种情况中的一种,然后做出反应,
或检查一件接一件,但只做决定而不是立即做出反应,然后再做出反应。
我想你问的是单独检查,所以去做决定吧。
// no decisions yet
bool NeedMotor = false;
if (Temperature > 24)
{
NeedMotor = true;
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
lcd.print("");
} else
{
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
lcd.print("");
}
if (Humidity > 10){
NeedMotor = true;
lcd.print("");
} else
{
lcd.print("");
}
if (NeedMotor)
{
digitalWrite(motor, HIGH);
} else
{
digitalWrite(motor, LOW);
}
这样,湿度决定不会覆盖温度决定,如果温度尚未引起,它可能会决定使用电机。如果需要电机将打开,否则将关闭。
请注意,我不确定液晶打印的目的。我把它留在了你的代码中。
我将从反转逻辑开始,条件的两个共同点是当它们低于阈值时它们都关闭,如果满足条件但两者都不低于阈值则打开风扇,并且然后有一个嵌套的条件语句来检查它是否是温度。
免责声明:可能有更简洁的方法。
//If the temperature or humidity are below threshold then
//turn off the motor and the red LED, and turn on the green LED
if(Temperature <= 24 && Humidity <= 10)
{
digitalWrite(motor, LOW);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
}
else //the temperature or humidity are above threshold
{
//turn on the motor
digitalWrite(motor, HIGH);
//if the temperature is above threshold turn off the Green LED
//and turn on the red led
if (Temperature > 24)
{
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
}
//if the humidity and temperature are both above threshold and the
//temperature falls back below threshold, invert the LEDs.
else
{
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen, HIGH);
}
//clear the LCD
lcd.print("");
}
您从字面上描述了所需的代码,但没有在实际代码中实现它。
bool MotorRequired = (Temperature > 24) || (Humidity >10);
或分开:
bool MotorRequired = (Temperature > 24);
MotorRequired = MotorRequired || (Humidity >10);
到达可以使用电机的点时
if(MotorRequired)
// power motor on
else
// power motor off
哪个可以更短无分支:
digitalWrite(motor, (MotorRequired) ? HIGH : LOW );
这和
一样if(Temperature > 24 || Humidity >10)
所以整个逻辑看起来像
digitalWrite(motor, (Temperature > 24 || Humidity >10) ? HIGH : LOW );
这些条件是独立的,但你的行动取决于它们,所以你必须对两者进行逻辑运算。