Question about how to make a fixation cross appear for various durations

I have been working with the builder view but am trying to go back to the coder view to change the code. I am trying to insert a fixation cross before each condition in my experiment for one of several amounts of time that are chosen randomly. For example, I would like the fixation cross to appear for either 0.5s, 1s, or 2s, and I would like the time that it is displayed to vary randomly. In making the cross as a text component in the builder view, it seems as though you can only make the cross occur for one constant duration of time (using the start and stop) duration. It seems like the way to do this would be going back to manipulate the code created from the builder view, but I am not exactly sure what to do and where to insert it.

Hi!
I currently find two ways of doing this

with conditions file:

create a conditions file that looks like

time
0.5
1
2

In the builder view, you create two routines, one for the fixation cross, one for your actual stimulus (or more if needed) and build a loop around them. Choose the conditions file in the loop and use $time as duration value for you fixation cross.

This will loop through all conditions the number you specify in your loop’s nReps. If you don’t want to do that try

in Coder:

For your fixation cross choose a variable duration like my_random_time and compile the coder script.
In coder, search for your variable (should look like frameRemains = 0.0 + my_random_time- win.monitorFramePeriod * 0.75) and insert the following above the while continueRoutine: statement:

    my_random_time = [0.5, 1, 2][randint(0, 3)]
    while continueRoutine:

I’m not sure about logging with this approach, or if this can be done more easily.

Best,
Robin

Don’t do that. There is a far better alternative:

For a simple task like this, you could actually just embed a random function as @schubisu suggests , directly in the onset field of your stimulus, with a $ prefix to indicate that it is code. But you will probably also want to save that value in the data, which requires another line of code. So insert a code component, put it above the text stimulus so that it updates the variable before the text stimulus needs to refer to it, and put in something like this in the Begin Routine tab:

fixation_start_time = np.random.choice([0.5, 1.0, 2.0])
thisExp.addData('fixation_start_time', fixation_start_time)

and then put $fixation_start_time in the stimulus onset field, set to update every routine.

1 Like

Thank you both for responding to this! I am quite new to using psychopy so I have some more questions-- I tried to create a time conditions file like you suggested, but I cannot insert it as a routine because it seems like the only way to upload a file to be a condition is through inserting a loop? So when I tried to insert it as a loop (in addition to the stimulus loop I already had) I had trouble trying to get it in the right order because it would have the cross at the several times in the time file simultaneously with the other stimulus rather than before it. I then thought that I should create one loop with both the conditions file, but from what I can tell, I cannot do this. I would like the flow to have the instructions first and then the loop with the fixation cross first then the stimulus, and then back to the fixation cross etc. Thanks again for your help, I really appreciate it.

I’m really struggling to understand this post, but I think you simply need to put all of your variables in one conditions file, connected to one loop, which will control both the fixation and the stimulus.

Hi Michael,

if I put all variables in one conditions file, is the ISI ($time) then chosen independently from the image file presented, if they are in the same row of the conditions file? - Just to be sure. Thank you.

No, data in rows in conditions files is inherently linked: that is, a row is used to group together values for a particular trial. When the file is randomised, all data in each row is kept together: it is just the order of the rows that is randomised.

If you want variables to be randomised independently of each other, then you probably need to deal with one of them in code rather than in your conditions file, as suggested in the posts above.

I have this same problem like above and it does not work for me :frowning: I need to set a random time but my code don`t work. I need help. What do u need to help me? Screen shots? Files? I do exacly like in instructions. Please help me!

Please start a new topic and provide all relevant details to your specific problem (i.e. what you want to achieve, what you have done, and what goes wrong). “My code don`t work.” is not sufficient to get a useful response.

Thanks, i just did it a minute ago: