psychopy.org | Reference | Downloads | Github

Unable to accurately record mouse click?

In my experiment, I show a traffic light (red circle, then amber circle, then green circle). Participants need to make a mouse click when they see the green circle.

I’m using a code snippet in my experiment to record which circle was shown (red/amber/green) when the participant made their mouse click.

My issue is that it always records that the mouse click was made during the amber circle presentation, even when that’s not the case!

How do I fix this? Thank you in advance! :slight_smile:

This is the code that I’m using (every frame):

while checkWhen == 1:
    if go == 1 & amberL.status==FINISHED & greenL.status==STARTED: #green shown.
        wentGreen = True #clicked when green circle shown.
        print('green')
        checkWhen = 0
        continue
    elif go == 1 & amberL.status==STARTED & greenL.status==NOT_STARTED: #green not shown yet but amber shown.
        wentAmber = True #clicked when amber shown.
        print('amber')
        checkWhen = 0
        continue
        
    elif go == 1 & redL.status==STARTED & amberL.status==NOT_STARTED: #if amber not started and red shown.
        wentRed = True #clicked when red shown.
        print('red')
        checkWhen = 0

The go variable just stores if the click has been made in a different code snippet and is updated every frame using
`if mouse.getPressed()[0]==1:
go = 1``

  1. Never use while loops like this in custom Builder code: you might be breaking Builder’s inherent drawing loop. i.e. Code in the “Each frame” tab is called on every screen refresh (typically at 60 Hz) so you don’t want to embed possibly infinite loops within that code, as it might cause Builder to not keep up with the screen refresh cycle. I’m not sure of the intended logic here, but you could probably just replace the while checkWhen == 1: with if checkWhen == 1: That way the code will still run frequently (once per screen refresh) but won’t break Builder’s timing cycle.
  2. and and & are not synonyms in Python. For Boolean logic like this, you want to replace each & with and to get the behaviour you are after, unless you want to pay lots of attention to use of brackets. (This is a subtle issue that still trips me up when old habits from other programming languages creep into my Python.)
1 Like

This fixed the issue perfectly! Thank you so much!