dht11 with comparators

Hi, i have this code working with dht11 + lcd but i need to introduce a comparators something like if humidity is lower than 40% then turn a led and with temperature the same thing like if higher than 23 C then turn another led

Code working with no comparators DHT11 + LCD

#define dht_dpin 14
#define LIGHT_SENSOR_PIN 1
#include <LiquidCrystal.h>

LiquidCrystal lcd(0, 1, 2, 3, 4, 5);

byte bGlobalErr;
byte dht_dat[4];
int light_intensity = 0;
unsigned int flip = 0;

void setup(){
pinMode(13, OUTPUT);
lcd.begin(16, 2);
lcd.print("Heeeeeello!");
InitDHT();
delay(300);
delay(700);
}

void loop(){

if ( flip & 1 )
{
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}

flip++;

light_intensity=analogRead(LIGHT_SENSOR_PIN);

ReadDHT();
switch (bGlobalErr) {
case 0:
lcd.setCursor(0, 0);
lcd.print("temp = ");
lcd.setCursor(7, 0);
lcd.print( dht_dat[2], DEC);

lcd.setCursor(0, 1);
if ((flip % 15) > 7 )
{
lcd.print("humidity = ");
lcd.setCursor(11, 1);
lcd.print( dht_dat[0], DEC);
} else {
lcd.print("Light = ");
lcd.setCursor(8, 1);
lcd.print( light_intensity, DEC);
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
delay(800);

}

void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}

void ReadDHT(){

bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(18);
delayMicroseconds(600);
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);
delayMicroseconds(40);

dht_in=digitalRead(dht_dpin);

if(dht_in) {
bGlobalErr=1;
return;
}
delayMicroseconds(80);

dht_in=digitalRead(dht_dpin);

if(!dht_in) {
bGlobalErr=2;
return;
}
delayMicroseconds(70);
for (i=0; i<5; i++)
dht_dat = read_dht_dat();

  • pinMode(dht_dpin,OUTPUT);*
  • digitalWrite(dht_dpin,HIGH);*
  • byte dht_check_sum =*
  • dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];*
  • if(dht_dat[4]!= dht_check_sum)*
  • {bGlobalErr=3; }*
    };
    byte read_dht_dat(){
  • byte i = 0;*
  • byte result=0;*
  • for(i=0; i< 8; i++) {*
  • while(digitalRead(dht_dpin)==LOW) ; *
  • delayMicroseconds(30);*
  • if (digitalRead(dht_dpin)==HIGH)*
  • result |=(1<<(7-i)); *
  • while (digitalRead(dht_dpin)==HIGH) ;*
  • }*
  • return result;*
    }

if humidity is lower than 40% then turn a led and with temperature the same thing like if higher than 23 C then turn another led

That's easy:

  read_dht11();
  if dht11.humidity < 40% turn on a led;
  else turn off the led;
  if hdt11.temperature > 23 turn on another led;
  else turn off the led;
        for (i=0; i<5; i++)
                dht_dat = read_dht_dat();

Nicely done.

thank you dhenry for your reply, in fact i did that but im not sure where should i put that sentence on the code and i have doubts if its gonna be simple as that

i added a servo but with no control of comparators.. i have problem with that part :sweat_smile: .I wanna turn the servo on for 20 secs when humidity is lower than 40%. Anyone?

#define dht_dpin 14
#define LIGHT_SENSOR_PIN 1
#include <LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
Servo myservo;

byte bGlobalErr;
byte dht_dat[4];
int light_intensity = 0;
int pos = 0;
unsigned int flip = 0;

void setup(){
pinMode(13, OUTPUT);
myservo.attach(9);
lcd.begin(16, 2);
lcd.print("Heeeeeello!");
InitDHT();
delay(300);
delay(700);
}

void loop(){

if ( flip & 1 )
{
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}

flip++;

light_intensity=analogRead(LIGHT_SENSOR_PIN);

ReadDHT();
switch (bGlobalErr) {
case 0:
lcd.setCursor(0, 0);
lcd.print("temp = ");
lcd.setCursor(7, 0);
lcd.print( dht_dat[2], DEC);

lcd.setCursor(0, 1);
if ((flip % 15) > 7 )
{
lcd.print("humidity = ");
lcd.setCursor(11, 1);
lcd.print( dht_dat[0], DEC);
} else {
lcd.print("Light = ");
lcd.setCursor(8, 1);
lcd.print( light_intensity, DEC);
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
delay(800);
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}

void ReadDHT(){

bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(18);
delayMicroseconds(600);
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);
delayMicroseconds(40);

dht_in=digitalRead(dht_dpin);

if(dht_in) {
bGlobalErr=1;
return;
}
delayMicroseconds(80);

dht_in=digitalRead(dht_dpin);

if(!dht_in) {
bGlobalErr=2;
return;
}
delayMicroseconds(70);
for (i=0; i<5; i++)
dht_dat = read_dht_dat();

  • pinMode(dht_dpin,OUTPUT);*
  • digitalWrite(dht_dpin,HIGH);*
  • byte dht_check_sum =*
  • dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];*
  • if(dht_dat[4]!= dht_check_sum)*
  • {bGlobalErr=3; }*
    };
    byte read_dht_dat(){
  • byte i = 0;*
  • byte result=0;*
  • for(i=0; i< 8; i++) {*
  • while(digitalRead(dht_dpin)==LOW) ; *
  • delayMicroseconds(30);*
  • if (digitalRead(dht_dpin)==HIGH)*
  • result |=(1<<(7-i)); *
  • while (digitalRead(dht_dpin)==HIGH) ;*
  • }*
  • return result;*
    }

I wanna turn the servo on for 20 secs when humidity is lower than 40%. Anyone?

You don't "turn on a servo". Please describe exactly what you mean by that. While you are thinking about it, look at the blink without delay example.

You don't "turn on a servo". Please describe exactly what you mean by that. While you are thinking about it, look at the blink without delay example.

yeah sorry for the misleading words, i have the sweep servo code but i still need the comparations when temperature is higher than 25 C and the when humidity is lower than 40% turn on a led

this code have the servo sweep, the dht11 + lcd but no parameters for comparation
#define dht_dpin 14
#define LIGHT_SENSOR_PIN 1
#include <LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
Servo myservo;

byte bGlobalErr;
byte dht_dat[4];
int light_intensity = 0;
int pos = 0;
unsigned int flip = 0;

void setup(){
pinMode(13, OUTPUT);
myservo.attach(9);
lcd.begin(16, 2);
lcd.print("Heeeeeello!");
InitDHT();
delay(300);
delay(700);
}

void loop(){

if ( flip & 1 )
{
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}

flip++;

light_intensity=analogRead(LIGHT_SENSOR_PIN);

ReadDHT();
switch (bGlobalErr) {
case 0:
lcd.setCursor(0, 0);
lcd.print("temp = ");
lcd.setCursor(7, 0);
lcd.print( dht_dat[2], DEC);

lcd.setCursor(0, 1);
if ((flip % 15) > 7 )
{
lcd.print("humidity = ");
lcd.setCursor(11, 1);
lcd.print( dht_dat[0], DEC);
} else {
lcd.print("Light = ");
lcd.setCursor(8, 1);
lcd.print( light_intensity, DEC);
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
delay(800);
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}

void ReadDHT(){

bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(18);
delayMicroseconds(600);
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);
delayMicroseconds(40);

dht_in=digitalRead(dht_dpin);

if(dht_in) {
bGlobalErr=1;
return;
}
delayMicroseconds(80);

dht_in=digitalRead(dht_dpin);

if(!dht_in) {
bGlobalErr=2;
return;
}
delayMicroseconds(70);
for (i=0; i<5; i++)
dht_dat = read_dht_dat();

pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3; }
};

byte read_dht_dat(){

byte i = 0;
byte result=0;
for(i=0; i< 8; i++) {
while(digitalRead(dht_dpin)==LOW) ;
delayMicroseconds(30);
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));
while (digitalRead(dht_dpin)==HIGH) ;
}
return result;
}