LCD Screen

Hello,

I have the Orangutan X2 with the LCD screen bundle and I wanted to know if it’s possible to print decimal values on the LCD screen. So far, every time I try to print decimal values I get bizarre characters from questions marks to others. I looked into the Pololu LCD library as well and didn’t see any print functions for decimals, only for chars and ints.

Is it safe to say that printing decimal values is a no go?

Hello.

It is possible to display decimal values onto the LCD screen. You might take a look at this post on how to display floating point numbers using printf as well as this post, which suggests the same method but in more detail. Note that linking the floating point version of printf will use significantly more program memory.

Alternatively, if you want to avoid using printf, you could try something like the following code (I tested it on an Orangutan SVP-1284 Robot Controller and it worked.):

void print_decimal(float x)
{
  if (x < 0)
  {
    print_character('-');
    x = -x;
  }
  
  long decimal_part = abs((long)(x * 1000) % 1000); // (up to 3 decimal points)

  print_long(x);                                    // print integer part
  print_character('.');                             // print decimal point
  if (decimal_part < 100) { print_character('0'); } // first leading 0 if necessary
  if (decimal_part < 10)  { print_character('0'); } // second leading 0 if necessary
  print_long(decimal_part);                         // print decimal part 
}

- Amanda

Couldn’t get the other ways to work, but your little trick did the job! Although it doesn’t work for the negative numbers…but thanks!

Amanda and I realized that the code example she originally posted had a few problems, such as when the decimal part of the number had leading zeroes (e.g. 23.001). I’ve edited her post to provide an improved example that should work better in that case, and it should also handle negative numbers better now.

- Kevin

Thank you, it works very well!