ikn
Posts: 42
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Mar 24, 2020 2:25 pm

It still doesn't explain why pin 7 and several of the other pins react the same way. I will do more digging and try to install pigpio to test them properly.

ikn
Posts: 42
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Wed Mar 25, 2020 10:59 am

I installed pigpio and did the gpiotest. The results seem to show that all GPIO pins are fine. Perhaps the multimeter testing method I did was the wrong way to go about testing them. I'm still not entirely sure which pins are best to use for input and output. I'm too scared to do any experimenting now in case I damage the GPIO pins. Say if I were for instance to swap pins around and use pins 7 & 10 for input buttons and use pins 37 & 38 for output to the leds. Would that work? (all pin numbers mentioned are BOARD pin numbers). Any pointers much appreciated.

gordon77
Posts: 4696
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Wed Mar 25, 2020 3:15 pm

That swap should be fine.

ikn
Posts: 42
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Thu Mar 26, 2020 7:38 pm

Nearly there. Here is the new code below (I swapped pins 7 & 10 with pins 12 & 13).

Code: Select all

import RPi.GPIO as GPIO
import glob
import subprocess
import os, signal
import time

# define GPIO pins used, first 10 for letters, second 10 for numbers
pins = (7,10,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40)
# buttons
stop = 8
shutdown  = 11
# LEDS
LED1 = 12
LED2 = 13

