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?
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
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
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 :)
Now that you have the button working all you need to do is:
Setup the GPIO pin at the top of the script, probably best just before the "Settings" section.
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.
JONAS_402
Sat, 06/11/2016 - 09:25
Permalink
sorted it
its, ok i managed to sort it, basiclly you move the buttons inside of the JOYBUTTONDOWN event type ie:
then it will only activate once per press, you can use this to stop and start recording too
piborg
Mon, 06/13/2016 - 10:53
Permalink
Button presses
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.
clankerr02
Mon, 02/05/2018 - 21:40
Permalink
Button presses
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
Tue, 02/06/2018 - 15:42
Permalink
Adding more buttons
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:
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 wantSecond look for the
elif event.type == pygame.JOYBUTTONDOWN:
line and add a test for the event button code inside that block like this:If everything has worked you should see the printed message each time you press the button you have set.
clankerr02
Tue, 02/06/2018 - 23:41
Permalink
Adding more buttons
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
Wed, 02/07/2018 - 10:47
Permalink
Getting the correct button value
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:
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 :)clankerr02
Wed, 02/07/2018 - 22:27
Permalink
using button press with gpio pins
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
Thu, 02/08/2018 - 10:45
Permalink
GPIO control
Now that you have the button working all you need to do is:
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.