Hello
i'm trying to print time hours/minus/seconds on lcdkeyboardshield using library LiquidCrystal.h in base of millis().
But after reset the second, minus after 60 to 0, i got that the second digit keep stay there but not cleaned.
Here is the clip
Here is the code i wrote
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
byte second = 0;
byte minus = 0;
byte hour = 0;
byte day = 0;
void setup()
{
lcd.begin(16,2);
}
void loop()
{
long pulse = millis()/1000;
//SECOND and reset after 60 second
if (pulse < 60) {second = pulse;} else {second = pulse - ((pulse/60)*60);}
//MINUS and reset after 60 minus
if (pulse < 3600) {minus = pulse/60;} else {minus = (pulse/60) - ((pulse/3600)*60);}
//HOUR and reset after 24 hours
if (pulse < 86400) {hour = pulse/3600;} else {hour = (pulse-((pulse/86400)*86400))/3600;}
//DAY
day = pulse/86400;
//Print time on screen
lcd.setCursor(0,0);//PULSE
lcd.print(pulse);
lcd.setCursor(8,0);//HOURS
lcd.print(hour);
lcd.setCursor(10,0);
lcd.print(":");
lcd.setCursor(11,0);//MINUS
lcd.print(minus);
lcd.setCursor(13,0);
lcd.print(":");
lcd.setCursor(14,0);//SECONDS
lcd.print(second);
}
Prior to printing a new value to a specific spot on your display, print a few (maybe 2 or 3 ?) spaces to that same spot.
That erases the old content.
Don't forget to put your cursor back at the right spot before printing the value after erasing the old content.
the print() function prints the number and nothing more.
So if you have a number less than 10 it prints 1 digit:
but when the number is greater than 10 it prints 2 digits:
So the output is:
0
1
2
3
4
5
6
7
8
9
10
.....
58
59
09
19
This is because the 2nd digit (the 9) from 2 digit number 59 is still there and
the new single digit number is only overwriting where the 5 was.
The easiest thing to do is put in some extra code to print an extra 0 character
whenever the number is less than 10.
This will need to be done for all the positions, hour, minus, seconds
That way you end up with
00
01
...
58
59
00
01
Alternatively you could use sprintf() to format a buffer then print the buffer.
i.e.
char buf[10]; // buffer
sprintf(buf, "%02d:%02d:%02d", hour, minus, second); // format the buffer
lcd.setCursor(8,0);
lcd.print(buf);
That format string will format a fixed with buffer and ensure that the numbers
are always 2 digits and are zero filled when the number is less than 10.