Simple Temp/Humity Code

Hi all, I just got a temperature and humidity sensor (si7021) and I'm trying to write a little code to have an LED go off at a certain temp or humid threshold. The LED just goes on all the time and I can see with the serial monitor that it's not adhering to the rule. Pretty new to programming Arduino so I'm sure my if statement is not right. Code is below, thanks!

#include "Adafruit_Si7021.h"

Adafruit_Si7021 sensor = Adafruit_Si7021();

void setup() {
  Serial.begin(115200);
  Serial.println("Si7021 test");
  sensor.begin();
}

void loop() {
  Serial.print("Humidity:    "); Serial.print(sensor.readHumidity(), 2);
  Serial.print("\tTemperature: "); Serial.println(sensor.readTemperature(), 2);
  delay(10000);

  if (sensor.readTemperature() > 30);
  {
    digitalWrite(8, HIGH);
  }
    
  
}
  if (sensor.readTemperature() > 30);

If the temperature is above 30, do nothing. Otherwise, do nothing. Why bother with the test?

The ; on the end of that statement is the no-op instruction.

Cool, thanks that was it. I get it the whole if is a statement and I was ending it short with the ;

Even if you fix the ; at the end of the if statement, you never turn it back off when the temp gets lower than 30.

-jim lee

And it still won't work as pin 8 was never declared as an output using pinMode(). The only thing that will happen is pin 8 will have an internal pullup attached.

fishparalyzer:
Cool, thanks that was it. I get it the whole if is a statement and I was ending it short with the ;

More correctly,

an if() statement looks like

[b]if[/b]([i]condition[/i]) [i]statement[/i]

"statement", above, can be:

  • a single do-nothing semicolon
  • an expression followed by a semicolon
  • a statement list enclosed in braces
  • a nested if, while, or do statement.

Spacing an indentation don't matter to the way the compiler interprets your code.

if(foo);

id the same as

if(foo)
    ;

PaulMurrayCbr:
More correctly,

an if() statement looks like

[b]if[/b]([i]condition[/i]) [i]statement[/i]

"statement", above, can be:

  • a single do-nothing semicolon
  • an expression followed by a semicolon
  • a statement list enclosed in braces
  • a nested if, while, or do statement.

Spacing an indentation don't matter to the way the compiler interprets your code.

if(foo);

id the same as

if(foo)

;

This is why I recommend that all if statements, while statements, etc. always use curly braces to define a statement list, even if there is only one statement.

if(sensor.readTemperature() > 30)
{
    ;
}

would stand out like a sore thumb as being clearly NOT what you wanted to do.