Folks,
I'm working on code for a possible project for my sister in law. She likes to keep track of birthdays. So I wanted to build her a clock which displays a simple happy birthday message along with the name of the family member on the day of the persons birthday (from midnight to midnight). On days when there isn't a birthday, I want to display the temperature and humidity. However when testing the code I noticed trailing characters when switching between the happy birthday message and the temp and humidity due to the different lengths of the text. How would I go about dealing with this?
#include <Wire.h> // include wire library
#include <RTClib.h> // RTC
#include <LiquidCrystal_I2C.h> // for LCD
#include "DHT_U.h"
#define Type DHT22
LiquidCrystal_I2C lcd(0x27, 20, 4); // create LCD, address is 0x27
RTC_DS3231 rtc; // create rtc, address is 0x68
DHT DHTT(sensePin, Type);
int sensePin=7;
float humidity;
float tempC;
float tempF;
// 0=Sunday, 1=Monday,...6=Saturday
const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
// 0=" ", 1=Jan, 2=Feb, ... 12=Dec
const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
void setup()
{
Serial.begin(9600); // start serial monitor for debugging if needed
lcd.init(); // initialize LCD
lcd.backlight(); // turn on LCD backlight
rtc.begin(); // initialize RTC
DHTT.begin();
} //END of setup()
void loop()
{
DateTime rtcTime = rtc.now(); // get time
int ss = rtcTime.second(); // save seconds
int mm = rtcTime.minute(); // save minutes
int hh = rtcTime.twelveHour(); // etc.
int DD = rtcTime.dayOfTheWeek();
int dd = rtcTime.day();
int MM = rtcTime.month();
int yyyy = rtcTime.year(); // save year
lcd.setCursor(0,0); // Set cursor to top left position
lcd.print(monthInWords[MM]); // print date in MMM-dd-yyyy format
lcd.print("-"); // print "-" for formating
if (dd<10) lcd.print ("0"); // add 0 before hour if less than 10
lcd.print(dd); // print day
lcd.print("-"); // print "-" for formatting
lcd.print(yyyy); // print year
lcd.print(" "); // print space for formatting
lcd.print(dayInWords[DD]); // print day in DDD format
lcd.setCursor(0,1); // set cursor at beginning of next row
if (hh<10) lcd.print("0"); // add 0 before hour if less than 10
lcd.print(hh); // print hour
lcd.print(":"); // add ":" for formatting
if (mm<10) lcd.print("0"); // add 0 before minite if less than 10
lcd.print(mm); // print minutes
lcd.print(":"); // add ":" for formatting
if (ss<10) lcd.print("0"); // add 0 before seconds if less than 10
lcd.print(ss); // print seconds
if (rtcTime.isPM()) lcd.print(" PM"); // print PM if PM
else lcd.print (" AM"); // else print AM
humidity = DHTT.readHumidity();
tempC = DHTT.readTemperature();
tempF = DHTT.readTemperature(true);
if (MM == 1 && dd == 6 && hh == 7 && mm == 16)
{
lcd.setCursor(0,2);
lcd.print("Happy Birthday");
lcd.setCursor(0,3);
lcd.print("Harry Wagner");
}
else {
lcd.setCursor(0,2);
lcd.print("Temperature: ");
lcd.print(tempF);
lcd.setCursor(0,3);
lcd.print("Humidity: ");
lcd.print(humidity);
}
}
Best,
Tony