# Using one button to display different images on an oled display.

**URL:** <https://forum.arduino.cc/t/using-one-button-to-display-different-images-on-an-oled-display/671065>\
**Category:** Programming\
**Created:** [August 21, 2020, 4:19am UTC](https://forum.arduino.cc/t/using-one-button-to-display-different-images-on-an-oled-display/671065 "2020-08-21T04:19:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coeffeeman](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@coeffeeman](https://forum.arduino.cc/u/coeffeeman)\
**Post date:** [August 21, 2020, 4:19am UTC](https://forum.arduino.cc/t/using-one-button-to-display-different-images-on-an-oled-display/671065/1 "2020-08-21T04:19:30Z")

</div>

I am using an oled display with the

I am trying to create a menu on my oled (different images). I have created code that, when one button is pressed, the screen changes to the next image.  
"

```cpp
#include <Adafruit_SSD1306.h>
#include <splash.h>
#define bitmap_height  128
#define bitmap_width    64
#include <Wire.h>
#include <Adafruit_GFX.h>
#define YELLOW 0xFFE0
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
int buttonState1 = 0;
const int BUTTON_PIN1 = 2;
int toggle = 0;
const unsigned char s1 [] PROGMEM = {
//image code
};
const unsigned char s2 [] PROGMEM = {
//second image code
};

void setup() {
  
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
 
}
 
void loop() {
  buttonState1 = digitalRead(BUTTON_PIN1);
  if (buttonState1 == LOW) {
    if (toggle ==0) {
    display.clearDisplay();
    display.drawBitmap(0, 0, s1, bitmap_height, bitmap_width, WHITE);
    display.display();
    toggle = 1;
  }
  else {
    display.clearDisplay();
    display.drawBitmap(0, 0, s2, bitmap_height, bitmap_width, WHITE);
    display.display();
    toggle = 0;

  }
  }
}

```

"

(I did not include the image codes on here as they are really big, but they do work)

However, I would like to add a third image to this, so that, when you push the button, the first image is displayed, then, you press the button again, the second image is displayed, then you press the button again and the third image is displayed. How would I go about this, I have tried for hours with no success.

---

<div class="post-metadata">

**Author:** ![IoT\_hobbyist](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@IoT\_hobbyist](https://forum.arduino.cc/u/IoT_hobbyist)\
**Post date:** [August 21, 2020, 4:51am UTC](https://forum.arduino.cc/t/using-one-button-to-display-different-images-on-an-oled-display/671065/2 "2020-08-21T04:51:52Z")

</div>

See this code:

```cpp
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;
unsigned long lastCount = 0;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  button.setCountMode(COUNT_FALLING);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  unsigned long count = button.getCount();

  if (count != lastCount) {
    int imgID = count % 3;
    if ( imgID == 0) {
      // SHOW FIRST IMAGE
    } else if ( imgID == 1) {
      // SHOW SECOND IMAGE
    } else if ( imgID == 2) {
      // SHOW THIRD IMAGE
    }

    lastCount = count;
  }
}

```

The above code used this [button library](https://arduinogetstarted.com/tutorials/arduino-button-library)

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 21, 2020, 6:38am UTC](https://forum.arduino.cc/t/using-one-button-to-display-different-images-on-an-oled-display/671065/3 "2020-08-21T06:38:33Z")

</div>

Button pushes variable  
When button pushed increment variable ++  
If variable \>3 variable =1

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:42am UTC](https://forum.arduino.cc/t/using-one-button-to-display-different-images-on-an-oled-display/671065/4 "2021-05-06T00:42:49Z")

</div>

Closed on May 6, 2021, 12:42am UTC


