I'm writing a code that reads the value of a joystick axis and then sends this value to an arduino. It works, however I do not want it constantly print 0 to the LXTerminal when nothing is being pressed.
The code prints: SDL_JoystickGetAxis value:0:
repeatably when the axis is not being pressed.
It then prints:
SDL_JoystickGetAxis value:-32768:
SDL_JoystickGetAxis value:0:
SDL_JoystickGetAxis value:-32768:
The left drive is
-1.0
Repeatably when the axis is being pulled all the way down, or a number between -1 and 1 depending where the joystick is.
I only want it to print The left drive is <leftDrive>
when the left axis is being pressed, and then nothing when it is not. How do I achieve this?
Here is my code:
import pygame
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
leftDrive = 0
rightDrive = 0
while True:
try:
pygame.event.pump()
joyCheckLeft = joystick.get_axis(1)
joyCheckRight = joystick.get_axis(2)
if joyCheckLeft != 0:
pygame.event.pump()
leftDrive = joystick.get_axis(axisLeftDrive)
print 'The left drive is'
print leftDrive
writeNumber(int(2+(leftDrive*-10)))
pygame.event.pump()
time.sleep(0.1)
except KeyboardInterrupt:
quit()
Note: The code doesn't work without pygame.event.pump()
. This is needed so that "get_axis()" actually returns the value of the axis.