| Reference | Downloads | Github

2 Response times in one routine


I have 2 images (clickable stimuli) in a routine. I want to do a mouse click on the first image and get the time stamp of this click and then do a mouse click on the second image, end the routine and get the timestamp of the second click (which in this case coincides with the end of the routine). I have tried different things like making my own variables (in the begin experiment, e.g. clicks=0, timer1=core.Clock() ) and increment them like this (Each frame):

if mouse.getPressed()[0]==1 and mouse.isPressedIn(background):

if mouse.getPressed()[0]==1 and mouse.isPressedIn(star):

Unfortunately, it doesn’t work. Anyone has an idea of how to approach this?

Hi @phoenix,

could you provide some more context? Did you put this code snippet in a code component in the builder or did you write the entire experiment in code?

What do you mean by “it doesn’t work”. Does your output file just show the rt column and not the rt2 column?

One of the first things I noticed was that you haven’t indented your if blocks. Did you just not copy the code properly or could that be an issue?

Every mouse object has a build-in timer (from the API for mouse objects):
“If getTime=True (False by default) then getPressed will return all buttons that have been pressed since the last call to mouse.clickReset as well as their time stamps:”

buttons, times = mouse.getPressed(getTime=True)

Hi @LukasPsy,

Thanks for your reply. Yes, I’ve put it in a code component. What I mean by it doesn’t work is that the output file contains only rt and not rt2. The reason it’s not indented is that I copied the code from an editor I use to change my code in order not to mess up with builder’s code component. This is not causing the issue, though. I used the code you pasted but even in this case I couldn’t get the behavior that I wanted. I was also thinking that this might resolve the problem as it sounds like what I want but it didn’t do the job either.

Help us to help you: we don’t know it isn’t causing the issue unless we can see the indentation.

The issue is actually worse than that: currently you aren’t measuring even the first reaction time correctly, but rather, the last time at which the mouse was pressed, instead of when it was first pressed. i.e. on each screen refresh, you will be checking if the mouse is pressed in your stimulus, and if so, overwriting the previously measured reaction time with the current time, until the button is no longer held down. So you need to add a few more flags to keep track of where you are at:

Begin routine tab:

background_already_clicked = False
star_already_clicked = False
clicks = 0

Each frame tab:

if not background_already_clicked and mouse.isPressedIn(background):
    thisExp.addData('rt', timer1.getTime())
    background_already_clicked = True
    clicks = clicks + 1
elif not star_already_clicked and mouse.isPressedIn(star):
    thisExp.addData('rt2', timer1.getTime())
    star_already_clicked = True
    clicks = clicks + 1

if clicks == 2:
    continueRoutine = False

Now if that code doesn’t work, the trick is to insert temporary debugging code within various parts of your code, like:

print('Background clicked')

Sprinkle print statements like that around until you can figure out why the second check isn’t triggering.

Also be thinking carefully about when code should run. It is correct to create the timer at the start of the experiment, but it should be reset at the start of each routine. It isn’t correct to create the clicks variable at the start of the experiment, as it needs to be set to zero at the start of each routine anyway, so setting it to zero at the start of the experiment is redundant.

Lastly, if I had to guess, my suspicion as to why your second check is not triggering is that there is a conflict between your code and the Builder mouse component. Because you are handling this functionality in code, don’t set the mouse component to force the end of the trial, or to record reaction times or to specify the valid clickable stimuli.

Thanks, @Michael. Your suggestions fixed the issue.

PS.Also indented my code above, so to appear properly.