Scrolling menu using two buttons on esp32 and OLED display

Hi all!
the menu is not displaying onto on the OLED and im not sure if there are any problems with the code.
there were no errors while compiling or uploading

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

#define i2c_Address 0x3C

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SH1107 display = Adafruit_SH1107(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define button_start 26
#define button_save 25
#define button_sel 33

int menuState = 0;
int menuItem = 1;

void setup() {
  Serial.begin(9600);
  display.begin(i2c_Address, true);

  delay(2000);
  display.clearDisplay();

  // Set buttons as inputs
  pinMode(button_start, INPUT);
  pinMode(button_save, INPUT);
  pinMode(button_sel, INPUT);

  // Display menu
  showMenu();
}

void loop() {
  int startState = digitalRead(button_start);
  int saveState = digitalRead(button_save);
  int selState = digitalRead(button_sel);

  // Loop to check which button is pressed
  if (startState == HIGH) {
    menuState = 1;
    menuItem = 1;
  }
  if (selState == HIGH) {
    menuItem = 2;
  }
  if (menuItem == 2 && startState == HIGH) {
    menuState = 2;
  }

  // Update the display with the current menu state
  showMenu();
}

void showMenu() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("Menu");
  // Display the options
  display.setCursor(10, 10);
  display.println("Start test");
  display.setCursor(10, 20);
  display.println("Menu");
  switch (menuState) {
    case 1:
      display.setTextColor(SH110X_BLACK, SH110X_WHITE); // 'inverted' text
      display.setCursor(10, 10);
      display.println("Start test");
      display.setTextColor(SH110X_WHITE);
      display.setCursor(10, 20);
      display.println("Menu");
      //*add start test function*
      break;
    case 2:
      display.setTextColor(SH110X_WHITE); // 'inverted' text
      display.setCursor(10, 10);
      display.println("Start test");
      display.setTextColor(SH110X_BLACK, SH110X_WHITE);
      display.setCursor(10, 20);
      display.println("Menu");
      display.clearDisplay();
      break;
  }
}

Welcome to the forum

Do the examples from the library work ?

how did you wire your buttons ? do you have pull down resistors ?

also your code is a machine gun :slight_smile:

you keep clearing the display , drawing something and immediately thereafter you loop and clear again the display etc

➜ that won't work well, at best you have a blinking screen....

you need to update the display only if your menuState has changed

Is there anything that appears on the OLED Display Unit? I have tried your sketch, noting appears on my OLED.

Try the following test program to check the wiring and functionality of your OLED.

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  testdrawstyles();    // Draw 'stylized' characters
}

void loop() 
{
}

void testdrawstyles(void) 
{
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
  display.println(F("Hello, world!"));

  display.setTextColor(SSD1306_WHITE);
  //(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.println(3.141592, 6); //6-digit after decimal point

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(2000);
}