# define letters
letters = ("A","B","C","D","E","F","G","H","I","J")

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for count in range(0,len(pins)):
    GPIO.setup(pins[count],GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stop,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.output(LED1,GPIO.HIGH) # turn LED1 ON
GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON

# find tracks
tracks = glob.glob("/home/pi/Music/*.mp3")
tracks.sort()

while True:

    if  GPIO.input(shutdown) == 0:
        print ("Goodbye!")
        os.system("sudo shutdown -h now")
    # check for a letter press
    X = -1
    for A in range(0,int(len(pins)/2)):
        if GPIO.input(pins[A])== 0:
            X = A
            
    # check for a number press
    Y = -1
    for B in range(int(len(pins)/2),len(pins)):
        if GPIO.input(pins[B])== 0:
            Y = (B - int(len(pins)/2))
            
    # if letter AND number pressed play a song
    if X > -1 and Y > -1:
        # calculate track number from letter and number pressed
        Z = 10 * X + Y
        if Z < len(tracks):
            print ("Playing Track: ", letters[X] + str(Y+1), tracks[Z] )
            rpistr = "mplayer " + '"' + tracks[Z] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                time.sleep(1)
                if  GPIO.input(stop) == 0:
                    print ("Track stopped")
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(shutdown) == 0:
                    GPIO.output(LED1,GPIO.LOW) # turn LED1 OFF
                    GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                    
                poll = p.poll()
I'm ready to do some soldering now but I'm struggling to figure out which way round to wire the leds and resistors. There are loads of tutorials but with different scripts and wiring diagrams. Is the wiring diagram below the correct way. Also, I know the GPIO pins have internal resistors, but I was thinking of maybe adding external resistors to the input buttons for extra peace of mind. What strength resistors would I use and which way to wire them. Like I said there are loads of diagrams and tutorials, but it's a bit of a dilema which ones to use. Any ideas much appreciated.
Attachments
untitled-sketch_bb_aSAL3Ak7PO.jpg
untitled-sketch_bb_aSAL3Ak7PO.jpg (166.23 KiB) Viewed 160 times

gordon77
Posts: 4696
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Thu Mar 26, 2020 8:21 pm

Looks OK. You can put the led resistor in either lead, it simply limits current.

To be safer fit a resistor in the lead to the switch, maybe 1k, this will protect the pi if you ever make an input an output and press the switch.

ikn
Posts: 42
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Thu Mar 26, 2020 11:42 pm

Would the resistor for the button go between the black ground wire and button or between the blue GPIO wire and button or doesn't it matter which.

User avatar
DougieLawson
Posts: 37763
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Thu Mar 26, 2020 11:46 pm

ikn wrote:
Thu Mar 26, 2020 11:42 pm
Would the resistor for the button go between the black ground wire and button or between the blue GPIO wire and button or doesn't it matter which.
it also doesn't matter which (just like the side of the LED that gets the resistor).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

ikn
Posts: 42
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Fri Mar 27, 2020 3:02 pm

Thanks. I am thinking of using these leds (white 3528 smd led 0.06w, 2.8-3.4v,20ma) I got from here:
https://www.ebay.co.uk/itm/10-100-pcs-x ... 130r4kLgBQ
With 360 ohm resistors for the leds and 1K resistors for the buttons (all ¼ watt metal film resistors) from here: https://www.ebay.co.uk/itm/25Watt-25W-M ... xYU9vqtAIQ
Does that sound like a good idea. Also should the resistors be soldered closer to the Raspberry Pi or should they be closer to the leds and buttons. Wires from the button board to the Pi could be up to 30cm long. Led wires would be a bit shorter.

gordon77
Posts: 4696
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Mon Mar 30, 2020 11:32 am

ikn wrote:
Tue Mar 24, 2020 2:25 pm
It still doesn't explain why pin 7 and several of the other pins react the same way. I will do more digging and try to install pigpio to test them properly.
This may help explain it

[email protected]:~ $ raspi-gpio get
BANK0 (GPIO 0 to 27):
GPIO 0: level=1 fsel=0 func=INPUT pull=UP
GPIO 1: level=1 fsel=0 func=INPUT pull=UP
GPIO 2: level=1 fsel=0 func=INPUT pull=UP
GPIO 3: level=1 fsel=0 func=INPUT pull=UP
GPIO 4: level=1 fsel=0 func=INPUT pull=UP pin 7
GPIO 5: level=1 fsel=0 func=INPUT pull=UP
GPIO 6: level=1 fsel=0 func=INPUT pull=UP
GPIO 7: level=1 fsel=0 func=INPUT pull=UP
GPIO 8: level=1 fsel=0 func=INPUT pull=UP
GPIO 9: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 10: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 11: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 12: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 13: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 14: level=1 fsel=0 func=INPUT pull=NONE
GPIO 15: level=1 fsel=0 func=INPUT pull=UP pin 10
GPIO 16: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 17: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 18: level=0 fsel=1 func=OUTPUT pull=NONE
GPIO 19: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 20: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 21: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 22: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 23: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 24: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 25: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 26: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 27: level=0 fsel=0 func=INPUT pull=DOWN

ikn
Posts: 42
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Mon Mar 30, 2020 4:31 pm

Ah right, yes they do seem to correspond with my multimeter readings. Although my Pi does not quite show the same reading as your test.

This is my Pi below in it's natural state with fresh noobs install and mplayer freshly installed. (with no scripts created or running yet and nothing attached to GPIO pins).

[email protected]:~ $ raspi-gpio get
BANK0 (GPIO 0 to 27):
GPIO 0: level=1 fsel=0 func=INPUT
GPIO 1: level=1 fsel=0 func=INPUT
GPIO 2: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 4: level=1 fsel=0 func=INPUT
GPIO 5: level=1 fsel=0 func=INPUT
GPIO 6: level=1 fsel=0 func=INPUT
GPIO 7: level=1 fsel=0 func=INPUT
GPIO 8: level=1 fsel=0 func=INPUT
GPIO 9: level=0 fsel=0 func=INPUT
GPIO 10: level=0 fsel=0 func=INPUT
GPIO 11: level=0 fsel=0 func=INPUT
GPIO 12: level=0 fsel=0 func=INPUT
GPIO 13: level=0 fsel=0 func=INPUT
GPIO 14: level=0 fsel=0 func=INPUT
GPIO 15: level=1 fsel=0 func=INPUT
GPIO 16: level=0 fsel=0 func=INPUT
GPIO 17: level=0 fsel=0 func=INPUT
GPIO 18: level=0 fsel=0 func=INPUT
GPIO 19: level=0 fsel=0 func=INPUT
GPIO 20: level=0 fsel=0 func=INPUT
GPIO 21: level=0 fsel=0 func=INPUT
GPIO 22: level=0 fsel=0 func=INPUT
GPIO 23: level=0 fsel=0 func=INPUT
GPIO 24: level=0 fsel=0 func=INPUT
GPIO 25: level=0 fsel=0 func=INPUT
GPIO 26: level=0 fsel=0 func=INPUT
GPIO 27: level=0 fsel=0 func=INPUT

Note GPIO 14 & 18 difer from yours. Does this make a difference. I have ordered all the bits I need and will be using the code below with 1K resistors for the buttons. So do you think it is safe to go ahead with this code and GPIO button and LED setup? Any help much appreciated. Thanks.

Code: Select all

import RPi.GPIO as GPIO
import glob
import subprocess
import os, signal
import time

# define GPIO pins used, first 10 for letters, second 10 for numbers
pins = (7,10,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40)
# buttons
stop = 8
shutdown  = 11
# LEDS
LED1 = 12
LED2 = 13

# define letters
letters = ("A","B","C","D","E","F","G","H","I","J")

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for count in range(0,len(pins)):
    GPIO.setup(pins[count],GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stop,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.output(LED1,GPIO.HIGH) # turn LED1 ON
GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON

# find tracks
tracks = glob.glob("/home/pi/Music/*.mp3")
tracks.sort()

while True:

    if  GPIO.input(shutdown) == 0:
        print ("Goodbye!")
        os.system("sudo shutdown -h now")
    # check for a letter press
    X = -1
    for A in range(0,int(len(pins)/2)):
        if GPIO.input(pins[A])== 0:
            X = A
            
    # check for a number press
    Y = -1
    for B in range(int(len(pins)/2),len(pins)):
        if GPIO.input(pins[B])== 0:
            Y = (B - int(len(pins)/2))
            
    # if letter AND number pressed play a song
    if X > -1 and Y > -1:
        # calculate track number from letter and number pressed
        Z = 10 * X + Y
        if Z < len(tracks):
            print ("Playing Track: ", letters[X] + str(Y+1), tracks[Z] )
            rpistr = "mplayer " + '"' + tracks[Z] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                time.sleep(1)
                if  GPIO.input(stop) == 0:
                    print ("Track stopped")
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(shutdown) == 0:
                    GPIO.output(LED1,GPIO.LOW) # turn LED1 OFF
                    GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                    
                poll = p.poll()

Return to “Python”