简单修复?预期'('之前'!'令牌

时间:2016-03-31 16:07:07

标签: c arduino rgb sensor temperature

所以我正在使用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.

4 个答案:

答案 0 :(得分:3)

您需要另一组括号

VS2013

答案 1 :(得分: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以获取更多信息。

希望这有帮助。

答案 2 :(得分:0)

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

是解决方案。正如大家都告诉我的那样。

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

答案 3 :(得分:0)

关于编译器错误,您的代码

if (f <= priorT + 2 || f >= priorT -2 || i = 1)
编译器将

理解为:

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

(请参阅Fabricio提供的链接,关于运营商优先级)。将赋值更改为比较修复了逻辑错误和语法错误,因为与赋值运算符(=)相反的比较运算符(==)具有比逻辑运算符(&amp;&amp;,||)更高的优先级。

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

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

也就是说,编译器认为您试图将1分配给表达式,这是无法完成的。这是错误消息抱怨不是lValue的意思。 (大约30年前,我被一个类似的错误信息吓到了。我打电话给一个比我更有经验的朋友,并告诉他什么是lValue。他的回答是:“你做的事情就像5 = x。再见!” )。