简体   繁体   English

如何独立检查多个变量?

[英]How to independently check more than one variable?

This is the part of the code with the error:这是有错误的代码部分:

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

By right, the motor should spin if the temperature is more than 24 or humidity is more than 10. But when I ran this code, the motor only spun if the humidity was more than 10. But when humidity was less than 10 and temperature was more than 24, motor did not spin.正确的是,如果温度超过 24 或湿度超过 10,电机应该旋转。但是当我运行这段代码时,电机只有在湿度超过 10 时才会旋转。但是当湿度小于 10 并且温度为超过24,电机不转。

The reason for this is as I am checking one variable after the other, is there a way in which I can check each variable independentaly from the other?这样做的原因是当我一个接一个地检查一个变量时,有没有一种方法可以独立检查每个变量?

Your logic checks one thing and reacts, then checks the other thing.你的逻辑检查一件事并做出反应,然后检查另一件事。
You can either check both things at once, identify one among 4 situations and then react, or check one thing after the other, but only make decisions instead of reacting immediatly, then react later.您可以同时检查两件事,确定 4 种情况中的一种,然后做出反应,或者一个接一个地检查,但只做出决定,而不是立即做出反应,然后再做出反应。

I think you ask specifically about checking separatly, so go with decision making.我认为您特别询问有关单独检查的问题,因此 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);
}

This way the humidity decision does not overwrite the temperature decision, it just potentially decides to use the motor if the temperature did not cause that yet.这样,湿度决定不会覆盖温度决定,如果温度还没有导致它可能会决定使用电机。 If the motor is needed it will be switched on, otherwise off.如果需要电机,它将打开,否则关闭。

Note, I am unsure about the purpose of the lcd printing.请注意,我不确定液晶打印的目的。 I left it as it is in your code.我将其保留在您的代码中。

You could include both conditions in the first if-else statement:您可以在第一个 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("");
}

The issue here is that both statements are in if...else clause.这里的问题是这两个语句都在 if...else 子句中。

Let's take the case when temperature is above 24 and humidity is below 10.让我们以温度高于 24 且湿度低于 10 的情况为例。

The program enters temperature if block, checks the condition, it passes, so it executes the block within the if clause, the one below:程序进入温度 if 块,检查条件,它通过,因此它执行 if 子句中的块,如下所示:

if (Temperature > 24){
    digitalWrite(motor, HIGH);
    digitalWrite(LedRed, HIGH);
    digitalWrite(LedGreen, LOW);
    lcd.print("");

Next it checks the condition in humidity if clause, it does not pass because the humidity is below 10 so it executes the else block:接下来它检查湿度 if 子句中的条件,它没有通过,因为湿度低于 10,所以它执行 else 块:

 else {
    digitalWrite(motor, LOW);
    lcd.print("");
  }

So the motor does start spining but is almost immediatly stoped by the else block in humidity if...else clause.因此,电机确实开始旋转,但几乎立即被湿度中的 else 块 if...else 子句停止。

To fix this you would need to refactor the code to check both condition with OR ( || ).要解决此问题,您需要重构代码以使用 OR (||) 检查这两个条件。

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

You will also require a nested if clause to set the LEDs only in case the Temperature >24.您还需要一个嵌套的 if 子句来设置 LED,仅在温度 >24 的情况下。

This way the code will be executed everytime either这样代码每次都会被执行

Temparature > 24 or Humidty > 10

I would start by inverting logic, the two things that the conditions have in common is that they are both off when they are below threshold, if the condition is met when neither is below threshold then turn on the fans, and then have a nested conditional statement which checks if it's the temperature.我将从反转逻辑开始,条件的两个共同点是当它们低于阈值时它们都关闭,如果在两者都不低于阈值时满足条件,则打开风扇,然后有一个嵌套的条件检查是否是温度的语句。

Disclaimer: There is probably a cleaner way of doing this.免责声明:可能有一种更清洁的方法。

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

You literally described required code but you didn't implement it in actual code.您从字面上描述了所需的代码,但您没有在实际代码中实现它。

bool MotorRequired = (Temperature > 24) || (Humidity >10);

Or separately:或单独:

bool MotorRequired = (Temperature > 24);

MotorRequired = MotorRequired || (Humidity >10);

when reaching point where motor can be used当到达可以使用电机的点时

if(MotorRequired)
   // power motor on
else
   // power motor off

Which can be shorter branch-free:哪个可以更短,无分支:

digitalWrite(motor, (MotorRequired) ? HIGH : LOW );

That is just same as这和

if(Temperature > 24 || Humidity >10)

So whole logic looks like所以整个逻辑看起来像

digitalWrite(motor, (Temperature > 24 || Humidity >10) ? HIGH : LOW );

Those conditions are independent but your action depends on BOTH of them, so you have to perform logical operation on both.这些条件是独立的,但您的操作取决于它们两者,因此您必须对两者执行逻辑操作。

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

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