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.