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?

6
  • What do leap seconds have to do with anything?
    – Majenko
    Jun 20 '19 at 18:02
  • @Majenko sorry there was a misunderstanding, updated the question. Jun 20 '19 at 18:06
  • Your problem is that you are printing 1. People don't do that for time. You use 01.
    – Majenko
    Jun 20 '19 at 18:06
  • how to use "01"? is there a setting i have to change? Jun 20 '19 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.
    – Majenko
    Jun 20 '19 at 18:08
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);
7
  • what's snprintf? it's giving me this error: expected constructor, destructor, or type conversion before '(' token Jun 20 '19 at 18:20
  • 1
    @newbie: It's a very standard libc function. Jun 20 '19 at 18:25
  • @EdgarBonet i'm sorry but what's the error is about? Jun 20 '19 at 18:45
  • 1
    Did you use the code inside a function or just dump it into global scope?
    – Majenko
    Jun 20 '19 at 18:45
  • 1
    Put it in loop() where you want to display the time.
    – Majenko
    Jun 20 '19 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.