Wait for joystick response in psychopy

I want in a trial, the program to wait for joystick axis response after the presentation of stimuli. But the following code does not wait for the response and the loop ends capturing the axis position at that instance.

from psychopy.visual import Window,Circle, Rect
from psychopy.hardware import joystick
from psychopy.core import wait

win = Window(size=(400,400),units='pix',color=(0,0,0),fullscr=False)
stim={}
stim[0]=Circle(win,radius=100,edges=128,lineColor=(1,-1,-1),lineWidth=8)
stim[1]=Rect(win,width=100,height=100,lineColor=(1,-1,-1),lineWidth=8)
nJoys = joystick.getNumJoysticks()  # to check if we have any
id = 0
joy = joystick.Joystick(id)  # id must be <= nJoys - 1

nAxes = joy.getNumAxes()  
locationX = []
locationY = []

for i in range(5):
    if (i%2==0):
        stim[0].draw()
        win.flip()
        wait(1)
        locationX.append(joy.getX())
        locationY.append(joy.getY())
    else:
        stim[1].draw()
        win.flip()
        wait(1)
        locationX.append(joy.getX())
        locationY.append(joy.getY())    
win.close()

Hello,

You can poll the joystick displacement every frame, ending the trial when a non-zero value is returned. For example …

threshold = 0.5  # dead-zone is less than 50% displacement
joy_x = joy.getX()
joy_y = joy.getY()

# use this value to trigger the trial to end
end_trial = (-threshold > joy_x > threshold) or (-threshold > joy_y > threshold)

You should also add a “dead-zone” where the joystick needs to be displaced beyond some limit to register. This prevents jitter from the participant’s thumb from advancing the trial.