Question about logic

Hello everyone,

I'm having a little issue trying to make this to work. Here is what I am trying to accomplish.

if (temperature < 80) || (temperature > 112 && ( humidity < 13)

Only when the above is true, I want something to happens. I have tried and tried, I cannot get it to work

Basically what I want is temperature to be between 80 and 112, and humidity less than 13, when that is true, then perform an else if statement.

I'm trying to program a HIH6130. This is about heat index.

Thanks.

if (temperature >= 80 && temperature <= 112  &&  humidity <= 13)
{
  // do stuff here if true
}

This is a if statement.
An else if would be if this isnt true and something else is

Let the compiler lead you in fixing your problem.
Start with this:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
 int temperature, humidity;

if (temperature < 80) || (temperature > 112  && ( humidity < 13)

 Serial.print("TRUE");
Serial.print("FALSE");
}
void loop()
{
}

The first compiler error message is little cryptic, so just replace the whole "if" line with

if ( (temperature < 80) || (temperature > 112 && ( humidity < 13)

and compile again and do (add) what the compiler is expecting BEFORE Serial

(rinse and ) repeat - compile again and do what compiler is again expecting BEFORE Serial

All better. It compiled and barring other errors elsewhere it will run.

And next time type in

if()

only at first and THAN insert ( type into brackets field) the compound condition inside the brackets.

Now you can check your logic by inserting values

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
 int temperature, humidity;

 temperature = 79;
humidity = 14; 

// if (temperature < 80) || (temperature > 112  && ( humidity < 13) replaced with 

correct if goes here 

 Serial.print("TRUE");
Serial.print("FALSE");
}
void loop()
{
}

PS Read up on how compound statement is evaluated - direction , precedence etc.
Have fun.

Hutkikz:

if (temperature >= 80 && temperature <= 112  &&  humidity <= 13)

{
 // do stuff here if true
}



This is a if statement.
An else if would be if this isnt true and something else is

Thank you , it works !

I spent some hours trying to get it working, finally I thought, time to ask the gurus. That was exactly what I was trying to accomplish: "only if the WHOLE expression is true, do this"

Here is the peace of code I was working on, I need to double check the correction "adj", but that is not a problem:

I added an screen shot. The top row is a sensor in my master bedroom, the bottom row is a sensor outside in the backyard.

The numbers represent the following: temperature , humidity, heat index, and dew point.

So nice to see how the humidity is inversely proportional to temperature.

// HEAT INDEX
double HIH6130::computeHeatIndex_F(void) {
double adj = ((13-humidity)/4)*sq((17-abs(temperature_F-95.))/17);

if ((temperature_F >= 80) && (temperature_F <= 112) && (humidity <= 13))

{
return (-42.379 +
2.04901523temperature_F +
10.14333127
humidity -
.22475541temperature_Fhumidity -
.00683783temperature_Ftemperature_F -
.05481717humidityhumidity +
.00122874temperature_Ftemperature_Fhumidity +
.00085282
temperature_Fhumidityhumidity -
.00000199temperature_Ftemperature_Fhumidityhumidity) - adj ;
}

else

{
return 0.5 * (temperature_F + 61.0 + ((temperature_F-68.0)1.2) + (humidity0.094));
}
}

Vaclav:
Let the compiler lead you in fixing your problem.
Start with this:

void setup() {

// put your setup code here, to run once:
  Serial.begin(115200);
int temperature, humidity;

if (temperature < 80) || (temperature > 112  && ( humidity < 13)

Serial.print("TRUE");
Serial.print("FALSE");
}
void loop()
{
}

Thank you so much for your tips. I tested but did not set the else statement to a particular value to see if the correct decision was made. What I mean is, I did not heat up the sensor to see if the program followed the correct logic.

Unless you have the operator precedences memorized (and why the hell would you), adding parentheses makes sure that the compiler interprets the statement the way that you want.

if((temperature >= 80 && temperature_F <= 112)  &&  humidity <= 13)

This tests that the temperature is between 80 and 112 (inclusive). Only if that condition is satisfied will the humidity be checked. Only when the temperature is in range AND the humidity is low will the body of the statement be executed.

techalex:
Thank you so much for your tips. I tested but did not set the else statement to a particular value to see if the correct decision was made. What I mean is, I did not heat up the sensor to see if the program followed the correct logic.

I have to apology for my omission - I did not specifically included the "else" also.
I believe the issue was incorrect syntax of "if" compound condition , not how "if" construct works.