#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// setup DHT pin and Type as well the LCD i2c address
#define DHTPIN A0
#define DHTTYPE 22
#define LCD_ADD 0x27
// Setup the DHT, LCD & RTC Moudle library object
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(LCD_ADD, 16, 2);
RTC_DS1307 rtc;
void setup() {
// initiate (start) the lcd, temp sensor and the rtc module:
lcd.init();
lcd.backlight();
dht.begin();
rtc.begin();
}
void loop() {
// read the temperature and humidity from the DHT sensor and store in variables
// because the dht return values with fractions we have to set the variable data type to a float
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// convert the above 2 variables into a string to be able to scroll it on the display as a text
String TempHumStr = "Temp: " + String(temperature, 1) + " C & Humidity: " + String(humidity, 1) + " %";
// Read the Date and time and store it in a variable called “now”
DateTime now = rtc.now();
// create a string for the date and time
String DateTimeStr = " Date: " + String(now.day()) + "/" + String(now.month()) + "/" + String(now.year()) + " Time: " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
// call the scroll message custom function and provide the date(messages) you want to show
// arguments as follow: (message , message, row for message, row for message 2, scroll time)
scrollMessage(TempHumStr, DateTimeStr, 0, 1, 200);
}
void scrollMessage(String message1, String message2, int row1, int row2, int delayTime){
// the first thing lets add 16 spaces before the messages
for(int i = 0; i < 16; i++){
message1 = " " + message1;
message2 = " " + message2;
}
// add 1 space after each message
message1 = message1 + " ";
message2 = message2 + " ";
// scroll the messages from left to right one character at a time
for(int x = 0; x < message1.length() + 16; x++){
lcd.setCursor(0, row1);
lcd.print(message1.substring(x, x + 16));
lcd.setCursor(0, row2);
lcd.print(message2.substring(x, x + 16));
delay(delayTime);
}
}