I'm trying to figure out how to make the green LED light up when the temperature is more than zero, but it's apparently not working?

#include <dht11.h>
#define DHT11PIN 4

int greenLED = 13;
int redLED = 12;

dht11 DHT11;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // green LED
  digitalWrite(13, LOW);
  pinMode(12, OUTPUT);  // red LED
  digitalWrite(12, LOW);
}

void loop() {
  Serial.println();

  int chk = DHT11.read(DHT11PIN);
  int input = digitalRead(DHT11PIN);

  if (DHT11PIN == 0)
  {
    digitalWrite(13, HIGH);
  }
  else (DHT11PIN > 0);
  {
    digitalWrite(12, HIGH);
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature  (C): ");
  Serial.println((float)DHT11.temperature, 2);

  delay(5000);
  }

Welcome to the forum

#define DHT11PIN 4
  if (DHT11PIN == 0)

Because of the first statement the second one will never be true

Did you mean to trad the state of the pin rather than its value ?

1 Like

just to add to the good point of @UKHeliBob (in case it's needed)


that's the temperature you want to compare to 0 (the one you print)


that's probably not needed

1 Like

You may also want to turn off the LEDs at some point. If they ever do get written HIGH, nothing will change that.

a7

1 Like

...and lose the semicolon when you've fixed the condition.

1 Like

and don't forget the if :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.