Hello Guys,
I Just started discovering embedded programming with Arduino possiblities with a little personal project. That’s pretty fun!
I have now a blocking issue with my code. I hope you guys can help me! Thank you very much in advance!!
A few details first… My board is composed of:
-
Arduino MEGA2560
-
LCD1602 display module
-
DS1307 RTC Module
-
DHT11 Temperature/Humidity Module
-
My Goal here is to Display Date, Time and Temperature on LCD Module
-
I first setted up the Time & Date only, and it was working great!
-
My current step now is to add DHT11 Temperature on LCD Screen
-
Nothing was displaying on Screen, so i simplified the code just to display temperature on Serial Monitor
The Temperature gathering part of my code is based on this sample code:
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
}
}
This sample code is working great on my board. Here is the Serial Monitor Result:
Then i apply nearly the exact same lines to my own code, and run it on the same Board:
// Temperature Sensor Library & Globals
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
// LCD Display Library
#include <LiquidCrystal.h>
// DS3231 RTC Clock time displayed on LCD Screen
#include <Wire.h>
#include <DS3231.h>
//Clock Variables
DS3231 clock;
RTCDateTime dt;
//LCD Interface definition
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
void setup()
{
// Serial Console Initialize
Serial.begin(9600);
//Define LCD Number of Columns & Rows
lcd.begin(16, 2);
// Initialize DS3231
Serial.println("Initialize DS3231");
clock.begin();
// Clock Reference Time Setting
//clock.setDateTime(__DATE__, __TIME__);
}
void loop()
{
//get DateTime from RTC Clock
dt = clock.getDateTime();
char rtcTime[20];
char rtcDate[20];
sprintf(rtcTime, "%02d:%02d:%02d", dt.hour, dt.minute, dt.second);
sprintf(rtcDate, "%02d/%02d/%02d", dt.day, dt.month, dt.year);
//get Temperature and humidity
float temperature;
float humidity;
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
}
else
{
Serial.println("nope.");
}
//print variables on screen
lcd.setCursor(0, 0);
lcd.print(rtcTime);
lcd.setCursor(12, 0);
lcd.print(temperature);
lcd.setCursor(0, 1);
lcd.print(rtcDate);
lcd.setCursor(12, 1);
lcd.print(humidity);
delay(1000);
}
When running this code, i never receive a measurement from Temp. Module. Here is Serial Monitor:
a few things i already tried:
-
Since i’m a total noob with electronic, i thought that maybe all those modules needed too much energy. I disconnected the LCD Screen, but it did not had any effet.
-
On Arduino IDE, user interface recognizes the line “DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );” and “DHT_nonblocking” is colored in Orange. But on The sample code, the same line is not colored (remains black). Since i’m on my first hours working with with Arduino IDE, i thought i could have some Library Linking issue. The thing is i don’t have any issue at compiling step, so i did not went further at this side… maybe i should…
Tank you very much in advance, and don’t hesitate to ask me if you need any other information!!