u8g is undefined? :(

I am making something and while I was writing the code (in visual studio with platformio) I got this error:

identifier "u8g" is undefined

code:

#include <Arduino.h>
#include "U8glib.h"
#define SW 10
#define CLK 8
#define DT 9 
void draw(void) {
  u8g.setFont(u8g_font_helvB12);
}

void setup()
{
  String item1 = "Hello world!";
  String item2 = "hi";
  String item3 = "planets";
pinMode(DT, INPUT);
pinMode(SW, INPUT);
pinMode(CLK, INPUT);
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
}

I installed the library through the library manager in the platformio extension.

I don't see an object named u8g defined anywhere in the sketch

Something like

U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);

would normally be present

The error is correct.

Check some of the examples in the U8glib library to see the bit you have missed.

UKHeliBob:
I don't see an object named u8g defined anywhere in the sketch

Something like

U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);

would normally be present

yeah I forgot I'm blind (+1 karma)

If all else fails believe the error message :slight_smile:

new question: how do I display a string on the oled?

The U8G2 base class inherits from print:

class U8G2 : public Print

So, it knows how to print everything that any other Print object does.

bennetmasterled:
new question: how do I display a string on the oled?

The print() function would be the obvious way

I'll try again but it usually doesn't work

Post a short "Hello World" sketch whether it works or not

ok nvm I DO know to use use u8g.print()

#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK);

String hi = "Hello world";
void draw(void) { 
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 22, "Hello World!");
}

void setup(void) {
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3); 
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  pinMode(8, OUTPUT);
}

void loop(void) {
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  u8g.print(hi);
}

That library is using a frame buffer for the display. All the print commands need to be within a loop formed by the following code:

  u8g.firstPage(); 
  do {
    // all print statements go here, or a call to a function containing the print statements
  } while( u8g.nextPage() );

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