Aligning, hiding and replacing text using U8g2lib

I'm using U8g2lib to show certain data from GPS sensor on the oled screen.

One of the behaviors I wish to achieve has to do with speed value, coming from this line where GPS distance between points is calculated.

Serial.print(shortestDist, 0);

However, I only want to show 3-digit distance. So if the calculated distance is more than 999 or less than 100, I would like to print some text instead of numbers.

Also, on another part of the OLED I want to print another set of values from the GPS but for aesthetic sake I want the numbers to be aligned to the right, default alignment is to the left.

Thanks!

Serial.print(shortestDist, 0);

Does that really print to the OLED ?
What is the purpose of the 0 parameter ?
What data type is shortestDist ?

Can you understand why we ask that users post their complete sketch when asking questions about it ?

Please use code tags (the </> icon) when you post your sketch

It's over 700 lines of code that I have, I really didn't want to post that tower only to figure the print function on that one value.

The print works properly and I take few other values from the GPS, for example this one as well

u8g2.print(gps.satellites.value());

So this puts the number of satellites on the screen, and what I'd like to know is how to limit the number displayed on the screen - I don't want any print if it's under double digits, for example. So that's what I was asking how to approach.

if( value > 99 && value < 999) Serial.print(value);
else Serial.print("Text");
1 Like

So post a smaller but complete example of the problem

I assume that you have tried something like this

if (gps.satellites.value() > 9)
{
  u8g2.print(gps.satellites.value());
}
else
{
  u8g2.print("Some text");
}
1 Like

I always forget to mention it since I find it redundant but I'm a copy paste newbie and frankly most of the code is just borrowed from other projects on instructable and such, I feel it's big enough of achievement for me when I manage to "merge" them and get them to work in the first place :smiley: this is a hobby for me, I would love to learn to code, but the desire to finish the project and get it to work always overcomes :smiley:

Does the 700 lines of code not already contain at least one if/else statement that you could garner ideas from ?

Perhaps but I find it bit hard where to look - but with the examples posted here I understand the logic and I just managed to get a lock with the GPS and the code seems to be working! Thanks!

You could start by searching your 700 line sketch for the word 'else'

Sorry, but what are you hoping to write if you don't even know your code?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.