Best approach for button functionality

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Best approach for button functionality

Postby Deouss » Tue Jul 03, 2018 3:56 am

It is a very simple question and maybe very novice however I would like to know the simplest and nicest method of implementing a simple on/off button functionality on ESP32.
I am guessing with internal pulldown(?) but what about filtering just like the pulse counter has?
Thanks for advices

Archibald
Posts: 110
Joined: Mon Mar 05, 2018 12:44 am

Re: Best approach for button functionality

Postby Archibald » Tue Jul 03, 2018 9:51 am

There's an example here . . . .
https://www.arduino.cc/en/Tutorial/Debounce
. . . . . but use internal resistor.

If you want to switch on/off the power to your ESP32 consider purchasing a push button switch with 'push on push off' action (sometimes called 'maintained or 'latching' action).

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Best approach for button functionality

Postby Deouss » Tue Jul 03, 2018 11:32 am

That example is strictly for Arduino and it doesn't filter the spikes.
I found interesting info here
https://forum.arduino.cc/index.php?topic=62065.0
I wonder if anyone used a schottky diode to eliminate any unpleasant effects which are basically related to boosting voltage.
3.3V switch shouldn't pose serious risks but I am not really sure. The button I plan to use is one on rotary encoder that bounces contact off after releasing. I will have to check on the scope what exactly is going on during button scenarios

Archibald
Posts: 110
Joined: Mon Mar 05, 2018 12:44 am

Re: Best approach for button functionality

Postby Archibald » Tue Jul 03, 2018 11:41 am

Yes, it's Arduino code but you shouldn't have any trouble reading it to understand what it does and why it does it. It does "filter" contact bounces. As it stands, it determines when there have been no contact bounces for at least 50ms.

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Best approach for button functionality

Postby Deouss » Tue Jul 03, 2018 1:30 pm

It is good code however it does not filter bounces but just delays the response until bouncing ends.
It is not the best way to handle button press. I need to get rid of physical voltage spikes and noise instead of skipping them.
Also I'd rather use interrupt than wait loop. Just was wondering if esp had built in filtering particularly for buttons.

username
Posts: 477
Joined: Thu May 03, 2018 1:18 pm

Re: Best approach for button functionality

Postby username » Tue Jul 03, 2018 9:33 pm

There is nothing wrong with using delays, there really is no need to add hardware to filter the bounce, when you can do it all in software just the same. That snipped of Arduino code is far more than it needs to be.

Also, one needs to be careful when using interrupts on buttons with no filtering. You can get many IRQ events very rapidly.


Just keep it simple.

Code: Select all

loop()
{
    if(button is low)
    {
    	// typical debounce time
    	delay_ms(20);
    	
    	// Lets check again if still low
    	if(button is low)
    	{
    		Lets do something about it. 
    		
		// Stay here if the thing we need to do is would be done before the user can let go of the button. 
		// So we dont re-trigger thinking its another press.
	    	while(button is low); 
    	}
    }

}


Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Best approach for button functionality

Postby Deouss » Tue Jul 03, 2018 10:16 pm

What if I do following (from gpio interrupt) :

Pseudocode on interrupt triggered

-disable interrupt
-wait until gpio low
-delay some time
-set button flags,variables,state
-enable interrupt

username
Posts: 477
Joined: Thu May 03, 2018 1:18 pm

Re: Best approach for button functionality

Postby username » Tue Jul 03, 2018 10:43 pm

There are many ways to skin a cat, but the way I skin mine when using a interrupt for this situation is this.

Pin changes state and jumps to ISR. The first thing is disable interrupt. Set a variable to true, then clear IRQ bit to be ready for next IRQ, and exit ISR.

Depending on how fast I need to check this button I will either create a new task for it or just just check in my main loop.
If the variable is true, I will do what I need to, then clear the variable, read the pin and wait until it goes back to the idle state and then re-enable the interrupt.

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Best approach for button functionality

Postby Deouss » Tue Jul 03, 2018 10:48 pm

How do I clear IRQ bit? Isn't disabling the interrupt the same?

username
Posts: 477
Joined: Thu May 03, 2018 1:18 pm

Re: Best approach for button functionality

Postby username » Wed Jul 04, 2018 12:10 am

I have not dug into the docs on the ESP32 to answer that. Typically there is a register and bit in that register enable / disable and configure things like interrupt on going high, going low, or both. Gotta look into the docs to find out.

Who is online

Users browsing this forum: No registered users and 111 guests