-
Hello! I'm a very new at tinkering with this and I managed to get my 128*64 OLED work nice with the u8g2 library. In my sketch I have two numbers that are being calculated and printed on the screen. For aesthetic sake, I'd like them to be aligned to the right, as I think it looks nicer when they change from single to double digits by adding a number to the left of the first one. I am using two separate counters being printed on two locations on the screen, one font used is u8g2_font_pxplusibmvga8_mn and another is u8g2_font_logisoso26_tn if that is of any importance. Thanks for the help! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
u8g2.drawStr(x-u8g2.GetStrWidth(s), y, s); https://github.com/olikraus/u8g2/wiki/u8g2reference#getstrwidth |
Beta Was this translation helpful? Give feedback.
-
Not tested: void loop(void) {
int x = 100 - (millis()/100)%101;
String xs = String(x);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_logisoso26_tn);
u8g2.setCursor(34-u8g2.getStrWidth(xs.c_str()), 36);
u8g2.println(xs.c_str());
} while ( u8g2.nextPage() );
} Reference: https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/ Edit: fixed "getStrWidth" |
Beta Was this translation helpful? Give feedback.
-
I solved this by this way:
|
Beta Was this translation helpful? Give feedback.
Not tested:
Reference: https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/
Edit: fixed "getStrWidth"