Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

How to sense multiple inputs simultaneously?

Tue Aug 13, 2019 8:29 am

Hello!
I am making a drum kit and I want to sense multiple inputs simultaneously. (Like a real Drum Kit!)
I have different channels set up in Pygame for each input and so there isn't any flaw with that. I want to find a way to sense multiple inputs at the same time.
Here is a snippet of the code----

Code: Select all

try:
while True:
while True:
if GPIO.input(29) ==1:
channel1.play(pygame.mixer.Sound(Drum1))
time.sleep(0.02)
break

if GPIO.input(31) ==1:
channel2.play(pygame.mixer.Sound(Drum2))
time.sleep(0.02)
break

#There are six more of these exact codes, just the GPIO number, the channel number, and the "Drum" number is changed.
(Edit: I don't know why the code doesn't appear indented after try and while true...
But don't worry, it works fine...)

I have defined things like GPIO, channel1, Drum1 earlier, so don't worry about that.

This is the exact code I use, and it works fine. Please note that the time.sleep command is to avoid multple sounds from the same input.

Please help!!!
Thank you!

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to sense multiple inputs simultaneously?

Tue Aug 13, 2019 10:19 am

Have a look at one of the gpiozero "recipes" for a GPIO music box.

https://gpiozero.readthedocs.io/en/stab ... -music-box

Your code "polls" the gpio pins waiting for them to be pressed and the delay will stop multiple buttons being recognised at the same time. The code I've linked to registers a callback for each button so that when they are pressed they will play the appropriate sound. See if it works for you.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

pcmanbob
Posts: 7254
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to sense multiple inputs simultaneously?

Tue Aug 13, 2019 10:22 am

The way you are checking for inputs you can only ever check them sequentially one after another, to capture multiple inputs you are going to have to use the event_detected() function with a callback for each event.

its explained here https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

see event_detected() function & Threaded callbacks as a starting point.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”