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