| Reference | Downloads | Github

Time of duration of stimuli depends on the response time of the participants

OS : Win 10
PsychoPy version: 3-3.1.5 (Builder)
(Sorry for my english)

Hi, in my experiment, a participant has to complete a mental fatigue task, its an alternation of numbers and letters. They have to press “2” when the number is even, “3” when he is odd, and “space” if 2 identical letters follow each other.

The letters and numbers should continue to scroll even if the participants does not respond.

The appearance of letters and numbers depends on the response time of the participant, but i don’t know how to code it.
So if the participant presses correctly and quickly, the letters and numbers will appear faster. And if the participant presses correctly but slowly, the numbers and letters will appear less quickly. So the duration of the stimulus depend of the participant respond time.

To be more precise:
The time of appearance of a stimulus is basically 1.5 seconds, so it remains on the screen for 1.5 seconds. During the task, the participant will respond more or less quickly to the appearance of the stimuli by pressing the response key on the keyboard.
I have therefore put in my external list an appearance time of 1.5 seconds for each stimulus. However, this appearance time should be adapted to the participant’s response time, and that’s what I don’t know how to do.
So for example, if the participant responds very quickly, the appearance time of the stimuli will automatically decrease.

I managed to randomly vary the time of appearance of the stimuli with this code:
import random
randDuration = random.randint

But I don’t know how to link the time of appearance of stimulis to the response time.


Dear Selma,

You need to be more specific. What is very quickly? What is the criterion for your adaptation? And by what amount do you want to increase or decrease the presentation time?

The following code increases or decreases the stimulus-presentation by 100 ms depending on whether the participant reacts faster or slower than the stimulus presentation time. Put this in the End Routine tab of a code-component that the top-most component in your trial

if stimDur < resp.rt:
        stimDur = stimDur + 0.100
elif stimDur >= resp.rt:
        stimDur = stimDur - 0.100

resp is the name of a keyboard-component which waits for a key-press indefinitely. stimDur is a variable that you use to set the duration of your stimulus. Define stimDur in the Begin Experiment tab of your code-component with 1.5 stimDur = 1.5, for instance. Ignore the Text-property. This was just for testing.

Best wishes Jens

Thank you for your help Jens, i don’t know why i have this erreur with your code :

Hello Selma

please find attached a toy-version. This ran without problems with PsychoPy 2020.2.10.
stimDur.psyexp (9.7 KB)

Perhaps you can extend on this.

Best wishes Jens

Hello, thank you again, but i don’t know why i can open everything except the code component.
Can you please send me a picture of the code?

Hello Selma,

click on the code-component code left of the timeline.


BTW, if you can’t open code-components upgrade to the most current PsychoPy-version.

Best wishes Jens

Hi, i tried your toy version and its working, but when i use the code in my script, i have the error again.

I use this code :

Hello Selma

do you mind uploading your experiment along with stimuli if needed so that one can test-run the experiment? Otherwise, simply use my experiment as basis and extend it to your needs.

Best wishes Jens