Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

OLED library - using % formatting
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

OLED library - using % formatting

by AnderEpos on Tue Oct 13, 2020 3:37 am

Good morning,

I am wondering how I can use the % formatting functions in the display.println() function.

I wish to get this result: 00:00 --> filler 0s. I am trying to use ("%2d",minutes)

The full code segment:
Code: Select all | TOGGLE FULL SIZE
     display.clearDisplay();
     display.setTextSize(1);             // Normal 1:1 pixel scale
     display.setTextColor(SSD1306_WHITE);        // Draw white text
     display.setCursor(0,0);             // Start at top-left corner
     display.println("Interrupt");
     display.print("%2d",minuten);
     display.print(" : ");
     display.println("%2d",sekondes);
     display.display();


Can anybody help please?

Thanks

AnderEpos
 
Posts: 4
Joined: Tue Oct 13, 2020 3:19 am

Re: OLED library - using % formatting

by dastels on Tue Oct 13, 2020 10:43 am

You can use sprintf to do the formatting and print the result to the display. E.g.
Code: Select all | TOGGLE FULL SIZE
#include <stdio.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);
  // put your setup code here, to run once:
  char buf[64];
  sprintf(buf, "%d", 43);
  Serial.println(buf);
}

void loop() {
  // put your main code here, to run repeatedly:

}


Just make sure you make the buf array of characters big enough to hold the longest possible formatted string + 1 (for the string terminator).

Dave

dastels
 
Posts: 3767
Joined: Tue Oct 20, 2015 3:22 pm

Re: OLED library - using % formatting

by AnderEpos on Tue Oct 13, 2020 12:06 pm

Thanks Dave. Just what I wanted

AnderEpos
 
Posts: 4
Joined: Tue Oct 13, 2020 3:19 am

Please be positive and constructive with your questions and comments.