# How can I switch between screens in oled

**URL:** <https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977>\
**Category:** Programming\
**Created:** [January 6, 2023, 11:18am UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977 "2023-01-06T11:18:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![riti20](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@riti20](https://forum.arduino.cc/u/riti20)\
**Post date:** [January 6, 2023, 11:18am UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/1 "2023-01-06T11:18:11Z")

</div>

I Have a ESP32 with oled display and DHT11 sensor and I want to show temperature and keep showing that and when I press a button it will show humidity and keep showing that until I press it again.

But what is happening is that it is oscillating between them.

```cpp
#include "DHT.h"
#include<Wire.h>
#include<Adafruit_GFX.h>
#include<Adafruit_SSD1306.h>

#define DHTTYPE DHT11 
#define SCREEN_WIDTH 128 //screen width 
#define SCREEN_HEIGHT 64 //screen hight
#define DHTPIN 2 //DHT11 input pin

DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

int trig = 0;

void setup() {
  Serial.begin(115200);
  // Start I2C Communication SDA = 5 and SCL = 4 on Wemos Lolin32 ESP32 with built-in SSD1306 OLED
  Wire.begin(5,4);

  pinMode( 2,INPUT_PULLUP );
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false, false)) {
    for(;;);
  }
dht.begin();
}

void loop() {
  oled();
}

void oled(){
  if(digitalRead(16)==LOW){
    trig++;
  }
  if(trig==2){
    trig=0;
  }

  switch(trig){
    case 0:{
      digitalWrite(15,HIGH);
      display.clearDisplay();
      display.drawPixel(127, 63, WHITE); // This command will help you to print a pixel on display , its the last pixel, coordinate 127,63
      display.drawPixel(0, 0, WHITE); // pixel with 0,0 coordinate
      display.setTextColor(WHITE);
      display.setCursor(10,0);
      display.setTextSize(3);
      float t = dht.readTemperature();
      display.println("Temp ="); // printing a variable
      display.setCursor(30,30);
      display.setTextSize(3);
      display.println(t);
      display.display();
    }break;
    case 1:{
      digitalWrite(15,HIGH);
      display.clearDisplay();
      display.drawPixel(127, 63, WHITE); // This command will help you to print a pixel on display , its the last pixel, coordinate 127,63
      display.drawPixel(0, 0, WHITE); // pixel with 0,0 coordinate
      display.setTextColor(WHITE);
      display.setCursor(10,0);
      display.setTextSize(3);
      float h = dht.readHumidity();
      display.println("HUMID ="); // printing a variable
      display.setCursor(30,30);
      display.setTextSize(3);
      display.println(h);
      display.display();
    }break;
  }
  Serial.println(trig);
  delay(1000);

}

```

play help.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 6, 2023, 1:14pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/2 "2023-01-06T13:14:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 6, 2023, 1:19pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/3 "2023-01-06T13:19:26Z")

</div>

> [@riti20](#):
>
> But what is happening is that it is oscillating between them.

Sound ike Pin 16 is always reading as LOW. Could there be a wiring error? Doe it stop oscillating if you disconnect the button from Pin 16?

---

<div class="post-metadata">

**Author:** ![riti20](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@riti20](https://forum.arduino.cc/u/riti20)\
**Post date:** [January 6, 2023, 1:27pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/4 "2023-01-06T13:27:49Z")

</div>

no, I am not getting any error and oscillation is not stopping even after I remove the button.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 6, 2023, 1:29pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/5 "2023-01-06T13:29:33Z")

</div>

Your input pin is probably floating. Use INPUT\_PULLUP on pin 16 also.

---

<div class="post-metadata">

**Author:** ![riti20](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@riti20](https://forum.arduino.cc/u/riti20)\
**Post date:** [January 6, 2023, 2:09pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/6 "2023-01-06T14:09:06Z")

</div>

no it's not solving the issue

---

<div class="post-metadata">

**Author:** ![riti20](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@riti20](https://forum.arduino.cc/u/riti20)\
**Post date:** [January 6, 2023, 2:10pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/7 "2023-01-06T14:10:03Z")

</div>

thank you so much it solved the problem.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 6, 2023, 2:13pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/8 "2023-01-06T14:13:27Z")

</div>

Why is some of the code in your case code blocks in curly brackets ? Remove the curly brackets for the case code or put the break commands in the braces, not a mixture of both

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 6, 2023, 7:15pm UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/9 "2023-01-06T19:15:15Z")

</div>

> [@riti20](#):
>
> no it's not solving the issue

Are you using an external pull-down resistor?

---

<div class="post-metadata">

**Author:** ![riti20](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@riti20](https://forum.arduino.cc/u/riti20)\
**Post date:** [January 7, 2023, 3:49am UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/10 "2023-01-07T03:49:27Z")

</div>

it's fine i am no longer using the switch case i changer it

```cpp
#include "DHT.h"
#include<Wire.h>
#include<Adafruit_GFX.h>
#include<Adafruit_SSD1306.h>

#define DHTTYPE DHT11 
#define SCREEN_WIDTH 128 //screen width 
#define SCREEN_HEIGHT 64 //screen hight
#define DHTPIN 2 //DHT11 input pin

DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

int trig = 0;

const int buttonPin = 16; // the pin that the puchbutton is attached to 

int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int cs = 0;
int level_persentage = 0;

void setup() {
  Serial.begin(115200);
  // Start I2C Communication SDA = 5 and SCL = 4 on Wemos Lolin32 ESP32 with built-in SSD1306 OLED
  Wire.begin(5,4);

  // initialize the button pin as a input
  pinMode(buttonPin, INPUT);
  
  pinMode( 16,INPUT_PULLUP );
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false, false)) {
    for(;;);
  }
dht.begin();
}

void loop() {

  int water_level = analogRead(14);
  level_persentage = map(water_level, 0, 4095, 0, 100);

  //code for button state change detetion
  
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
// Serial.println("on");
// Serial.print("number of button pushes: ");
// Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
// Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    TempHumid_oled();
  } else {
    water_oled();
  }
}

void TempHumid_oled(){
      digitalWrite(15,HIGH);
      display.clearDisplay();
      display.drawPixel(127, 63, WHITE); // This command will help you to print a pixel on display , its the last pixel, coordinate 127,63
      display.drawPixel(0, 0, WHITE); // pixel with 0,0 coordinate
      display.setTextColor(WHITE);
      display.setCursor(10,0);
      display.setTextSize(2);
      float t = dht.readTemperature();
      display.println("Temp ="); // printing a variable
      display.setCursor(60,13);
      display.setTextSize(2);
      display.println(t);
      display.setCursor(10,33);
      display.setTextSize(2);
      float h = dht.readHumidity();
      display.println("Humid ="); // printing a variable
      display.setCursor(60,48);
      display.setTextSize(2);
      display.println(h);
      display.display();
}

void water_oled(){
      digitalWrite(15,HIGH);
      display.clearDisplay();
      display.drawPixel(127, 63, WHITE); // This command will help you to print a pixel on display , its the last pixel, coordinate 127,63
      display.drawPixel(0, 0, WHITE); // pixel with 0,0 coordinate
      display.setTextColor(WHITE);
      display.setCursor(10,0);
      display.setTextSize(3);
      float h = dht.readHumidity();
      display.println("Water ="); // printing a variable
      display.setCursor(30,30);
      display.setTextSize(3);
      display.println(level_persentage);
      display.display();
}

```

this code is working perfectly for me.

---

<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:** [July 6, 2023, 3:49am UTC](https://forum.arduino.cc/t/how-can-i-switch-between-screens-in-oled/1073977/11 "2023-07-06T03:49:49Z")

</div>

Closed on July 6, 2023, 3:49am UTC

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