Need some help with OLED program please

I’m using an OLED display from Amazon (keyestudio 1.3" 128x64). I’ve gotten it to work ok using the U8GLib library.

What I’m trying to do is get a message to scroll on the screen before the main loop.

This code works as expected. The “Message” scrolls down from top and stops in middle.

#include "U8glib.h"
// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported 
// devices with all constructor calls is here: https://code.google.com/p/u8glib/  

U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9);  // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9

int yPos = 0;

void setup() {  
  
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1); // Instructs the display to draw with a pixel on. 
}

void loop() {  
  u8g.firstPage();
  do {  
    draw();
  } while( u8g.nextPage() );
 
  if(yPos < 40){
    // if it's too slow, you could increment y by a greater number
    yPos++;  }
  else{
    // When the yPos is off the screen, reset to 0.
    yPos = 40; 
  } 
}  
void draw(){
  u8g.drawStr( 16, yPos, "Message" );   
}

So I tried to implement it this way in the setup and it doesn’t work. Can someone help me with this?

#include "U8glib.h"

//**************************************************
// Change this constructor to match your display!!!
// U8GLIB_SH1106_128X64 u8g(4, 5, 6, 7);
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9);  // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//**************************************************

int yPos = 0;

void setup() {  
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1); // Instructs the display to draw with a pixel on. 

  for (int yPos = 0; yPos < 40; yPos++) {
    u8g.firstPage();
    do {    
        draw();
    } while( u8g.nextPage() );
  }
}

void loop() {  
while(1) { }
} 

void draw(){
  u8g.drawStr( 16, yPos, "Message");    
}

Thanks. Don

  for (int yPos = 0; yPos < 40; yPos++) {
    u8g.firstPage();
    do {   
        draw();
    } while( u8g.nextPage() );
  }

What is the point of changing the value in yPos in the for loop, since you never is it in the body of the statement?

The yPos that draw() sees is NOT the loop index.

I thought I was using it as a global variable. So what’s the solution?

I got it to work by changing for (int yPos = 0; yPos < 40; yPos++) to for (yPos = 0; yPos < 40; yPos++)

#include "U8glib.h"

//**************************************************
// Change this constructor to match your display!!!
// U8GLIB_SH1106_128X64 u8g(4, 5, 6, 7);
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9);  // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//**************************************************

int yPos = 0;

void setup() {  
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1); // Instructs the display to draw with a pixel on. 

  for (yPos = 0; yPos < 40; yPos++) {
    u8g.firstPage();
    do {    
        draw();
    } while( u8g.nextPage() );
  }
}

void loop() {  
while(1) { }
} 

void draw(){
  u8g.drawStr( 16, yPos, "Message");    
}