problem with button interface, and 128x64 oled code

Hello! in my code i’m trying to get my display to switch to the next “page” when I push a button, but i’m clearly doing something wrong. the display only shows the first “page” I have set up. any help would be awesome. This is the first big sketch I have ever attempted (albeit it’s not that big of a sketch.)

#include <SPI.h>                          //oled libraries
#include <Wire.h>                         // "
#include <Adafruit_GFX.h>                 // "
#include <Adafruit_SSD1306.h>             // "

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);     //define display

int x;                                   //used to switch between function screens
int buttonOne = 4;
int buttonTwo = 5;
int led = 6;                            //led pin on board
int fan = 7;                            //fan pin on board
int laser = 8;                          //laser pin on board
int debounce = 500;                     //universal wait after button push

void setup() {
  Serial.begin(9600);                   //begin serial communications for display
  pinMode(buttonOne, INPUT_PULLUP);     //use internal resistors
  pinMode(buttonTwo, INPUT_PULLUP);     //connect pin to ground via switch/button
  pinMode(laser, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c); //initializes display with logo ****make sure line doesn't read 0x3D but instead 0x3c****
  display.display();                         //must use display.display(); after every display command
  delay(1000);
  display.clearDisplay();                    //to avoid weird characters try to clear display befor updating text
}

void loop() {
  if (digitalRead(buttonOne) == LOW) {
    x++;
    delay(debounce);                                  //crude debounce
  }


  if (x == 0) {
    mainDisplay();
  }

  if (x == 1) {
    laserDisplay();                                          //displays the laser info screen
    if (digitalRead(buttonTwo) == LOW) {
      toggleLaser();
      delay(debounce);
    }
  }

  if (x == 2) {
    ledDisplay();                                              //displays the LED info screen
    if (digitalRead(buttonTwo) == LOW) {
      toggleLED();
      delay(debounce);
    }
  }

  if (x == 3) {

    fanDisplay();                                           //displays the fan info screen
    if (digitalRead(buttonTwo) == LOW) {
      toggleFan();
      delay(debounce);
    }
  }

  if (x == 4) {
    int x = 0;           // equal to zero so there is a buffer where nothing will be affected
  }


}




/*
   Main display screen code===================================================================================================
*/

void mainDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");

  display.setTextColor(WHITE);
  display.println("Laser");
  if (digitalRead(laser) == HIGH) {
    display.setTextColor(WHITE);
    display.setCursor(64, 8);
    display.println("ON");
  }
  else {
    display.setTextColor(WHITE);
    display.setCursor(64, 8);
    display.println("off");
  }
  display.setTextColor(WHITE);
  display.setCursor(0, 16);
  display.println("led");
  if (digitalRead(led) == HIGH) {
    display.setTextColor(WHITE);
    display.setCursor(64, 16);
    display.print("ON");
  }
  else {
    display.setTextColor(WHITE);
    display.setCursor(64, 16);
    display.print("off");
  }

  display.setTextColor(WHITE);
  display.setCursor(0, 25);
  display.println("Fan");
  if (digitalRead(fan) == HIGH) {
    display.setTextColor(WHITE);
    display.setCursor(64, 25);
    display.print("ON");
  }
  else {
    display.setTextColor(WHITE);
    display.setCursor(64, 25);
    display.print("off");
  }

  display.display();
  delay(200);
  display.clearDisplay();
}

/*
   Laser code==========================================================================================================
*/

void laserDisplay() {

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");

  display.setTextColor(WHITE);
  display.println("Laser");
  if (digitalRead(laser) == HIGH) {
    display.setTextColor(WHITE);
    display.setCursor(64, 0);
    display.print("ON");
  }
  else {
    display.setTextColor(WHITE);
    display.setCursor(64, 0);
    display.print("off");
  }
  display.clearDisplay();
}
//==================================
void toggleLaser()  {
  if (digitalRead(laser) == LOW) {
    digitalWrite (laser, HIGH);
    laserDisplay();
  }
  else {
    digitalWrite(laser, LOW);
    laserDisplay();
  }
}

/*
   LED code =========================================================================================================
*/

