Go Down

Topic: removing leading zero from an LCD (Read 1 time) previous topic - next topic

0miker0


I have cobbled together but do not fully understand code to remove the leading zero from and LCD. Right now this works for removing the leading zero:

      if(temp_value < 10)
      {
       display_value[1] = display_value[0];
       display_value[0] = ' ';
      }

If someone could explain how this works and if it the best way to go about doing it this way it would be great. After that I would like to remove the leading zero if the counter goes below 100 and then below 10. Right now I have no idea.

Thanks,
Mike

PaulS

Quote
If someone could explain how this works and if it the best way to go about doing it this way it would be great.

Once something gets printed on the LCD, it stays there until something new is written over it. That new thing can be a space, which erases the character that was there.

In that code, the value is converted to a string (presumably). So, 13 gets convert to '"13". 9 gets converted to "09", apparently.

That code then leaves "13" alone, but moves the 9 to the 0 position in "09", and then puts a space in the second position, so the string becomes "9 ".

Seeing the rest of the code would be useful.

Is it the best way? No. Formatting the string correctly in the first place would be better/faster/easier.

floresta

Quote
Is it the best way? No. Formatting the string correctly in the first place would be better/faster/easier.


And in either case you may want the space(s) before the single (or two) digit numbers, not after them.

Don


0miker0

Thanks for the help PaulS, I understand how you explained removing the zero for the less then 10 code that was working fine. I modified the code to remove the zero below 100 and it works but now messes up the remove zero below 10. A video of what I have now is here:
http://www.youtube.com/watch?v=WpN1QF2dWvM

The code is:

if(tmpred_value < 100) //chops off leading zero
      {
       red_value[2] = red_value[1];
       red_value[1] = red_value[0];
       red_value[0] = ' ';
      }
     
   if(tmpred_value < 10) //chops off leading zero
      {
       red_value[1] = red_value[0];
       red_value[0] = ' ';
      }

floresta

It looks like you are using a graphical LCD and not a character LCD as I (and probably Paul) had originally assumed but I don't think this makes any difference.  In any case you may be better off by formatting the string so that it always has three characters.  Then you insert one or two spaces before your actual numerical values as appropriate.  This will also have the effect of 'right justifying' your display instead of 'center justifying' it as you are presently doing.

Don


0miker0

Inserting spaces did not fix my problem. Like most of my programming it now works by accident. I have no idea yet why it this works, but it does:

  if(tmpred_value < 100) //chops off leading zero
      {
       red_value[2] = red_value[1];
       red_value[1] = red_value[0];
       red_value[0] = ' ';
      }
     
      if(tmpred_value < 10) //chops off leading zero
      {
       red_value[2] = red_value[1];  //was 1, 0
       red_value[1] = ' ';                 //was 0
      }

Thanks for the help,
Mike

PaulS

Quote
Inserting spaces did not fix my problem. Like most of my programming it now works by accident. I have no idea yet why it this works, but it does:

If you showed us the code that converts tmpred_value (an int) to red_value (a string), we could show you how to ensure that red_value was always the same length, and had the leading or trailing spaces like you want, in one step, instead of half a dozen steps.

0miker0

You are correct. That probably would have made things a bit easier if I did that in my first post. Here is the working code now with my itoa conversion:

while(menu_item_location <10)
     {
      tmpred_data = read_encoder();
      tmpred_value = tmpred_value + tmpred_data;
      itoa(tmpred_value, red_value, 10);
     
      if(tmpred_value < 100) //chops off leading zero
      {
       red_value[2] = red_value[1];
       red_value[1] = red_value[0];
       red_value[0] = ' ';
      }
     
      if(tmpred_value < 10) //chops off leading zero
      {
       red_value[2] = red_value[1];
       red_value[1] = ' ';
      }
     
      uoled.TextGraphic(110,60,SMALL_FONT,0x3666,1,1,  red_value,1);

I tried inserting spaces where ever I could but nothing changed.

Mike

PaulS

This is the code that converts the int to a string:
Code: [Select]
itoa(tmpred_value, red_value, 10);
It does an UNFORMATTED conversion.

Code: [Select]
sprintf(red_value, "%3d  ", tmpred_value);
would do the same thing, except as a FORMATTED conversion.

The part in the quotes is a format specifier. The % defines where a format specifier starts (where a variable's value will be substituted). The d says that the value is an int. The 3 says that we want the resulting value to have a minimum of 3 characters, with leading spaces as required ("  3", " 30",  "255", "1023"). The two spaces on the end are copied as-is to the output. So, you will get 5 characters (plus a trailing NULL) in the output - at a minimum.

You can remove the trailing spaces, if you want. They simply ensure that the string is long enough to cover the previous data, in case you are not using a fixed space font.

Go Up