Simulate button press?



  • Is there a way to simulate a button press in code? I'm looking for a M5.BtnA.press(). I've tried pulling GPIO pins low for a quick delay, then back high again, like this:

        digitalWrite(38, LOW);
        delay(100);
        digitalWrite(38, HIGH);
    

    Not only does that not work, I'm afraid I'd lose debouncing. Any ideas?



  • hello, gpio 38 in esp32 only input



  • Yup, @heybin, you're indeed right. Short of soldering a wire from an available GPIO to the switch itself, I don't see anything in the buttons library that could even be modified to allows for this functionality. It's either so simple that I just can't see the answer, or impossible (without a soldering iron involved)...



  • I'm wondering what is your purpose for simulating a button press? maybe there could be an alternative solution for your problem



  • Well, I have a couple of spots in my project that a button press would make things very easy. For example, I'm using M5ez for my interface, and use Blynk to display events on the M5 screen. I have backlight timeout set to 30 seconds. A new event doesn't turn the backlight back on, but a keypress does. So, in my if (newEvent) loop, I could have a M5.BtnA.press(), that would wake up the screen. Can I go into the M5ez library, figure out what happens after a real button press to wake the screen and replicate that? Yes, of course, and I have certainly tried that, but some of the items that need to be set are private, so calling them from a main loop is problematic. Rewriting that stuff so it's all public might work, but that solves one problem. I'd also like menus to time out - if there's no activity after going into a menu, I'd like it to time out and go back to the main menu. Again, code can certainly make that happen, but a M5.BtnA.press() would be a lot easier to implement... Does this make sense?