3

I have Arduino Uno and 0.96" I2C Oled 4 Pinned display. These are my Arduino codes:

#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);    // VDD=5V SCL=A5 SDA=A4

int a = 10;

void setup(void) {
}

void loop(void) {

  u8g.setFont(u8g_font_gdr25r);
  u8g.drawStr(8, 30, "E");
  u8g.setFont(u8g_font_gdr25r);
  u8g.drawStr(30, 30,"l");
  delay(2000);  

  //  u8g.println(a);  //This code raw is not working
  //  delay(2000);
}

I am trying to indicate integer type variable is called "a" to the OLED screen, but u8glib library hasn't got

println( (int) a );

function to do this. I only see

u8g.drawStr();

function that this can't handle integer variables to the display. How can I do that?

5

The U8glib user reference page mentions a couple of ways to print the value of a number using ASCII characters.

Eg, following the idea at the second mention of sprintf at above link:

char buf[9];
sprintf (buf, "%d", a);
u8g.drawStr(33, 33, buf);

Edit: The above follows the example at the link, but I've struck it out because in general snprintf is safer than sprintf. See below.

enum {BufSize=9}; // If a is short use a smaller number, eg 5 or 6 
char buf[BufSize];
snprintf (buf, BufSize, "%d", a);
u8g.drawStr(33, 33, buf);

The other way is via print. (Search for U8GLIB::print in above link, and also see arduino.cc's Print docs).

Eg:

u8g.print(a);

To get a newline effect, you may need to have a variable keeping track of the current line number; add 1 to it to get next line's number, and use that as a position parameter in a drawStr call.

0

Ok, I have found the solution (Thank you @jwpat7) Here is my working code:

#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // VDD=5V GND=GND SCL=A5 SDA=A4

int a = 0;

void setup() {      }

void loop() {

u8g.firstPage(); 
 do {
u8g.setFont(u8g_font_gdr25r);
//u8g.drawStr(8, 30, "Eeee");

char buf[9];
sprintf (buf, "%d", a);
u8g.drawStr(18, 3, buf);

  } while( u8g.nextPage() );
  delay(200); 
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.