繁体   English   中英

如何独立检查多个变量?

[英]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,电机不转。

这样做的原因是当我一个接一个地检查一个变量时,有没有一种方法可以独立检查每个变量?

你的逻辑检查一件事并做出反应,然后检查另一件事。
您可以同时检查两件事,确定 4 种情况中的一种,然后做出反应,或者一个接一个地检查,但只做出决定,而不是立即做出反应,然后再做出反应。

我认为您特别询问有关单独检查的问题,因此 go 与决策。

// 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-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("");
  }

因此,电机确实开始旋转,但几乎立即被湿度中的 else 块 if...else 子句停止。

要解决此问题,您需要重构代码以使用 OR (||) 检查这两个条件。

If ( Temperature > 24 || Humidty > 10 )
{
    //spin
    if ( Temperature > 24 )
    {
        //set LEDs
    }
}
else
{
    // do not spin
}

您还需要一个嵌套的 if 子句来设置 LED,仅在温度 >24 的情况下。

这样代码每次都会被执行

Temparature > 24 or Humidty > 10

我将从反转逻辑开始,条件的两个共同点是当它们低于阈值时它们都关闭,如果在两者都不低于阈值时满足条件,则打开风扇,然后有一个嵌套的条件检查是否是温度的语句。

免责声明:可能有一种更清洁的方法。

//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 );

这些条件是独立的,但您的操作取决于它们两者,因此您必须对两者执行逻辑操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM