Go Down

### Topic: Humidity vs Temperature and Comfort: DHT11 code (Read 6005 times)previous topic - next topic

#### JimboZA

##### Dec 09, 2014, 08:05 pmLast Edit: Dec 09, 2014, 08:06 pm by JimboZA
They say "It's not the heat, it's the humidity". Well, my late Mum said it anyway.

The chart here shows a colour scheme for the comfort as perceived by "the human body" (it doesn't say whose   ) for H vs T.

This sketch maps those colours into a 2D array, and extracts the colour code for the current H and T. It displays the results in the serial monitor, and sets an RGB LED to R, G, B or Y according to the chart. (Haven't hooked an actual LED up yet... )

Code: [Select]
`// Load h vs t comfort chart into 2d array// example: http://www.klingenburg-usa.com/news/hugo.html// Read the humidity and temp from a dht11  // Look up the comfort zone in the array// Display results in serial monitor// Set an RGB LED as Red, Blue, Green or Yellow ala chart#include <dht11.h>dht11 DHT;#define DHT11_PIN 5 byte humid;byte temp;byte humidInd;byte tempInd;char comfColour;// pins of the RGB LED..byte LED_R = 2;byte LED_G = 3;byte LED_B = 6;//fill the array based on the colour chartint comf[10][11] = {  'B','B','B','R','R','R','R','R','R','R','\0',  //high humid  'B','B','Y','Y','R','R','R','R','R','R','\0',  'B','B','Y','Y','Y','R','R','R','R','R','\0',  'B','B','Y','G','G','Y','Y','R','R','R','\0',  'B','B','Y','G','G','G','Y','R','R','R','\0',  'B','B','Y','G','G','G','Y','Y','R','R','\0',  'B','B','B','Y','Y','G','Y','Y','R','R','\0',  'B','B','B','B','Y','Y','Y','Y','R','R','\0',  'B','B','B','B','B','B','B','R','R','R','\0',  'B','B','B','B','B','B','B','R','R','R','\0',  //low humid}; // void setup(){  pinMode(LED_R, OUTPUT);  pinMode(LED_G, OUTPUT);  pinMode(LED_B, OUTPUT);  Serial.begin(9600);  Serial.println("DHT COMFORT PROGRAM ");  Serial.print("LIBRARY VERSION: ");  Serial.println(DHT11LIB_VERSION);  Serial.println();  Serial.println("Humidity (%),\tTemperature (C),\tComfort");}void loop(){  //get data  int chk;  chk = DHT.read(DHT11_PIN);    // READ DATA  humid = DHT.humidity;  temp = DHT.temperature;  //calc indices into the 2d matrix  humidInd =9- humid/10;  if (temp%2==0)  //even temp  {    tempInd=(temp/2)-8;  }  else  //odd temp  {    tempInd=(temp/2)-7;  }  if (tempInd < 0) tempInd=0;  if (tempInd > 9) tempInd=9;  // get the current value from the array  comfColour = comf[humidInd][tempInd];  // DISPLAY DATA  Serial.print(humid);  Serial.print(",\t\t\t");  Serial.print(temp);  Serial.print(",\t\t");  Serial.println(comfColour);  //and set the led  if (comfColour == 'R')  {    digitalWrite(LED_R, HIGH);    digitalWrite(LED_G, LOW);    digitalWrite(LED_B, LOW);  }  if (comfColour == 'G')  {    digitalWrite(LED_R, LOW);    digitalWrite(LED_G, HIGH);    digitalWrite(LED_B, LOW);  }  if (comfColour == 'B')  {    digitalWrite(LED_R, LOW);    digitalWrite(LED_G, LOW);    digitalWrite(LED_B, HIGH);  }  if (comfColour == 'Y')  {    digitalWrite(LED_R, HIGH);    digitalWrite(LED_G, HIGH);    digitalWrite(LED_B, LOW);  }  delay(10000);}//loop`

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### robtillaart

#1
##### Dec 09, 2014, 09:47 pmLast Edit: Dec 09, 2014, 10:26 pm by robtillaart
That's a great application!

Note the array could be of the type byte, as that would half its footprint.
And you could put a compressed RGB value in every byte: three bits R, three bits Green, 2 bits blue. [RRRGGGBB] effectively giving 256 colors. yes that would be "a bit" artificial, but it would allow you to interpolate more smoothly between the values.

* I do not understand the \0, as they are not needed

* humidInd = 9 - (humid+5)/10;  // rounding iso truncating.
similar for temperature.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### JimboZA

#2
##### Dec 10, 2014, 03:07 amLast Edit: Dec 10, 2014, 03:25 am by JimboZA
Thanks for the tips there Rob, I'll implement the rounding. Thanks for the link in the Playground too: fame at last whoop whoop.

I'm going to put this into my Arduino webserver too. That's tonight's work.... Probably print the phrase "This is your comfort level" in the appropriate colour.

I'm reluctant to implement too many colours. Who's to say we all feel the same comfort at the same H&T. I will probably go for splitting the yellow into yellow above the green on the red side, and yellow below by the blue: maybe a bright yellow vs a dark orange. Or something.

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### JimboZA

#3
##### Dec 10, 2014, 08:57 am
Actually I might change that chart for the humidex which puts a number to each H/T pair and then into a band.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### robtillaart

#4
##### Dec 10, 2014, 08:06 pm
I recall doing some humidex code ..... search .......

check - http://forum.arduino.cc/index.php?topic=107569.0
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### JimboZA

#5
##### Dec 11, 2014, 03:37 am
Thanks Rob, I might use that. This is one of those cases where one should weigh up the pros and cons (time?, memory?) of lookup vs calc-on-the-fly, I guess.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### Imanesirin

#6
##### Dec 11, 2014, 12:55 pm
Hi all,
how can'I make sure that the sensor of pression gives the good value?

Regards.

#### JimboZA

#7
##### Dec 11, 2014, 02:47 pm
Is pression, pressure?

No idea, and the DHT is a humidity and temperature sensor.

If you have trouble with a pressure sensor you are using, you should start a new thread with details of what type of sensor it is (link to datasheet always helps), your code, and a schematic of your circuit. Also explain clearly what's wrong.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### robtillaart

#8
##### Dec 11, 2014, 07:51 pm
Thanks Rob, I might use that. This is one of those cases where one should weigh up the pros and cons (time?, memory?) of lookup vs calc-on-the-fly, I guess.
As the DHT sensor needs ~2 seconds between measurements one has time enough to calculate.
The good thing of formulas is that on can calculate it with quite some precision while for a lookup table one needs to interpolate.

What one at least could do is to check if the formula and the graph on the link you posted match ...
if not there is at least one of them wrong ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### dhiya

#9
##### May 15, 2016, 01:41 am
good application :
i am has been used project about the temperature by dht sensor and i used opt  and mosfet  for control the res for temprrature and humidite but what is meaning
humidInd =9- humid/10;

if (temp%2==0)  //even temp
{
tempInd=(temp/2)-8;
}
else  //odd temp
{
tempInd=(temp/2)-7;
}

if (tempInd < 0) tempInd=0;
if (tempInd > 9) tempInd=9;

9,2,8 and 7 ..

Go Up