Leds wont turn off

Hi there,

A quick summary to begin with:
I am programming a temperature/humidity sensor (DHT22), that code I got up and running but I wanted to implement something extra. I want indicate the right temprature with 3 leds, 1 blue led when it is too cold, 1 green led for the right temperature and 1 red led for when it is too hot. I made an if statement with the temperature values and added the leds to those values but when an led turned on it wont turn of, that is the problem at the moment. I am new to programming and cant find the solution on my own, I hope someone can help me. Thank you for reading!

Regards

Daniel

my code:
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2

DHT dht(DHTPIN, DHTTYPE);
float tt = 0.00;
void setup() {
Serial.begin (9600);
dht.begin();
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

}

void loop() {
//delay (1000);

float t = dht.readTemperature();
float h = dht.readHumidity();

if (t>19.5)
digitalWrite(5, HIGH);
else(LOW);
if (t>22)
digitalWrite(5,LOW);

if(t<19.5)
digitalWrite(4, HIGH);
else(LOW);

if(t>22)
digitalWrite(6, HIGH);
else(LOW);

Serial.print("Temperature: “);
Serial.print(t);
Serial.print(” *C ");
Serial.print(’\t’);

Serial.print("Humidity: “);
Serial.print(h);
Serial.print(” % ");
Serial.print(’\n’);
}

Read the “How to use the forum” thread and follow directions in section #7 about posting code.

There are several things wrong with your program. I think the biggest problem is the way you use “else(LOW)” to set pin states. I’m pretty sure it doesn’t work that way.

If you are comparing a value against against multiple constants, you should start either with the largest constant or the lowest constant and use if, elseif and else so you don’t keep comparing the value once you’ve found a match.

You should also use constants instead of using pin numbers. The numbers you use to compare the temperature against should also be constants. This way it would be easy to change the pin assignments or use different temperature thresholds. You just change the number once at the top of the program instead of search through the code looking to see where each number is used.

Using good constant names also makes the code easier to read.

Here’s my version of your code.

#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2

DHT dht(DHTPIN, DHTTYPE);
float tt = 0.00;

const int RED_LED = 6;
const int GREEN_LED = 5;
const int BLUE_LED = 4;

const float HOT_THESHOLD = 22.0;
const float COLD_THESHOLD = 19.5;

void setup() {
  Serial.begin (9600);
  dht.begin();
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);


}

void loop() {
  //delay (1000);
  
  float t = dht.readTemperature();
  float h = dht.readHumidity();

   
  if (t > HOT_THESHOLD)
  {
    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(BLUE_LED, LOW);
  }  
  else if (t > COLD_THESHOLD)
  {
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, HIGH);
    digitalWrite(BLUE_LED, LOW);
  } 
  else
  {
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(BLUE_LED, HIGH);
  }
  
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print('\t');

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" % ");
  Serial.print('\n');
}

Thanks for the help, I really appreciate it! :slight_smile:

OK, so having appreciated it, do what he said. Read the instructions, then go back and modify your post to mark up the code as such so we can examine it conveniently and accurately.

Using good constant names also makes the code easier to read.

Using variables of the smallest applicable size is also good practice, as in

const byte RED_LED = 6;
const byte GREEN_LED = 5;
const byte BLUE_LED = 4;

UKHeliBob:
Using variables of the smallest applicable size is also good practice, as in

I don't know if the OP is still around but I appreciate the suggestion.

Thanks

Edit: I still agree it's a good idea to declare variables in the smallest applicable size, but I found it interesting when declaring a constant, the type of variable (at least byte and int) doesn't affect the program size.

UKHeliBob:
Using variables of the smallest applicable size is also good practice, as in

const byte RED_LED = 6;

const byte GREEN_LED = 5;
const byte BLUE_LED = 4;

Usually I use a #define for that, but I guess this doesn't make any difference in memory space used, right?

Cheers, Ale.

ilguargua:
Usually I use a #define for that, but I guess this doesn't make any difference in memory space used, right?

Cheers, Ale.

people debate #define vs const - just google it for oodles of discussions of this at great length.

I have always found the notion of declaring a variable as a constant somewhat contradictory :slight_smile:

I see it as declaring an entity, some are variable, some are constant.

I personally only use NAMES_IN_ALL_CAPITALS_WITH_UNDERSCORES only for #define,
const values get standard camelCaseNames, but that's a pure matter of taste.

I'm sure this is all old news but I'm relatively new to the Arduino so I was curious about the code size of the various constant/variable options.

#define and const both produce the same size of code. The variables size declaration doesn't matter when declaring a constant (at least not with byte and int).

The byte and the int constants took the same code space and variable space.

byte and int made a difference when the value wasn't being declared as a constant.

Hi,
[SOAPBOX}
We didn't HELP the OP.
We solved his problem.
He was too quick.
Did he learn anything???
Should have given him link to Reference .. If else
Then let HIM do a rewrite.
[/SOAPBOX]

Tom..... :slight_smile:

I debate about this myself because even if a full program is provided, there are endless possibilities for the OP to modify the program.

That said, I think you're right. Thanks for the reminder.

UKHeliBob:
I have always found the notion of declaring a variable as a constant somewhat contradictory :slight_smile:

That is because you see it as “declaring a variable”.

But it isn’t!

It is in fact, declaring a constant (a “named constant”). No-one ever said - the language never said - it was a variable. It is specified as a particular type of constant in the same way as is a variable, because it must have that characteristic when it is used.