Conditional Branching/ One stimuli button press to cue another stimuli

Hello everyone! I am new to psychopy and I would like some help building an experiment using the Builder.

I want to present an image A for 6 seconds where the participant can press a space button anytime during that time. If the participant presses the button, another image B will appear on the screen for 6 - t (with t being the amount of time it took the participant to press the button). After those collective 6 seconds, a new image C will pop up and the same decision-making process will occur. However, if the participant chooses not to press the space bar, image A will be displayed on the screen for the 6 seconds with image C appearing afterwards.

Currently, I have the following:


I have made the test image with the space button key press. I think I’ve also made the appropriate excel file with just the image names and what not but I haven’t added any additional columns to that sheet. I have the nReps$ for Press and NoPress loops set to nRepsPress and nRepsNoPress.

I’m not sure this is correct as I’ve based it off of a tutorial for a correct button press and an incorrect button press for one stimulus with each rerouting to a different stimulus. I think mine is a little different because I only have one button option.

Any help will be greatly appreciated!! Thank you!

Hi I’m not quite sure of the logic presented in the screenshots above (you should explain what the variables mean). But no conditional branching should be necessary here, and the whole thing could be done in a single routine.

i.e. define your three image stimuli. A and B are shown from 0 to 6 s, and image C from 6 s onwards. Set image B to have an opacity of 0 (i.e. to be invisible initially).

Insert a code component (above the stimuli). In the Each frame tab, put something like this:

if event.getKeys('space'): # if space key pushed,
    name_of_image_B_stimulus.opacity = 1.0 # make B appear

You have defined B to end at 6 seconds, so the offset is taken care of for you.

Extensions to the code can be made to record responses, deal with other keypresses to end the trial, or whatever.

Hi! Thank you for the response. I think I’m talking about something else! To clarify, I want to display an image A for 6 seconds. If the participant presses a button, they will be shown image B for the remaining seconds. So if the subject presses the button at 4 seconds, image B will be shown for 2 seconds. If the subject presses the button at 5 seconds, image B will be shown for 1 second. If the participant does not press the button at all, they will view the image A for 6 seconds. Then, image C will appear.

Essentially, I would like image B to be cued by a button press in image A for 6 seconds - t, with t being the amount of time the participant viewed image A.

Hope that clarifies some things!

Clarification is always good! But I think this code should do exactly what you want. It runs once every frame (i.e. at the screen refresh rate, typically 60 Hz). So if the subject pushed the button at 4 seconds, it would be detected quickly and the display updated, ideally within 16.7 ms.

So the sequence would be this:

  1. The routine begins. Image A is visible. Image B exists but is set to be invisible. Image C does not yet exist.
  2. A button is pushed at 4 s. The opacity of image B is changed to 1.0 so it will become visible on the next screen refresh (within 16.7 ms).
  3. At 6 seconds, both image A and B disappear (as you specified in the image component dialogs that their offset is at t=6.0). Simultaneously, image C appears, as you specified its onset to be at t=6.0.

i.e. Image B would be shown for (almost exactly) 2 s. If the key is pressed at 5 s, it would be shown for 1 s. If not pushed at all, it is never seen. Make sense?

Thanks for clarifying my clarification! I’ve applied your instructions and it works! Woohoo! Thank you so much. I just have one more question:

I just added Image A and B and tried to loop it because I want image C and D to do the same thing. However, I’ve run into a bit of a problem!

Image A is column “pics” on my excel sheet while Image B is column “reapp”. I tried running the experiment and B appears when I press space during A. However, images from “reapp” appear on the screen instead of images from “pics”. So instead of image C popping up from the column “images” like Image A, Image C is from “reapp”. And there is no longer an option to press space during this image C. I think it might have to do with how I’ve set the images. I’ve tried both “constant” and “set every repeat” but I don’t know if those are correct. What do you think?

Not sure I really understand the description of the new problem, but here goes…

  • Assuming each of your image stimulus components has its own variable name in the .xlsx file, then each needs needs to have that variable name specified in its Image field, preceded by a $ symbol. i.e. there should be four image components in your routine and four corresponding columns in the .xlsx file.
  • The Image fields should be specified to set every repeat. This means that a new value is applied to each on every trial, rather than using a fixed value for the whole experiment. That is, one row of four image names will be used on each trial.
  • Image components B and D should both be set to have an initial opacity of 0.

The code given above was only designed to make image B appear. To do the same thing for Image D, then it needs a little modification:

if event.getKeys('space'): # if space key pushed,

    if t <= 6.0: # in the first six seconds of the trial...    
        name_of_image_B_stimulus.opacity = 1.0 # make B appear
    else: # must be in the second part of the trial, so
        name_of_image_D_stimulus.opacity = 1.0 # make D appear

Do you need to store the times of the keypresses in your data file?