Variable length of looped routine

Hi - I have a task where there is one trial of a scene and object, then there is an ‘active baseline’ period. In the active baseline, I have left and right facing arrows appear on the screen for 1s each in a randomized order. I can get the arrows routine to run for a set period of time by defining nReps$ in the properties popup for the loop. However, I would like this active baseline to continue running for a variable period of time that is jittered, and I would like to define the length of the jitter from a conditions file (if possible). For example, the first trial consists of one scene + object, then the active baseline for 6 seconds, then another scene + object and the active baseline for 12 seconds.

I have tried nesting loops and defining the length of routine in the larger loop by referring nReps$ to an xlsx file with variable timings listed. That resulted in the loop repeating for every time I had listed in the xlsx file, rather than running for the timing of one row, then exiting the loop.

Thanks for your help.

Hi Bridget,

The nReps value doesn’t really have anything to do directly with time or duration: it is just the number of times that you want the conditions file associated with that loop to be repeated. To determine the duration of a routine, you need to specify durations of components within that routine. Those duration can themselves be variable names that come from your conditions file, which allows the length of routine to be jittered. i.e. instead of a fixed time of 1 s for your arrow stimulus duration, use a variable that comes from your conditions file. There is no need for a loop just for that specific purpose: use the conditions file attached to the main loop that controls your trials. Does that make sense?

Hi Michael,

Thanks for getting back to me so quickly.
The components in the routine are arrows (left and right). I want the routine to randomly select these arrows, and display them on the screen for one second each (I am specifying 1s for the duration of each arrow, and I have Image on 'set every repeat). I want the routine to continue selecting arrows for a set period of time. For example, one trial might show 2 arrows before looping back through to the scenes, the next time there might be 12 arrows (i.e., 12s) before looping back through to the next scene. If I set duration for of the component within the routine, I am making it so that one arrow stays on the screen for the period of time I select in the conditions file, rather than making the routine keep selecting arrows until a timer is out. In other words, I think I need a timer function working on an ‘if’ loop. For example, set time from conditions file, keep running routine until time is greater than conditions file, then exit. Any idea how to do that?

OK, that is more clear now. Yes, you can use a variable from your main conditions file to put in the nReps field of another loop that is nested within that main trials loop.

Note that a loop like this shouldn’t itself be connected to a conditions file: it will just get the repetition number from the conditions file controlling your main, outer, trial loop.

This inner loop surrounding the arrows routine only should also have the “is trials” setting de-selected. i.e. this isn’t a loop that is running trials and hence needs to change the structure of the data file. It is just a loop that acts to repeat some stimuli within a given trial. Make sure you check the datafile format to see that you are indeed only getting one row per trial.

Hope that helps, but if I still haven’t quite understood, perhaps posting a screenshot of your flow panel and relevant dialog box settings would be useful.

This works! Thanks so much Michael - very very helpful! I can now get the loop to run for a variable amount of time, while keeping each stimulus on the screen for 1s at a time within that.


I have a similar situation where I have a loop set up to repeat the same builder text boxes and objects but would like the loop to end after a certain amount of time. If I used this method, how would I format the excel sheet to define the time via nreps? Right now, I’m using a code aspect of timeStart = core.getTime() in the begin routine tab and the following code in the each frame tab. However, this will only end the loop if the time passed is greater than or equal to 20 seconds for that specific routine within the loop. Each routine ends when the participant presses enter, so if I continue to press enter and keep the routine looping, it will not end at 20 seconds.

timePassed = core.getTime() - timeStart

if timePassed >= 20.0:
continueRoutine = False
trials.finished = True

Only record the start time once, on the first iteration of the loop, rather than have it reset each time the routine starts:

if your_loop_name.thisN == 0:
    timeStart = core.getTime()