How can I switch between screens in oled

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.

#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.

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

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?

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

Your input pin is probably floating. Use INPUT_PULLUP on pin 16 also.

no it's not solving the issue

thank you so much it solved the problem.

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

Are you using an external pull-down resistor?

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

#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.

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