简单修复?预期 '(' 在 '!' 标记之前

Simple fix? expected '(' before '!' token

所以我正在使用 DHT11 Therm/Humidity 传感器。 我已经完成了所有工作,我只是想在没有变化的情况下减少输出。在添加此行之前,没有问题。如果您能告诉我我的错误而不是告诉我更好的方法,将不胜感激。这只是为了让我知道哪里出了问题。

if!(f > priorT + 2 && f < priorT -2 && i = 1)  //in a non-repetitive fashion dictated by the counter, if there isnt a large change, do not output. 

下面提供了完整的错误消息和代码。

代码:

    #include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

int BLED=9;
int GLED=10;
int RLED=11;

void setup(){

  Serial.begin(9600);

  delay(1000);//Delay to let system boot

  Serial.println("DHT11 Humidity & temperature Sensor\n\n");

  delay(500);//Wait before accessing Sensor

  // initializing output to RGB LED

  pinMode (BLED, OUTPUT);
  pinMode (GLED, OUTPUT);
  pinMode (RLED, OUTPUT);

  digitalWrite(RLED, 127);        //show that initialization is complete
  digitalWrite(GLED, 0);
  digitalWrite(BLED, 0);

  delay(200);

  digitalWrite(RLED, 0);
}//end "setup()"



void loop(){


  double f, priorT, currentT;  //variables for conversion and temperature change determination.
  int i = 0;                       //counter
  DHT.read11(dht_apin);

    f = DHT.temperature * 1.8 + 32;     //fahrenheit conversion from DHT.Temperature

    priorT = currentT;

    currentT = f;


    if!(f > priorT + 2 && f < priorT -2 && i = 1)  //in a non-repetitive fashion dictated by the counter, if there isnt a large change, do not output.
    {
      Serial.print("Awaiting status change.");
      i++;
    }

    else if(f > priorT + 2 && f < priorT -2)        //if there is a temperature change, output the change
    {
      Serial.print("Current humidity = ");

      Serial.print(DHT.humidity);

      Serial.print("%  ");

      Serial.print("temperature = ");

      Serial.print(DHT.temperature); 

      Serial.print("C  ");

      Serial.print(f);
      Serial.println(" F");
      i = 0;
    }
    if(f < 70 && f > 40)
    {
      digitalWrite(BLED, 90);
      digitalWrite(RLED, 0);
      digitalWrite(GLED, 0);
    }
    else if(f > 70 && f < 90)
    {
      digitalWrite(BLED, 0);
      digitalWrite(RLED, 0);
      digitalWrite(GLED, 127);
    }
    else if(f < 40)
    {
      digitalWrite(BLED, 127);
      digitalWrite(RLED, 0);
      digitalWrite(GLED, 0);

    }
    else if(f > 90)
    {
      digitalWrite(RLED, 127);
      digitalWrite(GLED, 0);
      digitalWrite(BLED, LOW);
    }

  delay(5000);//Wait 5 seconds before accessing sensor again.

  i++;


}

错误消息:

Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Xxwitherpoon\Documents\Arduino\Sensors\DHT11andRGBLED\DHT11andRGBLED.ino: In function 'void loop()':

DHT11andRGBLED:52: error: expected '(' before '!' token

     if!(f > priorT + 2 && f < priorT -2 && i = 1)  //in a non-repetitive fashion dictated by the counter, if there isnt a large change, do not output.

       ^

DHT11andRGBLED:58: error: 'else' without a previous 'if'

     else if(f > priorT + 2 && f < priorT -2)        //if there is a temperature change, output the change

     ^

exit status 1
expected '(' before '!' token

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

您需要另一组括号

if(!(f > priorT + 2 && f < priorT -2 && i = 1))

您缺少外部括号,在您的代码中应该是:

if (!(f > priorT + 2 && f < priorT -2 && i = 1))  //in a non-repetitive fashion dictated by the counter, if there isnt a large change, do not output. 

您还可以避免使用两级括号来缓解 De Morgan's Law。例如:

if (f <= priorT + 2 || f >= priorT -2 || i != 1)  //in a non-repetitive fashion dictated by the counter, if there isnt a large change, do not output.  

您也可以查看 C++ Operator Precedence 了解更多信息。

希望对您有所帮助。

if (!(f > priorT + 2 || f < priorT -2 || i == 1))

是解决方案。正如你们告诉我的那样。

关于 OR 语句,你是对的。他们是必要的。我的问题也是 i = 1 需要是 i == 1 duh。谢谢大家。

关于编译错误,你的代码

if (f <= priorT + 2 || f >= priorT -2 || i = 1)

被编译器理解为:

if (f <= (priorT + 2 || f) >= (priorT -2 || i) = 1)

(参见 link Fabricio 提供的关于运算符优先级的内容)。将赋值更改为比较修复了逻辑错误和语法错误,因为比较运算符 (==) 与赋值运算符 (=) 相反,其优先级高于逻辑运算符 (&&, ||).

编译器报错的原因是:你的表达式被解释如下:

if(!(..... (priorT -2 && i) = 1))

也就是说,编译器认为您试图将 1 赋给一个表达式,这是不可能的。这就是抱怨某些东西不是 lValue 的错误消息的含义。 (大约30年前,我被类似的错误信息吓傻了。我打电话给一个比我更有经验的朋友,问他什么是lValue。他的回答是:"you are doing something like 5=x. Bye!")。