How can I make a button switch screens on an OLED I2C display?

I am working on a project that requires the screen of an OLED display to switch to the next screen with the push of a button and I can't seem to figure out how to code it. Any help would be appreciated. I will paste my code below.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 32

#define OLED_RESET     -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define height   128
#define width    32

int button = 2;
int screen_current = 1;
int screen_total = 3; // Change this to however many screens you have
int btn_allow = 1; // Don't change this


void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
 

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); 
  }

    pinMode(button, INPUT_PULLUP);
  display.clearDisplay();

}

void loop() {
  // put your main code here, to run repeatedly:
  screen_current = 1;

while(digitalRead(btn_pin) == HIGH && btn_allow = 1) // Button depressed
{
    // Go to the next screen
    screen_current++;

    // Stop the button from working again until you let go
    btn_allow = 0;

    // Reset to screen one
    if(screen_current > screen_total)
    {
      screen_current = 1;
    } 
}

if(digitalRead(btn_pin) == LOW) // Button not depressed
{
    // Allow the button to work again
    btn_allow = 1;
}

if(screen_current == 1)
{ 
    // Whatever you want screen 1 to do
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(30,9);
    display.println("SCREEN 1");
    display.display();
    }

if(screen_current == 2)
{ 
    // Whatever you want screen 2 to do
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(30,9);
    display.println("SCREEN 2");
    display.display();
    }

if(screen_current == 3)
{ 
    // Whatever you want screen 3 to do
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(30,9);
    display.println("SCREEN 3");
    display.display();
    }

}

1 Like

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

Thanks for the quick reply. How would I go about implementing this into my code?

Download the StateChangeDetection example in the IDE.
Run it.
Study it.
Understand how it works.
Then you can incorporate it into your code.

If you fail, then post your attempt and we might be able to spot where you went wrong.

Thanks! I'll follow these steps and see if it works.

When I wired up the project as it instructed, the board shorts ground and 5v when the button is pressed. Is this how the code works?

The pin is kept LOW by the connection to GND via the resistor and pressing the button takes the pin HIGH, but the connection to GND is via the 10K resistor. That is not a short

The code works by detecting the pin connection to 5v as HIGH

Ah I see. I got the example working now. I will study this and try to implement this into my code soon. Thank you!

Hello imprevi

The sketch won´t compile.

Do you have experience coding in C++?
How many menues you will have?

A note: In programming, a counter starts at 0.

Hello Paul,

I am still a beginner trying to learn some code to make some projects as a hobby so I don't have much experience.

I also ran into an error with the code, but I was unsure what the error meant and how to solve it.

I plan to have a couple of screens of text to change between with a button. I am unsure how many screens I will use right now but I wanted to learn how to code a simple screen switch with a button.

I am planning to have a button move to the next page and another button to return to the last page.

I'm also confused about what you mean when you say a counter begins at 0. Could you please elaborate?

Thanks for the reply. Any tips or solutions would be appreciated!

You also need to add.

if(screen_current >= screen_total) screen_current = 1; // wrap round your screen counter

When you get round to incorporating the state change function into your code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.