void ledDisplay() {

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");

  display.setTextColor(WHITE);
  display.println("led");
  if (digitalRead(led) == HIGH) {
    display.setTextColor(WHITE);
    display.setCursor(64, 0);
    display.print("ON");
  }
  else {
    display.setTextColor(WHITE);
    display.setCursor(64, 0);
    display.print("off");
  }
  display.clearDisplay();
}

void toggleLED() {
  if (digitalRead(led) == LOW) {
    digitalWrite (led, HIGH);
    ledDisplay();
  }
  else {
    digitalWrite(led, LOW);
    ledDisplay();
  }
}


/*
   Fan code =========================================================================================================
*/

void fanDisplay() {

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");

  display.setTextColor(WHITE);
  display.println("Fan");
  if (digitalRead(fan) == HIGH) {
    display.setTextColor(WHITE);
    display.setCursor(64, 0);
    display.print("ON");
  }
  else {
    display.setTextColor(WHITE);
    display.setCursor(64, 0);
    display.print("off");
  }
  display.clearDisplay();

}

void toggleFan() {
  if (digitalRead(fan) == LOW) {
    digitalWrite(fan, HIGH);
    fanDisplay();
  }

  else {
    digitalWrite(fan, LOW);
    fanDisplay();
  }
}

No experiencve with the display but your clear the display at the end of each display function. Are you supposed to do that?

Only exception is the end of MainDisplay where you first call display.display and wait 200 ms before clearing it again.

void mainDisplay() {
  ...
  ...
  display.display();
  delay(200);
  display.clearDisplay();
}

This is a problem

  if (x == 4) {
    int x = 0;           // equal to zero so there is a buffer where nothing will be affected
  }

You are creating a new, local variable, only visible within the if() statement. You then assign it the value 0. After the if statement is done, that variable goes away. It has nothing to do with the global variable x (except, it has the same name which is confusing to humans, put perfectly acceptable to compilers).

If you want to assign to the global variable, just do so, without the type declaration

  if (x == 4) {
    x = 0;           // equal to zero so there is a buffer where nothing will be affected
  }
display.display();

This kind of stuff. Reusing names. will do nothing but increase your mental load. They allow naming things to lighten our mental loads.

Also, resetting the color to WHTE every time is probably not necessary. I'd set up my paramiters, location, color once, then figure out what I was going to put there. Just less code to wade through when trying to figure out what is going wrong.

And, why are you erasing everything you do after 2 tenths of a second?

delay(200);
  display.clearDisplay();

-jim lee

I know this is old, but i figured i should share my new code in the event it helps someone else. I now have everything working like I wanted

/*
   multi function display for halo helmet
   created by: Caleb Fink
   Libraries from public domain
   Start date: dec 20, 2018
   Last updated: 4,17,19


   transistors used to power all components from 6v-12v dc
   (currently using 6v form 4 AA)

   oled I2c connections:
   SDA = A4
   SCL = A5

    transistors (2n3904 w/ 1k on base)
    laser = D8
    Fan   = D7
    LED's = D6

    inputs (using pullups)
    Button1 = Gnd - D4
    Button2 = Gnd - D5
    6v = vcc
    
*/



#include <SPI.h>                          //oled libraries
#include <Wire.h>                         // "
#include <Adafruit_GFX.h>                 // "
#include <Adafruit_SSD1306.h>             // "

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);     //define display

int x = -1;                                   //used to switch between function screens
int buttonOne = 4;
int buttonTwo = 5;
int led = 6;                            //led pin on board
int fan = 7;                            //fan pin on board
int laser = 8;                          //laser pin on board
char laserState[4];
char ledState[4];
char fanState[4];
int debounce = 100;                     //universal wait after button push
int tune = 100;



void setup() {
  Serial.begin(9600);                   //begin serial communications for display
  pinMode(buttonOne, INPUT_PULLUP);     //use internal resistors
  pinMode(buttonTwo, INPUT_PULLUP);     //connect pin to ground via switch/button
  pinMode(laser, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c); //initializes display with logo ****make sure line doesn't read 0x3D but instead 0x3c****
  display.display();                         //must use display.display(); after every display command
  delay(1000);
  display.clearDisplay();                    //to avoid weird characters try to clear display befor updating text
        laserState[0] = 'O';
        laserState[1] = 'F';
        laserState[2] = 'F';
        laserState[3] = 0;
            fanState[0] = 'O';
            fanState[1] = 'F';
            fanState[2] = 'F';
            fanState[3] = 0;
              ledState[0] = 'O';
              ledState[1] = 'F';
              ledState[2] = 'F';
              ledState[3] = 0;



  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Welcome");
  display.println("Cortana");
  display.setCursor(0, 16);
  display.println("Proto");
  display.setCursor(0, 25);
  display.println("Press B1 to continue");
    display.setCursor(64, 8);
    display.println("V 0.1");
    display.setCursor(64, 16);
    display.print("MK 1.1");
    display.display();
    delay(200);






}

