1

This is my code.

#include <U8glib.h> 
U8GLIB_SSD1306_128X64 u8g(12, 11, 8, 9, 10);
void clear_screen();
const int pageCount = 1;
int p;
void (*pages[pageCount])() = {clear_screen};
int duration [pageCount] = {2000};
void setup() {
  
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1);
  p = 0;
  Serial.begin(9600); 

}

void loop()
{
u8g.firstPage();
  do {  
    (*pages[p])();
  } while( u8g.nextPage() );
  delay(duration[p]);
  p = p+1;
  if (p == pageCount)
    p=0;
}
void clear_screen()
{
  u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 20, "BP");
  }

This is the output is a floating text which says BP starts at 20 y length and progresses towards the right after every page refresh. and moves 20 y length down as soon as it reaches the end of the OLED Check this video out. Video to the Error

  • 1
    I don't know the answer, but it's common to use the module (%) operator in `p = p+1; if (p == pageCount) p=0;`. So you get `p = (p + 1) % pageCount;` – Michel Keijzers Feb 11 '21 at 11:28
  • I have run your code, removing the `(12, 11, 8, 9, 10)` portion, because the display I used is I2C. It does not show the behaviour you describe. It shows "BP" in the upper-left corner where it remains indefinitely. – timemage Feb 11 '21 at 16:20
  • Do you understand how the code really work? Why do you call the function `clear_screen` while it is actually draw a string? why the need to use function pointer `(*pages[p])();` when it can simply call `clear_screen()`? I don't use u8glib but I can tell from the code that it will always draw the string at fix location at `0, 20` as @timemage point out, if you want to screen moving across the screen, you need to pass-in a variable `u8g.drawStr( 0, y, "BP");`. – hcheung Feb 12 '21 at 01:35

0 Answers0