Pages: [1]   Go Down
Author Topic: Wrong printing on LCDKeyboardShield with LiquidCrystal library  (Read 116 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
#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);
}

Anyone fixed that problem?
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 35
Posts: 1299
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi and welcome.

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.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

i.e. for the hour
Code:
  lcd.setCursor(8,0);//HOURS
  if(hour < 10)
    lcd.print('0');
  lcd.print(hour);


Alternatively you could use sprintf() to format a buffer then print the buffer.
i.e.
Code:
char buf[10]; // buffer

Code:
  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.

--- bill
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's exactly what i looking for! thank you very much!!

btw the language that i writing in arduino program is C ? that mean i can use all code from C to write ?

thanks again!  smiley-lol
Logged

Pages: [1]   Go Up
Jump to: