Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

{SOLVED} How to make a momentary push button that exit games and take me back to game menu ?


  • Global Moderator

    @O-M-A-A said in How to make a momentary push button that exit games and take me back to game menu ?:

    Can use it in GPIO 3 instead of the power button ? and remove the power button wires as I don't need it.

    You do need it, because that switch that you want to buy will not shut down properly your PI (i.e. will not run the shutdown command), it will merely cut down the power.



  • @mitu I see, so what to do now



  • @O-M-A-A Hope I tranlated correct

    "bite together and pull through the cheeks"

    You are close to finish this!



  • @mitu @cyperghost yeah, thank you.

    it looks awesome, everything is working except the 1 button which is not a big deal, I will search for my problem. thanks guys a lot for your help and patience. I'm rly grateful for the time u spent it with a newbie ;D.



  • @cyperghost @mitu
    I was thinking, I have tried the button with every pins, one of these pins executed the letter H on the keyboard, and I was able to make the button as hotkey.
    is it possible to make this key also perform the start button, so when pressed will function as a start and hotkey which will exit the emulator ;D.


  • Global Moderator

    I just tried on by PCB and the reset button is wired on PIN3, with the other one wired to a GND pin.

    Here's a little script you could run (python <script.py>) to check a PIN for a momentary press

    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    import subprocess
    
    pinNo = 3
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pinNo, GPIO.IN)
    
    while True:
        #grab the current button state
        buttonState2 = GPIO.input(pinNo)
    
        if not buttonState2:
            # Reset emulator
            print "pressed " + str(pinNo)
    

    You can test various pins for button press by replacing changing pinNo to your desired Pin number.

    The original script I posted can probably be simplified as

    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    import subprocess
    
    GPIO.setmode(GPIO.BOARD)
    
    shutdownPin=5
    resetPin=3
    
    GPIO.setup(shutdownPin, GPIO.IN)
    GPIO.setup(resetPin, GPIO.IN)
    
    oldButtonState1 = True
    
    while True:
        #grab the current button state
        buttonState1 = GPIO.input(shutdownPin)
        buttonState2 = GPIO.input(resetPin)
    
        # check to see if button has been pushed
        if buttonState1 != oldButtonState1 and buttonState1 == False:
            # shutdown
            subprocess.call("shutdown -h now", shell=True,
              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            oldButtonState1 = buttonState1
        
        if not buttonState2:
            # Reset emulator
            print "Reset pressed"
            subprocess.call("/home/pi/scripts/multi_switch.sh --es-closeemu", shell=True,
              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
        time.sleep(.3)       
    


  • @mitu dudeeee,
    it works ;DDD, thank you so much for ur efforts, I couldn't believe it xD
    ur amazing, I really appreciate it.
    Also, thanks to you @cyperghost u guys are awesome



  • @mitu

    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    import subprocess
    
    GPIO.setmode(GPIO.BOARD)
    
    shutdownPin=5
    resetPin=3
    
    GPIO.setup(shutdownPin, GPIO.IN)
    GPIO.setup(resetPin, GPIO.IN)
    
    oldButtonState1 = True
    
    while True:
        #grab the current button state
        buttonState1 = GPIO.input(shutdownPin)
        buttonState2 = GPIO.input(resetPin)
    
        # check to see if button has been pushed
        if buttonState1 != oldButtonState1 and buttonState1 == False:
            # shutdown
            subprocess.call("shutdown -h now", shell=True,
              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            oldButtonState1 = buttonState1
        
        if not buttonState2:
            # Reset emulator
            print "Reset pressed"
            subprocess.call("/home/pi/scripts/multi_switch.sh --es-closeemu", shell=True,
              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    

    this script made it work, like magic

    Thanks a lot


  • Global Moderator

    Glad you got it working.
    One minor correction - add back the sleep timer at the end, otherwise the script will consume more resources than needed. I've edited my previous post with the timer line. Make sure you copy the script again and preserve the indentation.



  • @mitu sure bro, again thank you very much



Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.