 Hey guys!

I’m trying to make a very simple time and date clock display for beginners. (I took the basic code from someone else, modified for ease of use, simplicity and made sure to label every line to help beginners better understand what is actually going on)

My question for you guys is how can I display the leading zeros in a number (Example: 01 as opposed to 1)

``````//Arduino IDE ver. 1.8.5
//Arduino Uno R3
//DFRobot LCD Keypad Shield ver. 1.1

#include <LiquidCrystal.h> //LCD display library name

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Tells the Arduino which pins are being used by the LCD display. You will need to change the numbers to suit the LCD display you're using.

int hour = 23; //Change this to set the clock hour
int minute = 59; //Change this to set the clock minute
int second = 54; //Change this to set the clock minute

int month = 7; //Change this to set the clock month
int day = 24; //Change this to set the clock day
int year = 2018; //Change this to set the clock year

void setup()
{
lcd.begin(16, 2); //Tells the Arduino that the LCD is using 2 rows, 16 characters each
}

void loop()
//TIME
{
lcd.setCursor(0, 0); //LCD curser begins on the top row, far left side

lcd.print("TIME: "); //The LCD prints "TIME: "
lcd.print(hour); //The LCD prints the hour
lcd.print(":"); //The LCD prints ":"
lcd.print(minute); //The LCD prints the minute
lcd.print(":"); //The LCD prints ":"
lcd.print(second); //The LCD prints the second(s)

if (hour < 12)lcd.print("AM"); //If the hour is less than 12, the LCD prints AM
if (hour == 12)lcd.print("PM"); //If the hour is equal to 12, the LCD prints PM
if (hour > 12)lcd.print("PM"); //If the hour is greater than 12, the LCD prints PM
if (hour == 24)hour = 01; //If the hour is equal to 24, reset the hour back to 1 (1AM)

second = second + 1; //Add +1 second, every second

delay(1000); //Refresh the LCD every second

lcd.clear(); //Clears the LCD

if (second == 60) //If the seconds equal 60
{
second = 0; //Reset the seconds back to 0
minute = minute + 1; //Add +1 to the minute(s)
}
if (minute == 60) //If the minutes equal 60
{
minute = 0; //Reset the minute(s) back to 0
hour = hour + 1; //Add +1 to the hour(s)
}
if (hour == 24) //If the hours are equal to 24 (Midnight)
{
hour = 1; //Reset the hours back to 1 (1AM)
}

//DATE
lcd.setCursor(0, 1); //LCD curser begins on the bottom row, far left side
lcd.print("DATE: "); //The LCD prints "DATE: "
lcd.print(month); //The LCD prints the month
lcd.print("/"); //The LCD prints "/"
lcd.print(day); //The LCD prints the day
lcd.print("/"); //The LCD prints "/"
lcd.print(year); //The LCD prints the year

if (((hour > 22) && (minute > 58) && (second > 58))) //If the hour is greater than 22, the minute is greater than 58 and the second is greater than 58
{
day = day + 1; //Add +1 to the day
}

//MONTHS
//January
if (month == 1) //If it's the first month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in January)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//February
if (month == 2) //If it's the second month of the year
{
if (day == 29) //If the days are equal to 29 (28 days in February for a non-leap year. 29 days in February for a leap-year
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//March
if (month == 3) //If it's the third month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in March)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//April
if (month == 4) //If it's the fourth month of the year
{
if (day == 31) //If the days are equal to 31 (30 days in April)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//May
if (month == 5) //If it's the fifth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in May)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//June
if (month == 6) //If it's the sixth month of the year
{
if (day == 31) //If the days are equal to 31 (30 days in June)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//July
if (month == 7) //If it's the seventh month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in July)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//August
if (month == 8) //If it's the eighth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in August)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//September
if (month == 9) //If it's the nineth month of the year
{
if (day == 31) //If the days are equal to 31 (30 days in September)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//October
if (month == 10) //If it's the tenth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in October)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//November
if (month == 11) //If it's the eleventh month of the year
{
if (day == 31)//If the days are equal to 31 (30 days in November)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//December
if (month == 12) //If it's the twelfth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in December)
{
day = 1; //Reset the days back to 1
month = 1; //Add +1 to the month
year = year + 1; //Add +1 to the year
}
}
}
``````

Just create a helper function e.g.

``````void print02(uint16_t val)
{
if (val < 10) lcd.print("0");
lcd.print(val);
}
``````

The Arduino is pretty hopeless at producing nicely formatted output.

David.

@david_prentice
Hahaha, that's true. Thank you very much! +1 Karma. Going to implement it now david_prentice:
Just create a helper function e.g.

``````void print02(uint16_t val)
``````

{
if (val < 10) lcd.print(“0”);
lcd.print(val);
}

``````void print02(uint16_t val)
{
if (val < 10)   // <10 = 09, 08, 07, 06, 05, 04, 03, 02, 01, 00
{
lcd.print("0");
}
lcd.print(val);
}
``````

I don't know why but I still can't get it to work right.
Going to try it the long way...

You simply replace all your annoying statements e.g.

``````  lcd.print(hour); //The LCD prints the hour
``````

with a call to your helper function

``````  print02(hour); //The LCD prints the hour
``````

There are lots of other ways to do it. For example using sprintf() to format all your output in one go and then printing the result on the LCD. This makes a project far easier to maintain.

I note that you have verbal diarrhoea when it comes to writing comments.
There are algorithms that will calculate the days in a month. Or you can simply use a lookup table.

Comments are for your benefit. Use the style that you feel happiest with.

David.

Got it to work the long way.

(haha, verbal diarrhea. I was just trying to comment it so that beginners could understand without it reading too technical)

However, I also am still curious to do it your way, so I’m trying it again. And I’ll also look into the sprint() as you suggested.

Btw, here’s how I did it the long way:

``````//Arduino IDE ver. 1.8.5
//Arduino Uno R3
//DFRobot LCD Keypad Shield ver. 1.1

#include <LiquidCrystal.h> //LCD display library name

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Tells the Arduino which pins are being used by the LCD display. You will need to change the numbers to suit the LCD display you're using.

int hour = 23; //Change this to set the clock hour
int minute = 59; //Change this to set the clock minute
int second = 54; //Change this to set the clock minute

int month = 7; //Change this to set the clock month
int day = 24; //Change this to set the clock day
int year = 2018; //Change this to set the clock year

void setup()
{
lcd.begin(16, 2); //Tells the Arduino that the LCD is using 2 rows, 16 characters each
}

void loop()
{
//TIME
lcd.setCursor(0, 0); //LCD curser begins on the top row, far left side

lcd.print("TIME: "); //The LCD prints "TIME: "
lcd.print(hour); //The LCD prints the hour
lcd.print(":"); //The LCD prints ":"
lcd.print(minute); //The LCD prints the minute
lcd.print(":"); //The LCD prints ":"
lcd.print(second); //The LCD prints the second(s)

if (hour == 24)hour = 01; //If the hour is equal to 24, reset the hour back to 1 (1AM)

if (hour < 10)
{
lcd.setCursor(0, 0); //LCD curser begins on the top row, far left side

lcd.print("TIME: 0"); //The LCD prints "TIME: "
lcd.print(hour); //The LCD prints the hour
lcd.print(":"); //The LCD prints ":"
lcd.print(minute); //The LCD prints the minute
lcd.print(":"); //The LCD prints ":"
lcd.print(second); //The LCD prints the second(s)
}

if (minute < 10)
{
lcd.setCursor(0, 0); //LCD curser begins on the top row, far left side

lcd.print("TIME: 0"); //The LCD prints "TIME: "
lcd.print(hour); //The LCD prints the hour
lcd.print(":0"); //The LCD prints ":"
lcd.print(minute); //The LCD prints the minute
lcd.print(":"); //The LCD prints ":"
lcd.print(second); //The LCD prints the second(s)
}

if (second < 10)
{
lcd.setCursor(0, 0); //LCD curser begins on the top row, far left side

lcd.print("TIME: 0"); //The LCD prints "TIME: "
lcd.print(hour); //The LCD prints the hour
lcd.print(":0"); //The LCD prints ":"
lcd.print(minute); //The LCD prints the minute
lcd.print(":0"); //The LCD prints ":"
lcd.print(second); //The LCD prints the second(s)
}

second = second + 1; //Add +1 second, every second

delay(1000); //Refresh the LCD every second

lcd.clear(); //Clears the LCD

if (second == 60) //If the seconds equal 60
{
second = 0; //Reset the seconds back to 0
minute = minute + 1; //Add +1 to the minute(s)
}
if (minute == 60) //If the minutes equal 60
{
minute = 0; //Reset the minute(s) back to 0
hour = hour + 1; //Add +1 to the hour(s)
}
if (hour == 24) //If the hours are equal to 24 (Midnight)
{
hour = 1; //Reset the hours back to 1 (1AM)
}

//DATE
lcd.setCursor(0, 1); //LCD curser begins on the bottom row, far left side
lcd.print("DATE: "); //The LCD prints "DATE: "
lcd.print(month); //The LCD prints the month
lcd.print("/"); //The LCD prints "/"
lcd.print(day); //The LCD prints the day
lcd.print("/"); //The LCD prints "/"
lcd.print(year); //The LCD prints the year

if (((hour > 22) && (minute > 58) && (second > 58))) //If the hour is greater than 22, the minute is greater than 58 and the second is greater than 58
{
day = day + 1; //Add +1 to the day
}

//MONTHS
//January
if (month == 1) //If it's the first month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in January)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//February
if (month == 2) //If it's the second month of the year
{
if (day == 29) //If the days are equal to 29 (28 days in February for a non-leap year. 29 days in February for a leap-year
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//March
if (month == 3) //If it's the third month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in March)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//April
if (month == 4) //If it's the fourth month of the year
{
if (day == 31) //If the days are equal to 31 (30 days in April)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//May
if (month == 5) //If it's the fifth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in May)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//June
if (month == 6) //If it's the sixth month of the year
{
if (day == 31) //If the days are equal to 31 (30 days in June)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//July
if (month == 7) //If it's the seventh month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in July)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//August
if (month == 8) //If it's the eighth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in August)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//September
if (month == 9) //If it's the nineth month of the year
{
if (day == 31) //If the days are equal to 31 (30 days in September)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//October
if (month == 10) //If it's the tenth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in October)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//November
if (month == 11) //If it's the eleventh month of the year
{
if (day == 31)//If the days are equal to 31 (30 days in November)
{
day = 1; //Reset the days back to 1
month = month + 1; //Add +1 to the month
}
}

//December
if (month == 12) //If it's the twelfth month of the year
{
if (day == 32) //If the days are equal to 32 (31 days in December)
{
day = 1; //Reset the days back to 1
month = 1; //Add +1 to the month
year = year + 1; //Add +1 to the year
}
}
}
``````

You have not fixed it at all. You just get :09 followed by :010

Regarding verbal diarrhoea. I just blank it out. My brain refuses to read it.

The long-winded way of doing your problem is:

``````  if (hour < 10) lcd.print("0");
lcd.print(hour); //The LCD prints the hour
...
if (minute < 10) lcd.print("0");
lcd.print(minute); //The LCD prints the minute
...
``````

DR2727:

``````  if (hour == 24)hour = 01; //If the hour is equal to 24, reset the hour back to 1 (1AM)
``````

...

if (hour == 24) //If the hours are equal to 24 (Midnight)
{
hour = 1; //Reset the hours back to 1 (1AM)
}

On my clock, midnight comes an hour before 1AM. And your code looks like it has problems with rolling over to the next day and also to the next month.

DR2727:
I’m trying to make a very simple time and date clock display for beginners. (I took the basic code from someone else, modified for ease of use, simplicity and made sure to label every line to help beginners better understand what is actually going on)

My question for you guys is how can I display the leading zeros in a number (Example: 01 as opposed to 1)

This is a very common problem. Unfortunately, there is no “clean” solution, because whoever wrote the Arduino `print()` functions did not see fit to support leading zeros. This leads to posts like yours.

Perhaps the most “beginner-friendly” way to deal with this problem is what david_prentice suggested. For clarity, I will reformat and comment his suggestion.

``````  if (hour < 10) // This checks for single digit hours (anything less than 10 will be a single digit)
{
lcd.print("0"); // The LCD prints a zero before the hours
}

lcd.print(hour); //The LCD prints the hour

lcd.print(":"); // The LCD prints a colon between hours and minutes

if (minute < 10) // This checks for single digit minutes (anything less than 10 will be a single digit)
{
lcd.print("0"); // The LCD prints a zero before the minutes
}

lcd.print(minute); //The LCD prints the minute
``````

Then there is the way I would do it.

``````char buf;  // a place to put a character string

sprintf(buf, "%02d:%02d:%02d", hour, minute, second); // put a character string showing the time (hour:minute:second) into buf

lcd.print(buf); // the LCD prints the character string from buf
``````

This second approach is much shorter and easier to read, if you know what you are doing. But, you have to know what you’re doing. There is more info on it here: sprintf | Liudr's Blog