Need help at code

Hello guys,

Im trying to control my room temperature, i´m using a dht11 and lcd display. At the moment i got the temp but now i tried to implament a relai that switch on at 20°C. But i struggle with IF Else

Here is the code:

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;
int Relaipin = 9;
#define DHT11_PIN 7

void setup(){
lcd.begin(16, 2);
}

void loop()
{
int chk = DHT.read11(DHT11_PIN);
if ( chk < 20)
digitalWrite(Relaipin, HIGH);

lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000);
}

sorry for my english it isnt that good

But i struggle with IF Else

What code should be executed if chk is less than 20 ?
What code should be executed if chk is equal to or greater than 20 ?
What code should always be executed in loop() regardless of the value of chk

Always put { and } round the code block to be executed by if and else even if there is only one statement in the code block

What is the problem? What is the struggle?

I notice that you have code to turn the relay on, but no code anywhere to turn it back off, so once it is on it is on for good.

You mean something like this ?

if(chk <= 22) {
digitalWrite(LEDpin, HIGH);

} else {
digitalWrite(LEDpin, LOW);
}

Try it and see.

rakanischu:
You mean something like this ?

if(chk <= 22) {
digitalWrite(LEDpin, HIGH);

} else {
digitalWrite(LEDpin, LOW);
}

Personally I would lay it out differently

if (chk <= 22)
{
  digitalWrite(LEDpin, HIGH);
}
else
{
  digitalWrite(LEDpin, LOW);
}

which to me makes it clearer what the code does, but it is the same code either way

Let us know how you get on when you test it

Your gonna want some Hysteresis.

Tryed the code but the LEDpin is constant on HIGH

What do your debug Serial prints show? What does the LCD show?

Post your complete code every time you make a change. Use CODE TAGS.

On the LCD is shown the temp and humidity this works good.

this is the actually used code:

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;
int LEDpin = 9;
#define DHT11_PIN 7

void setup(){
lcd.begin(16, 2);
}

void loop()
{
int chk = DHT.read11(DHT11_PIN);

if(chk <= 22)
{
digitalWrite(LEDpin, HIGH);
}

else
{
digitalWrite(LEDpin, LOW);

}

lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000);
}

No pinMode() for the LED pin so it defaults to being an input

gfvalvo:
Post your complete code every time you make a change. Use CODE TAGS.

See how much nicer it looks in code tags? Also, ctrl-t in the Arduino IDE to fix your indenting.

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;
int LEDpin = 9;
#define DHT11_PIN 7

void setup() {
  lcd.begin(16, 2);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);

  if (chk <= 22)
  {
    digitalWrite(LEDpin, HIGH);
  }

  else
  {
    digitalWrite(LEDpin, LOW);
  }

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(DHT.temperature);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(DHT.humidity);
  lcd.print("%");
  delay(1000);
}