Button debounce: Short & Long press without blocking


#1

Hey there, currently I am running a python script that uses a button attached to the GrovePi using a screw terminal. Until now it had only one function in each “mode”, however I would like to add short and long press functions without (further) delaying the program. Any thoughts on this?

Currently this is my function to read the button:


def pushcallback():
	readingpush = grovepi.digitalRead(pinpushbutton)
	time.sleep(0.2)
	if (readingpush == grovepi.digitalRead(pinpushbutton)):
		return readingpush

#2

Hi @jeroenveenvan,

Here are some suggestions for you:

  • Use threads (one for each button) - it’s going to let your main program run just as before.

  • Read more frequent the button’s state in the same period of time: i.e.: instead of reading the button’s state, then wait 200ms and then check again, try reading the button’s state for say … 5 times during those 200ms.

  • Use some statistical analysis for eliminating noise during the reads - maybe the user doesn’t push as with much force the button in order to trigger it, so I think this approach is going eliminate these errors.

What do you think?

Thank you!