简体   繁体   English

按钮按下打开 OLED ESP32

[英]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.我正在开发一个 DHT22 传感器,当按下按钮时,它会在 OLED 上显示读数。 I am using Arduino IDE.我正在使用 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. GPIO 13 当前正在从传感器获取信号,GPIO 26 与电源和接地一起连接到物理按钮。 All of the code with "//added" is the new code I added in order to get the button to work.所有带有“// added”的代码都是我为了让按钮工作而添加的新代码。 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退出状态 1
uninitialized const 'PushButton' [-fpermissive]未初始化的 const 'PushButton' [-fpermissive]

In setup function I added:setup function 中,我添加了:

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

And in loop function I added:loop function 我添加:

    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.首先,您只需要ButtonPin变量即可使用按钮。 As your button is connected to GPIO 26, you must declare this pin as an INPUT.由于您的按钮连接到 GPIO 26,因此您必须将此引脚声明为 INPUT。 Later, you shoould use the digitalWrite function to get the state of the pin.稍后,您应该使用digitalWrite function 来获取引脚的 state。 Depending on how you connect your button, you will receive a '0' or a '1' when it is pressed.根据您连接按钮的方式,按下按钮时您将收到“0”或“1”。 Anyways, I have been using a really comfortable library to work with buttons on Arduino, check it here .无论如何,我一直在使用一个非常舒适的库来处理 Arduino 上的按钮,请在此处查看 I hope this could help我希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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