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.
All you then need to do is swap the print statement for the code to change your LED.
What you need is the ability to detect when the button is pressed and when it is released.
The pygame.JOYBUTTONDOWN event happens when a button is pressed, so the same change from before can be used to turn the light on.
The pygame.JOYBUTTONUP event happens when a button is released. We can add another event type to detect this and use it to turn the light off.
This code should print the correct messages as the button is pressed and released. All you then need is the code to turn the light on and off :)
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 'Turn light on here'
elif event.type == pygame.JOYBUTTONUP:
# A button on the joystick just got released
hadEvent = True
if event.button == buttonNew:
print 'Turn light off here'
elif event.type == pygame.JOYAXISMOTION:
# A joystick has been moved
hadEvent = True
piborg
Tue, 03/12/2019 - 22:03
Permalink
Setting an LED on a button press
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.
All you then need to do is swap the print statement for the code to change your LED.
Thieu
Tue, 06/30/2020 - 18:57
Permalink
Hello piborg, is it possible
Hello piborg, is it possible to set the light on when button hold
And light off when button not hold?
Greetings Thieu
piborg
Wed, 07/01/2020 - 16:03
Permalink
Yes it is :)
What you need is the ability to detect when the button is pressed and when it is released.
The
pygame.JOYBUTTONDOWN
event happens when a button is pressed, so the same change from before can be used to turn the light on.The
pygame.JOYBUTTONUP
event happens when a button is released. We can add another event type to detect this and use it to turn the light off.This code should print the correct messages as the button is pressed and released. All you then need is the code to turn the light on and off :)
Thieu
Wed, 07/01/2020 - 18:38
Permalink
Many thanks piborg👍
Many thanks piborg👍