Go Down

Topic: DHT11 (Read 728 times) previous topic - next topic

cornelia

Hi I really need help: =(
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: [Select]
#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);
}



marco_c

Code: [Select]
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: [Select]

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. :)
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up