Pages: [1]   Go Down
Author Topic: DHT11  (Read 658 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I really need help: smiley-cry
I am working in an artist proyect.
I think this is a simple thing, but i have investigated and not found much;
i have a dht11 temperature an humidity sensor of 4 pins, and am trying to turn on a led for a moment and then turn it off
whith the changes humedity  values.
  I nedd to respond when a person breaths near of the sensor, so I already use the librery and Its enough that the led activate with
the change of tow or three values but i cant di that because i dont already undestand how the sensor works.

Here I have what i change in the code.
Thanks a lot, i hope you can help me!
Sorry about my english.  smiley-red
Code:
#include <dht11.h>

dht11 DHT11;
 
int pinLed = 6;
float valorActual;
float valorhumedad;
float un;
int iPuerto =4;

void setup()

{
Serial.begin(9600);



}


void getdata(int iPuerto)
{
valorActual = DHT11.humidity,(iPuerto);
  
int chk = DHT11.read(iPuerto);

Serial.print("Sensor");
Serial.print(iPuerto);
Serial.print(" ");
switch (chk)
{
case 0:
Serial.println((float)DHT11.humidity,(iPuerto));
Serial.println(" % ");
Serial.println((float)DHT11.temperature,(iPuerto));
Serial.println(" o C");
break;
case -1: Serial.println(" Checksum error"); break;
case -2: Serial.println(" Time out error"); break;
default: Serial.println(" Unknown error"); break;

if (DHT11.humidity == DHT11.humidity+3 ){
 digitalWrite (pinLed , HIGH);
  delay(1000);
   digitalWrite (pinLed , LOW);
 }else {
  
   digitalWrite (pinLed , LOW);
 }

  }
}



void loop()
{
getdata(iPuerto);

delay(400);
}

 
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (DHT11.humidity == DHT11.humidity+3 ){

This will never be true because the value of the humidity will never be the same as that value plus 3.

I am having trouble understanding what it is you want to have happen, so I am interpreting that you want the LED to come on if the value of humidity changes by 3 or more (whatever that means).

The key is that you need to remember what the value was the time before when you called getdata. So the code needs to look like this:

Code:
static int oldValue; // I have this as int but it needs to be the same type as DHT11.humidity

valorActual = DHT11.humidity,(iPuerto);
   
int chk = DHT11.read(iPuerto);

Serial.print("Sensor");
Serial.print(iPuerto);
Serial.print(" ");
switch (chk)
{
case 0:
Serial.println((float)DHT11.humidity,(iPuerto));
Serial.println(" % ");
Serial.println((float)DHT11.temperature,(iPuerto));
Serial.println(" o C");
break;
case -1: Serial.println(" Checksum error"); break;
case -2: Serial.println(" Time out error"); break;
default: Serial.println(" Unknown error"); break;

if (DHT11.humidity - oldvalue >= 3 ){ // new value and old value are different by more than the threshold
 digitalWrite (pinLed , HIGH);
  delay(1000);
   digitalWrite (pinLed , LOW);
 }else {
   
   digitalWrite (pinLed , LOW);
 }
oldValue = DHT11.humidity; // save the old value for next time
  }
}

The 'static' in the definition makes sure that the value remains in the memory between calls to getdata.

There are more elegant ways to do this, but this will do for the moment. smiley
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: