How to gett switch and case to work, in my code ???

I'm trying to make a code that's make one momentary button to flip between 3 different "info".

My plan is at start show "info 1", then will the button add value 1 for "info 2".

Butt I don´t understand how Switch and cases work. I just don´t gett this code to work.

#include 
#include 
#include 


int switchPin = 2;   // momentary switch on 2, other side connected to ground
int Info = 0;

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);


void setup(void) {
  u8g2.begin();

 pinMode(switchPin, INPUT);
 digitalWrite(switchPin, HIGH);      // turn on pullup resistor
}

void loop()
{

  
 if (digitalRead(switchPin) == HIGH){  
   delay(500);                        // delay to debounce switch


   Info ++;
   if(Info > 3){
     Info = 1;
   }
   
   switch (Info) {
     case 1: 
        u8g2.clearBuffer();
       u8g2.setCursor(0, 0);
       u8g2.print("First.");
        u8g2.sendBuffer();
       break;
     
       
     case 2: 
        u8g2.clearBuffer();
       u8g2.setCursor(1, 1);
       u8g2.print("Second.");
        u8g2.sendBuffer();
       break;
     
       
      case 3: 
         u8g2.clearBuffer();
        u8g2.setCursor(2, 2);
        u8g2.print("Third.");
         u8g2.sendBuffer();
        break;
      
 }
}
}

I just don´t gett this code to work.

So what does happen when you run the program ?
Do you see anything on the screen ?

You are using INPUT_PULLUP but are testing for the input being HIGH. How is the input wired ?

Thanks for answering, and sorry for my poor english.

The wireing is.

128x64 oled gnd --> Ard. Gnd VDD --> Ard. 5V SCK --> Ard. SCL SDA --> Ard. SDA

Button, momentary one side --> Ard. 5V other side --> Ard. GND (10K ohm) + digital 2

When I run the program I see nothing on screen. I have tried to put information outside "Switch-code-box" and there I see that.

I have tryed to use the button-setup whit an led-diod and can make it turn on/off.

So my conclusion is that it is the code thats bugging me. :-(

My guess is that you are using a library named U8g2lib. Go to the directory where you installed the Arduino IDE and look for a directory name U8g2lib. Look in that directory for another directory named Examples. Pick one and tell us if it works. If not, you may have the display wired incorrectly.

But this code work like a charm, in the same hardware setup.

#include 
#include 
#include 

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup(void) {
  u8g2.begin();
}

void loop(void) {
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawFrame(0,0,128,64);
  u8g2.sendBuffer();
  delay(3000);

  u8g2.drawStr(2,12,"Hello World!"); 
  u8g2.sendBuffer();
  delay(3000);
  
  u8g2.drawStr(2,22,"Good Bye!");
  u8g2.sendBuffer(); 
  delay(3000);  

  u8g2.clear();
  delay(3000);
}

Take a look at the reference material for u8g2lib and the .print() function

https://github.com/olikraus/u8g2/wiki/u8g2reference#print

I believe that you need to specify a font mode and drawing color.

Yes... That could be right. I gonna do some modding of the code and come back...

@CattleDog

YES!!! Your my hero of the day!

I missed that one, becase I was blind looking at the code for the switch/case and button code.

Then it was that simple...

Here is my working code.

#include 
#include 
#include 


int switchPin = 2;   // momentary switch on 2, other side connected to ground
int Info = 0;

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);


void setup(void) {
  u8g2.begin();

 pinMode(switchPin, INPUT);
 digitalWrite(switchPin, HIGH);      // turn on pullup resistor
}

void loop()
{

  
 if (digitalRead(switchPin) == HIGH){  
   delay(500);                        // delay to debounce switch


   Info ++;
   if(Info > 3){
     Info = 1;
   }
   
   switch (Info) {
     case 1: {
        u8g2.clearBuffer();
       u8g2.setFont(u8g2_font_ncenB08_tr);
       u8g2.drawStr(2,12,"First.");
        u8g2.sendBuffer();
       break;
     }
       
     case 2: {
        u8g2.clearBuffer();
       u8g2.setFont(u8g2_font_ncenB08_tr);
       u8g2.drawStr(2,12,"Second.");
        u8g2.sendBuffer();
       break;
     }
       
      case 3: {
         u8g2.clearBuffer();
        u8g2.setFont(u8g2_font_ncenB08_tr);
        u8g2.drawStr(2,12,"Third.");
         u8g2.sendBuffer();
        break;
      }
 }
}
}

If you really do have the pullup resistor turned on then every 500 milliseconds the test condition will be satisfied and Info will be incremented with no need to press the button. Is that what you intend to happen ?

Yes.

This is gonna show data from sensors on my motorcycle.
It’s Volt, Oiltemp and so on.

So I want the info to be dynamic and change every ones in a program cycle.
500 ms is just a taken number.

But I belive that if I reduse the time (longer time) the proram will take less battary? …right?

What is the purpose of the momentary switch on pin 2 ?

To scroll.

If I only want one sett of information.

For exemple if I only wanna see the time clock, big, over the screen. Or what gear the engien is running in. And so on.

OK, but you will need to hold the button in to freeze the information on the screen. Will that be convenient ?

There are ways to use a momentary pushbutton to toggle between states (rolling display/fixed display) if that is what you want.