psychopy.org | Reference | Downloads | Github

End routine after specified time


#1

Hello everybody,

I am working on a mental rotation task with psychopy builder and everything looks good so far, except one thing: How can you end a looping routine after a specified time?

Background: The task is meant to cause interference for 6 minutes and must not be finished earlier, therefore I added many stimuli and want to end the loop/routine after 360 seconds.

I already tried to insert custom code in the End Routine area (with ‘trials’ being the actual name of the loop):

if t=360
	trials.finished=True

When trying to run the experiment I always get ‘invalid syntax’, is it the t variable or what am I missing? Thank you in advance!

Greetings
Pave


#2

Two things in the first line:

if t >= 360:

if statements end with a colon, and you can’t test for exact values of floating point numbers, as they can’t be exactly represented by computers.

Also, add this:

continueRoutine = False

As ending the loop only takes effect after the routine has ended, so you need to end that early too.


#3

Thank you for your quick reply!

I implemented your suggestions and now I don’t get a syntax error but the routine does not end after 6 minutes…

The code now looks like this:

if t>=360:
    trials.finished=True
    continueRoutine=False

Could it be affected by the nReps value for the loop? Because I set it to 5 to make sure, that no participant will finish the routine before the timer has ended. There are no error prompts or something like that.

Greetings,
Pave


#4

Ahh, OK. The value of t restarts at zero on each each routine, so you need to calculate a time that crosses the boundary of routines. Builder maintains a clock called globalClock that you can use, which keeps counting throughout the experiment. You need to get the value of that clock at the start of the first trial and then check against that later:

Begin routine tab:

if trials.thisN == 0: # only on the first trial
    startTime = globalClock.getTime()

Each frame tab:

if globalClock.getTime() - startTime >= 360:
    trials.finished = True
    continueRoutine = False

#5

That works, thanks a lot Michael!

One more minor question: When the time is up the routine ends only if a key is then pressed. Is it possible to force the routine to end without waiting for a keystroke / the next picture?


#6

The continueRoutine = False line should achieve that. I wonder if the keyboard component is interfering with that, by changing the value of continueRoutine back to True. Try shifting the code component relative to the keyboard component (e.g. if placed below the keyboard component, it’s code will execute later, and overturn whatever the result of the keyboard component was.) You can right-click on component icons to alter their order.


#7

Oh wait, I confused the different files and the code got in the wrong section (End routine instead of each frame), it works fine now.

Sorry for the confusion and thank you very much for your help!