-1

So I'm working with a DHT11 Therm/Humidity sensor. I've got it all to work, I'm just trying to mitigate the output if there is no change. Prior to adding this line, there was no issue. If you can show me my error rather than telling me a better way to do it, it would be most appreciated. This is merely for my education to know what went wrong.

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. 

The entire error message and code is available below.

Code:

    #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++;


}

Error msgs:

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.
Nate
  • 15
  • 5
  • 4
    `if (entire expression belongs INSIDE parens)` - you have a `!` outside your expression. it should be `if(!(yourexpression))` – WhozCraig Mar 31 '16 at 16:11

4 Answers4

3

You need another set of parentheses

if(!(f > priorT + 2 && f < priorT -2 && i = 1))
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

You are missing external parentheses, in your code should be:

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. 

You can also avoid two levels of parentheses allaying the De Morgan's Law. For example:

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.  

You can take a look also to C++ Operator Precedence for more information.

Hope this helps.

Fabricio
  • 3,248
  • 2
  • 16
  • 22
  • Thank you both for your input. After researching this issue I already tried your method of additional parenthesis. Still I get an error message. 'code'Using library DHT in folder: C:\Program Files (x86)\Arduino\libraries\DHT (legacy) exit status 1 lvalue required as left operand of assignment'code' – Nate Mar 31 '16 at 21:37
  • Sorry I can't seem to get the 'code' part... But im not sure if its the arduino software causing this issue. – Nate Mar 31 '16 at 21:45
0
if (!(f > priorT + 2 || f < priorT -2 || i == 1))

was the solution. As you all told me.

You were right fabrico about the OR statements. They were necessary. My issue was also the i = 1 needed to be i == 1 duh. Thanks guys.

Nate
  • 15
  • 5
0

Regarding the compiler error, your code

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

was understood by the compiler as:

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

(see the link Fabricio provided, regarding operator precedence). Changing the assignment to a comparison fixed both the logic error AND the syntax error, because a comparison operator (==), contrary to an assignment operator (=) has higher priority than a logical operator (&&, ||).

The reason the compiler complained was this: your expression was interpreted as follows:

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

That is, the compiler thought you were trying to assign 1 to an expression, which can not be done. This is the meaning of error messages complaining about something NOT being an lValue. (around 30 years ago, I was dumbfounded by a similar error message. I called a friend who was more experienced than me, and asked him what an lValue is. His reply: "you are doing something like 5=x. Bye!").

Alphonsos_Pangas
  • 514
  • 1
  • 3
  • 13