简体   繁体   中英

Button Push turning on an OLED ESP32

I am working on getting a DHT22 sensor which displays a reading onto an OLED when a button is pushed. I am using Arduino IDE. Right now I have the sensor working and displaying on the screen, but I am having difficulty getting it to only turn on when the button is pushed. GPIO 13 is currently getting the signal from the sensor, and GPIO 26 is connected to the physical button along with power and ground. All of the code with "//added" is the new code I added in order to get the button to work. Any help is greatly appreciated!

At the top I added:

const int ButtonPin = 26; //added
const int PushButton;      //added

I am getting the error:

exit status 1
uninitialized const 'PushButton' [-fpermissive]

In setup function I added:

pinMode(ButtonPin, OUTPUT); //added
pinMode(PushButton, INPUT)://added

And in loop function I added:

    int Push_button_state = digitalRead(PushButton); //added
    if (Push_button_state == HIGH) //added
        float h = dht.readHumidity();
    // Read temperature as Celsius
    float t = dht.readTemperature();

    u8g2.firstPage();
    do {
        draw();
    } while ( u8g2.nextPage() );
    delay(1000);
}
else {  //added

I think you are not clear about what you want to achieve. First of all, you only need ButtonPin variable to work with the button. As your button is connected to GPIO 26, you must declare this pin as an INPUT. Later, you shoould use the digitalWrite function to get the state of the pin. Depending on how you connect your button, you will receive a '0' or a '1' when it is pressed. Anyways, I have been using a really comfortable library to work with buttons on Arduino, check it here . I hope this could help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM