DHT11 with LED for indication of humidity ranges, code is not being excecuted

Hi everyone, I need someone to look at my set up and code, having problems to make it working the way I want.

What I am trying to do:
I want an RGB-LED indicate when the humidity, measured by a DHT11, exceeds certain ranges.

My current situation:
I have the DHT11 working and showing temperature and humidity via an LCD display. The RGB-LED is connected via 3 PWM ports and is glowing green, but it is not reacting to any humidity changes.

What I have tried so far:

  • move the if/else-functions to different positions in the program loop
  • use only one if/else-function
  • replace the RGB-LED with three separate LEDs
  • use digitalWrite instead of analogWrite

nothing of the above has helped.
I really hope someone has an idea what is wrong with my code/setup...

See attachement for schematics

The code:

#include <LiquidCrystal.h>
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#define BLUE 3
#define GREEN 5
#define RED 6

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("temp. & humidity:");

  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);

}

int redValue;
int greenValue;
int blueValue;

static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if ( millis( ) - measurement_timestamp > 3000ul )
  {
    if ( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return ( true );
    }
  }

  return ( false );
}

void loop() {

  float temperature = 25.0;
  float humidity = 50.0;

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);

  if ( measure_environment( &temperature, &humidity ) == true )
  {
    lcd.print( "T=" );
    lcd.print( temperature, 0 );
    lcd.print( ", H=" );
    lcd.print( humidity, 0 );

    if (40.0 < humidity < 60.0)
    {
      if (45.0 < humidity < 55.0)
      {

        analogWrite(RED, 0),
        analogWrite(GREEN, 20),
        analogWrite(BLUE, 0);
      }
      else
      {
        analogWrite(RED, 30),
        analogWrite(GREEN, 20),
        analogWrite(BLUE, 0);
      }
    }
    else
    {
      analogWrite(RED, 20),
      analogWrite(GREEN, 0),
      analogWrite(BLUE, 0);
    }
  }

}

Thanks for any input!

Nice job with code tags on your first post.

if (40.0 < humidity < 60.0)
    {
      if (45.0 < humidity < 55.0)
      {

This is not correct syntax for determining if a value is in a range.

A correct form is

if (40.0 < humidity and humidity < 60.0)
    {
      if (45.0 < humidity and humidity< 55.0)
      {

You need to use the boolean “and” (&&) for the logic to ensure that both conditions are true.

https://www.tutorialspoint.com/arduino/arduino_boolean_operators.htm

That totally makes sense... Thanks for the quick help, cattledog, it works now!

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