Diddyborg red joystick

Hi there,
I'm currently in the process of adding buttons to diddyborg joystick and for some reason I get 5 calls per button press, is there a way around this or to ignore the 1st 4?

its, ok i managed to sort it, basiclly you move the buttons inside of the JOYBUTTONDOWN event type ie:

            elif event.type == pygame.JOYBUTTONDOWN:
                # A button on the joystick just got pushed down
                if joystick.get_button(button_take_picture):
                    take_picture()

then it will only activate once per press, you can use this to stop and start recording too

piborg's picture

Glad to hear you figured it out :)

When used outside of the JOYBUTTONDOWN event it should always be true as long as the button remains held down.
This is useful for "hold for speed boost" type buttons which turn off when the button is released instead.

After a long break I'm back trying to add an external Led to my robot. It will hopefully activated by pressing the star button on the game pad. Could some one please help with the code. I'm currently using using diddyRedJoy2 script.
Thanks in advance

piborg's picture

It is fairly simple to add a new button :)

First add the button's number to the "settings" section towards the top of the script:

# Settings for the joystick
axisUpDown = 1                          # Joystick axis to read for up / down position
axisUpDownInverted = False              # Set this to True if up and down appear to be swapped
axisLeftRight = 2                       # Joystick axis to read for left / right position
axisLeftRightInverted = False           # Set this to True if left and right appear to be swapped
buttonResetEpo = 3                      # Joystick button number to perform an EPO reset (Start)
buttonSlow = 8                          # Joystick button number for driving slowly whilst held (L2)
slowFactor = 0.5                        # Speed to slow to when the drive slowly button is held, e.g. 0.5 would be half speed
buttonFastTurn = 9                      # Joystick button number for turning fast (R2)
buttonNew = 14							# New button, set to PS3 cross
interval = 0.00                         # Time between updates in seconds, smaller responds faster but uses more processor time

There is a quick reference sheet here for PS3 remotes, otherwise you can use jstest /dev/input/js0 and see which value changes when you press the button you want

Second look for the elif event.type == pygame.JOYBUTTONDOWN: line and add a test for the event button code inside that block like this:

            if event.type == pygame.QUIT:
                # User exit
                running = False
            elif event.type == pygame.JOYBUTTONDOWN:
                # A button on the joystick just got pushed down
                hadEvent = True
                if event.button == buttonNew:
                    print 'New button pressed :)'
            elif event.type == pygame.JOYAXISMOTION:
                # A joystick has been moved
                hadEvent = True

If everything has worked you should see the printed message each time you press the button you have set.

Thanks for your help. unfortunately it didn't work. I added the code as described but when I pushed the cross nothing happened. Also I realised my initial question was a bit vauge. I need to be able to push the cross and a LED connected to a GPIO pin lights up.
Thanks again

piborg's picture

If the message was not displayed on screen then it is likely the button value is wrong. The example value is only good for official PS3 controllers, other controllers (even PS4) will have different values.

If you add this line in the script it should print the button number each time a button is pressed:

            if event.type == pygame.QUIT:
                # User exit
                running = False
            elif event.type == pygame.JOYBUTTONDOWN:
                # A button on the joystick just got pushed down
                hadEvent = True
                print 'Button pressed:', event.button
                if event.button == buttonNew:
                    print 'New button pressed :)'
            elif event.type == pygame.JOYAXISMOTION:
                # A joystick has been moved
                hadEvent = True

After that run the script and see what number is shown. When you have the correct number change the value of buttonNew to match it. Once that is done you should see the message print out when cross is pressed :)

Thanks, I can now map the buttons on my gamepad. I still can't figure out how to use the above code to control a LED connected to the GPIO pins.

piborg's picture

Now that you have the button working all you need to do is:

  1. Setup the GPIO pin at the top of the script, probably best just before the "Settings" section.
  2. Add the code to change the GPIO pin where the print 'New button pressed :)' line is currently.

Controlling GPIO pins from Python is fairly straight forward. See our LEDBorg - Lesson 1 tutorial for an explanation on what to do and a simple example controlling three GPIO pins :)

If you are missing the wiringpi2 module see LEDBorg - Getting Started for instructions to install it.

Subscribe to Comments for "Diddyborg red joystick"