OLED screen menu-esp32

Hi all, thanks for taking the time to read my post.
I am trying to make a simple menu using an oled screen, 2 push buttons and an esp32 board. For now, all this program is meant to do is just to display whether option 1 or option 2 has been selected. There are 2 buttons, each corresponding to an option. So if I press button 1 then option 1 is selected and if I press button 2 option 2 is selected. When I run the code below, the screen displays the menu options along with 'option 1 is selected' before any buttons have even been pressed. Also, when I press button 2 it says that option 2 is selected but then does not revert back to option 1 when button 1 is pressed. I do not think I connected them to the wrong pins as both GPIO 2 and 4 have input pullup resistors. I have tried to resolve this by having a previous state variable.
The hardware configuration: one pin of button to GPIO and other pin of button connected to 10K resistor which is connected to ground.
Does anyone have any suggestions on how I can resolve this issue? Thank you

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

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64

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

#define BUTTON1 2
#define BUTTON2 4

//define the state of the menu
int menuState = 0;

// track if the buttons were pressed in the previous iteration
bool prevButton1State = HIGH;
bool prevButton2State = HIGH;

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

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

  //set buttons as inputs with pull-up resistors
  pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);

  //display the menu
  showMenu();
}

void loop() {
  //read the state of the buttons
  int button1State = digitalRead(BUTTON1);
  int button2State = digitalRead(BUTTON2);

  // Check if Button 1 is pressed
  if (button1State == LOW && prevButton1State == HIGH) {
    //button 1 is pressed update menuState and display the menu
    menuState = 1;
    showMenu();
  }

  // Check if Button 2 is pressed
  if (button2State == LOW && prevButton2State == HIGH) {
    //button 2 is pressed update menuState and display the menu
    menuState = 2;
    showMenu();
  }

  // Update the previous button states
  prevButton1State = button1State;
  prevButton2State = button2State;
}

//function to display the menu on the OLED screen
void showMenu() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Menu");
  //display the options
  display.setCursor(10, 10);
  display.println("Option 1");
  display.setCursor(10, 20);
  display.println("Option 2");
  //display the selection
  display.setCursor(0, 40);
  switch (menuState) {
    case 0:
      display.println("No option selected");
      break;
    case 1:
      display.println("Option 1 is selected");
      break;
    case 2:
      display.println("Option 2 is selected");
      break;
  }
  //update the display
  display.display();
}

have a look at esp32-pinout-reference-gpios
depending on board pin 2 may be connected to a LED
probably move button to another pin?

1 Like

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