u8g library... how it works?

hi all,
I'm newbie with this library. I started to learn how work Arduino with 20x4 display but now i'm trying to do something with a bigger display 128x64 ST7920.
I've found online an example who stamps to different screens in loop, i would to stamp it at the press of a button but I'm not able to do it :confused:

#include "U8glib.h"
// setup u8g  
 U8GLIB_ST7920_128X64_1X u8g(18, 16, 17);

const int buttonPin = A0;     
int buttonState = 0;       

void setup() {  
  u8g.setFont(u8g_font_unifont);
  pinMode(buttonPin, INPUT);
}

void loop() {  
  u8g.firstPage();
  do {  
    draw();    
  } while( u8g.nextPage() );
  delay(1000); 
  u8g.firstPage();
   
  do {  
    draw2();    
  } while( u8g.nextPage() ); 
 delay(1000); 
}
  

void draw(){
  u8g.drawStr( 0, 40, "First Page");    
}

void draw2(){
  u8g.drawStr( 0, 40, "Second Page");    
}

I tryed in many different ways but no one works right.

thanks for your patience

I've found online an example who stamps to different screens in loop

What does "stamps" mean? The library draw...

i would to stamp it at the press of a button

You have no code to do that.

but I'm not able to do it

The code you posted does something. You expect it to do something. That you posted here implies that what it does is not what you expect, but is does not tell us anything about what it does or what you expect or how they differ.

I did in this way and it works

#include "U8glib.h"

U8GLIB_ST7920_128X64_4X u8g(18, 16, 17);	// SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17

const int buttonPin = A3; 
int val = 0;  
int old_val = 0;  

int buttonState= 0;
  
void draw(void) {

  val = digitalRead(buttonPin);
  if ((val == HIGH) && (old_val == LOW)) {
   buttonState = 1 - buttonState;
  delay(10);
  }
  
old_val = val;
  
  if (buttonState == 1) {   
    draw2();
  } else {
    draw1();
  }
}

void draw1(void) {
  // graphic commands to redraw the complete screen should be placed here  
  //u8g.setFont(u8g_font_unifont);
  u8g.setFont(u8g_font_fub14);
  u8g.drawStr( 0, 22, "First Page");

}

void draw2(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_helvB12);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 22, "Second Page");  
  
}
void setup(void) {
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);
pinMode (buttonPin, INPUT);
 
}

void loop(void) {
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  delay(50);
}

... but if I would add more pages?

... but if I would add more pages?

Then you should dig into the library, to find out how to tell it that there are more pages.

What you have now is a kludge. Not a bad one, but not a good one, either.

PaulS:
Then you should dig into the library, to find out how to tell it that there are more pages.

What you have now is a kludge. Not a bad one, but not a good one, either.

that's true but is the only way i found at the moment :stuck_out_tongue:
I'll learn more about the u8g library, the problem is that I'm a beginner and everything is new and difficult for me.
:sweat_smile: