Go Down

Topic: *Solved* Displaying LM35 & DHT11 on LCD : only looping twice through program (Read 1 time) previous topic - next topic

t1sugar

Hi Folks,

I am new to the world of Arduino but absolutly love the concept of it :D

I got a Sensor Kit and Uno from ebay which contained a DHT11, LM35 and LCD 16,2.
I downloaded some sample code for a similar kit with the additions of light & Tilt and adapted that code as far as i could, I have got the values displayed on the LCD but they are not refreshing.

I have had a good read of many forum posts but cant seem to find the solution.

heres the adapted code
Code: [Select]
/////////////////////////////////////////////
// This demo code uses 4 different sensors and an LCD module
// to demonstrate reading a displaying continuous sensor data.
// Data is read from a mechanical tilt sensor to show FLAT or TILT
// An LDR is used to measure light level and is roughly converted to Lux
// A DHT11 is used to measure Humidity in %RH
// Finally an LM35 is used to measure temperature in degress C
//
// This code may be freely used and copied.
//
// Gareth Davies - June 2012
//
////////////////////////////////////////////////

#include <LiquidCrystal.h>
#include <dht11.h>

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

int temp, humidity, hCheck;


#define tempPin 1
#define humidityPin 6

dht11 DHT11;

// initialise LCD library and pins
void setup() {
 lcd.begin(16, 2);
 pinMode(humidityPin, INPUT);
 pinMode(tempPin, INPUT);
}

void loop()
{
 temp = (5.0 * analogRead(tempPin) * 100.0) / 1024;
 delay(10);
 hCheck = DHT11.read(humidityPin);
 if(hCheck != 0)
   humidity = 255; //Must be an error
 else
   humidity = DHT11.humidity;
   
 showData(temp, humidity);
 delay(1000);
}
void showData (int temp, int humidity)
{
 String s1, s2, s3;
 String spaces = "              ";
 s1 = String(temp) + char(0xdf) + "C";
 if(humidity == 255)
 s2 = "ERROR";
 else
 s2 = String(humidity) + "%";
 s3 = s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
 lcd.setCursor(0,0);
 lcd.print(s3);
}


Please accept my humble apologies if this is a daft question / post

Edit: After playing a little more i added some serial commands and ran project again - Interestingly the programme only looped twice i.e. only displayed via serial temp, humidity values twice.


Kindest Regards
Tony

marco_c

Don't use String. Use an array of characters.

The String implementation in the Arduino library has a bug and is notorious for leaking memory.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

michinyon

I think it is legal to have "global" variables temp and humidity  and the same
as local variables with the same name in your last function there,   but it really isn't a good idea
because it is easy to get confused.

And if you are having them as global variables,  they don't need to be parameters to
your function.

michinyon

And if you are looking for a suggestion on how to proceed with debugging,  I suggest
you separate the sensor reading issue from the possible issue of the lcd object misbehaving.

There is no obvious infinite loop in this code,  so the fault might be elsewhere.

step 1:  try reading the sensor and sending the output to your pc by serial link.  This will
check the sensor code works.

step 2:  write another sketch which just tests the lcd object.

and avoid the strings.

t1sugar

Thanks Very Much for the advice

Ok, I have it working, heres what I did.

Looked at creating an array but I would have to understand this a bit more, so I followed "michinyon" advice and tested the sensor part, and it worked, I then removed the string usage and replaced with lcd character placement;
Is this a type of array?  

either way it works now: i intend on completing the tutorials so as to get a better understanding of all things Arduino.

Thanks again for the pointers  :)

here's the code:
Code: [Select]


#include <LiquidCrystal.h>
#include <dht11.h>

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

int temp, humidity, hCheck;


#define tempPin 1
#define humidityPin 6

dht11 DHT11;

// initialise LCD library and pins
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}

void loop()
{
 temp = (5.0 * analogRead(tempPin) * 100.0) / 1024;
 delay(10);
 hCheck = DHT11.read(humidityPin);
 if(hCheck != 0)
   humidity = 255; //Must be an error
 else
   humidity = DHT11.humidity;
   
 showData(temp, humidity);
 Serial.print("Humidity : ");Serial.println(humidity);
 Serial.print("Celcius : ");Serial.println(temp);
 delay(1000);
}
void showData (int temp, int humidity)
{
 
 lcd.setCursor(0,0);
 lcd.print(temp) + lcd.print(char (0xdf)) + lcd.print("C");
 lcd.setCursor(13,0);
 lcd.print(humidity) + lcd.print("%");
}

BlackLab1

Just for kicks have you gone over to Adafruit.com and check out their Tutorial on the DHTxx sensor? { http://learn.adafruit.com/dht} They have some example code I played with for fun, that was helpful.

Go Up