void loop() {
  if (digitalRead(buttonOne) == LOW) {
    x++;
    delay(debounce);                                  //crude debounce
  }


  if (x == 0) {
    mainDisplay();
    Serial.print("1");
  }

  if (x == 1) {
    laserDisplay();                                          //displays the laser info screen
    if (digitalRead(buttonTwo) == LOW) {
      toggleLaser();
      Serial.print("2");
      delay(debounce);
    }
  }

  if (x == 2) {
    ledDisplay();                                              //displays the LED info screen
    if (digitalRead(buttonTwo) == LOW) {
      toggleLED();
      Serial.print("3");
      delay(debounce);
    }
  }

  if (x == 3) {
    fanDisplay();                                           //displays the fan info screen
    if (digitalRead(buttonTwo) == LOW) {
      toggleFan();
      Serial.print("4");
      delay(debounce);
    }
  }

  if (x == 4) {
     x = 0;           // equal to zero so there is a buffer where nothing will be affected
    Serial.print("5");
    delay(debounce);
  }


}




/*
   Main display screen code===================================================================================================
*/

void mainDisplay() {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");
  display.println("Laser");
  display.setCursor(0, 16);
  display.println("led");
  display.setCursor(0, 25);
  display.println("Fan");
    display.setCursor(64, 8);
    display.println(laserState);
    display.setCursor(64, 16);
    display.print(ledState);
    display.setCursor(64, 25);
    display.print(fanState);
    display.display();
    
  delay(tune);
display.clearDisplay();
}

/*
   Laser code==========================================================================================================
*/

void laserDisplay() {

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");
  display.println("Laser");
    display.setCursor(64, 0);
    display.println(laserState);
    display.display();
  delay(tune);
  display.clearDisplay();
}
//==================================
void toggleLaser()  {
  if (digitalRead(laser) == LOW) {
    digitalWrite (laser, HIGH);
    laserState[0] = 'O';
    laserState[1] = 'N';
    laserState[2] = 0;
    laserState[3] = 0;
    laserDisplay();
  }
  else if (digitalRead(laser) == HIGH) {
    digitalWrite(laser, LOW);
    laserState[0] = 'O';
    laserState[1] = 'F';
    laserState[2] = 'F';
    laserState[3] = 0;
    laserDisplay();
  }
}


/*
   LED code =========================================================================================================
*/

void ledDisplay() {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");
  display.println("led");
    display.setCursor(64, 0);
    display.print(ledState);
    display.display();
  
  delay(tune);
  display.clearDisplay();
}

void toggleLED() {
  if (digitalRead(led) == LOW) {
    digitalWrite (led, HIGH);
    ledState[0] = 'O';
    ledState[1] = 'N';
    ledState[2] = 0;
    ledState[3] = 0;
    ledDisplay();
  }
  else {
    digitalWrite(led, LOW);
    ledState[0] = 'O';
    ledState[1] = 'F';
    ledState[2] = 'F';
    ledState[3] = 0;
    
    ledDisplay();
  }
}


/*
   Fan code =========================================================================================================
*/

void fanDisplay() {

  
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Mode");
  display.println("Fan");
    display.setCursor(64, 0);
    display.print(fanState);
    display.display();
  
  delay(tune);
  display.clearDisplay();
}

void toggleFan() {
  if (digitalRead(fan) == LOW) {
    digitalWrite(fan, HIGH);
    fanState[0] = 'O';
    fanState[1] = 'N';
    fanState[2] = 0;
    fanState[3] = 0;
    fanDisplay();
  }

  else {
    digitalWrite(fan, LOW);
    fanState[0] = 'O';
    fanState[1] = 'F';
    fanState[2] = 'F';
    fanState[3] = 0;
    fanDisplay();
  }
}