59 - 09 in LCD and RTC

I am using this library using RTC DS1302 (Virtuabotix is under construction) and LCD library provided for I2C. But whenever my timer reaches 59 (for minutes and hours), my LCD prints "09" instead of "00". I think there's something wrong with the LCD library. Can someone edit it for me coz I don't know how to. From 59 to 09, then 19, then 29, and so on. But it get backs to its old counting when it reaches 10.. :expressionless: Thanks in advance!

my LCD prints "09" instead of "00". I think there's something wrong with the LCD library.

Nothing wrong with the LCD library.
The value it is printing is just a single 0 and the 9 is left over from printing the 59.
A quick fix is to always print a blank space after printing any number.
Slightly better fix is to print a space after a number only if the number is less than 10.

Grumpy_Mike:

my LCD prints "09" instead of "00". I think there's something wrong with the LCD library.

Nothing wrong with the LCD library.
The value it is printing is just a single 0 and the 9 is left over from printing the 59.
A quick fix is to always print a blank space after printing any number.
Slightly better fix is to print a space after a number only if the number is less than 10.

I think it works fine now. Thanks sir! :slight_smile:

I believe that sounds like the same problem I had seen in past. I do not think it is error in library, but in the main code. What is occurring is the "9" is not clearing from the right digit location on reaching 59, since the next number in sequence is just a single digit it fills in left most instead of right most. so does not over right the two location just the one. Of course when you reach "10" it does over right both.
If you like the single number '0', '1', '2'...'9' then you will need to clear the whole line prior to writing the new number.
If you prefer having a leading zero before the single digit '00', '01', '02,...'09' then modify the output to include that digit added thus forcing the new number to over write the old 9.

   //show time
   
    lcd.setCursor(4,1);
    //show hours
    //Add a zero for hours under ten
   switch (now.hour()) {
     case 0:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
     case 1:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
    case 2:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
    case 3:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
     case 4:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
      case 5:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break; 
     case 6:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
     case 7:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;
     case 8:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break; 
    case 9:
      lcd.print(0);
      lcd.print(now.hour(), DEC);
      break;  
    default: 
    lcd.print(now.hour(), DEC); 
   }
    lcd.print(':');
    //show minutes
    //Add a zero for minutes under ten
   switch (now.minute()) {
     case 0:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
     case 1:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
    case 2:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
    case 3:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
     case 4:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
      case 5:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break; 
     case 6:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
     case 7:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;
     case 8:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break; 
    case 9:
      lcd.print(0);
      lcd.print(now.minute(), DEC);
      break;  
    default: 
    lcd.print(now.minute(), DEC); 
   }
    lcd.print(':');
    //show seconds
    // Add a zero for seconds under ten
    switch (now.second()) {
     case 0:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
     case 1:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
    case 2:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
    case 3:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
     case 4:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
      case 5:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break; 
     case 6:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
     case 7:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;
     case 8:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break; 
    case 9:
      lcd.print(0);
      lcd.print(now.second(), DEC);
      break;  
    default: 
    lcd.print(now.second(), DEC); 
   }
1 Like

@spicetraders
Why are you:-

  1. Repeating the fix I posted when the OP understood it and has fixed his code.
  2. Using such stupidly long code to do it. A simple if statement will do, not miles of case statements.

If you like the single number '0', '1', '2'...'9' then you will need to clear the whole line prior to writing the new number.

It's simpler than that:

Old:

    lcd.setCursor(4,1);
    lcd.print(now.hour(), DEC);

New:

    lcd.setCursor(4,1);
    lcd.print("  ");
    lcd.setCursor(4,1);
    lcd.print(now.hour(), DEC);

Don

Thank you so much Grumpy_Mike and floresta. :slight_smile: It works fine now. @spicetraders thanks for the suggestion but it works now. :slight_smile:

@grumpy gee you really are and your post was not there when i wrote.
not always one way to do things.
adds an example of switch and case.
as stated adds leading '0"

not all ideas are right or wrong they just exist as do I and grumpy_mike

. . . and your post was not there when i wrote.

You both were probably composing your posts at the same time but Mike finished and posted before you did.

If you do a [Preview] before you actually [Post] you not only get a chance to see what your information will look like but if someone has made a new post you will get a message to that effect.

Don

Still missing the point, there is more then one solution and I offered a different view (adding leading '0' that did not need sarcastic BS.
So even if I seen his post I would have added to the post.......

Still missing the point, there is more then one solution and I offered a different view

Sure there is more than one route to get between New York and Boston. But going through Las Vegas is nether practical nor sensible.

Unless you gamble.
Again for every case there is more then one path, some want coffee some want tea.
24hr military time missing a zero would be trouble and could be disastrous. So some folks seek the extra zeros.
I fully understand you think inside the box that you know, but others think on the other side of the box wall..
If all that is presented to the world is one side one village then they may never know the option and choices in the outside world.
If the flat earth folks had their way we never would have across the pond existing.

If you do not get my point now then you just never will.

If you do not get my point now then you just never will

And I will say exactly the same to you.

What is the point of posting code that is a hundred times longer than it need be? It only shows that you have absolutely no idea of how to code. Yes it is another point of view but it is so inept as to be not worth posting.

then post your way of making zeros so we have the ultimate way and learn from our errors

From the original post:

I think there's something wrong with the LCD library.

I have not examined the library but I am familiar with the clock chip. The clock is configured to return information in a packed binary coded decimal format. This means for example that the 'minutes' data comes back in a single byte with a four-bit binary number representing the 'tens of minutes' in the upper nibble and another four-bit binary number representing the 'ones of minutes' in the lower nibble.

It is the library that is apparently filtering out any leading zeros and in that respect one could conclude that there is something wrong with the library. This is the problem that you run into when using library code. You gain the advantage of not having to write the code yourself but you must also live with the fact that the author of the library might not exactly think the way you do.

Along the same lines, the clock is capable of running in the 24 hour mode but I don't know if this library is set up to use that mode. There's more to it than merely displaying the leading zeros along with the hours from 13 to 23.

Don

An interesting thought and place to explore.

spicetraders:
then post your way of making zeros so we have the ultimate way and learn from our errors

OK here:-

void displayTime(){
  lcd.setCursor(4,1);
  aPrint(now.hour());
  lcd.print(':');
  aPrint(now.minute());
   lcd.print(':');
 aPrint(now.second()); 
}

void aPrint(int number){
  if(number < 10) lcd.print(0);
  lcd.print(number);
}

See how easy that was instead of slinging mud.
I will give it a go and see if I like it.