0

I'm using RTClib for my DS3231 module, the problem is the library is displaying the second without zero behind it: 22:1:1 and when i print the time on my 16x2 LCD time is displaying like this:

22:22:59

22:23:19

22:23:29

...

22:23:59

22:23:10

22:23:11

the second number of seconds is not getting cleared. i know one solution to this is to use lcd.clear() function to update the LCD in each loop but is there any other way to clear that second number or do something so library show zero behind second?

asked Jun 20, 2019 at 18:00
6
  • What do leap seconds have to do with anything? Commented Jun 20, 2019 at 18:02
  • @Majenko sorry there was a misunderstanding, updated the question. Commented Jun 20, 2019 at 18:06
  • Your problem is that you are printing 1. People don't do that for time. You use 01. Commented Jun 20, 2019 at 18:06
  • how to use "01"? is there a setting i have to change? Commented Jun 20, 2019 at 18:08
  • 2
    If the seconds are less than ten, then print a 0. Then print the seconds. It's that simple. The library has nothing to do with it. It's what you do with the number in your code that matters. Commented Jun 20, 2019 at 18:08

1 Answer 1

1

It's your responsibility to format the time properly, not the library. The library just gives you a number. If you want a leading zero then it's up to you to provide that leading zero.

For example:

if (now.seconds() < 10) {
 lcd.print("0");
}
lcd.print(now.seconds());

Or:

char timestr[9];
snprintf(timestr, 9, "%02d:%02d:%02d", now.hours(), now.seconds(), now.minutes());
lcd.print(timestr);
answered Jun 20, 2019 at 18:12
6
  • what's snprintf? it's giving me this error: expected constructor, destructor, or type conversion before '(' token Commented Jun 20, 2019 at 18:20
  • 1
    @newbie: It's a very standard libc function. Commented Jun 20, 2019 at 18:25
  • @EdgarBonet i'm sorry but what's the error is about? Commented Jun 20, 2019 at 18:45
  • 1
    Did you use the code inside a function or just dump it into global scope? Commented Jun 20, 2019 at 18:45
  • 1
    Put it in loop() where you want to display the time. Commented Jun 20, 2019 at 18:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.