What am I trying to achieve?:
I need to finish the routine when an interval point is reached (this interval is split into 1 second, 2s, 3s, 4s, and 5s).
I inserted a code component in a Routine where the video is displayed and with a resp component called ‘resp’:
currtime = myClock.getTime()
if currtime >= exittime:
continueRoutine = False
exittime = 999
if resp.keys == 'space':
exittime = math.ceil(currtime) #to approximate the time
My idea is that Psychopy takes the time when the key is pressed (could be 0.67s, 0.88s, etc.), and finishes the trial when the time is 1 second (or if resp.rt = 3.55 → finish at 4s).
Right now, the Routine ends when the response is given (it doesn’t wait until the interval).
Unselect “Force end of routine” in your keyboard component.
Otherwise, what you are doing seems good - using math.ceil() is an elegant approach. It’s possible that you might need to do this instead:
if 'space' in resp.keys:
because in some situations, .keys will be a list rather than a single value. But try your approach first.
I’d also shift the second if to be the first check rather than the last, so that the exit time gets updated before you check if it has been exceeded. Currently, even if the time was exactly 1.00, the routine won’t end until the next screen refresh.
Lastly, in the future, surround your code snippets in this forum before and after with ```
This ensures that the code is properly formatted and crucially allows us to see the indentation properly (I’ve edited your post above to do this).
Hi Michael, thank you very much for your response and helpful comments!
The “Force end of routine” in the resp component is unselected.
I shifted the second if, so right now I have:
Each frame:
currtime = myClock.getTime()
if resp.keys == 'space':
exittime = math.ceil(currtime)
if currtime >= exittime:
continueRoutine = False
exittime = 999
The only key response allowed is ‘space’
Now it doesn’t work, it doesn’t finish the Routine after the response (the Routine ends because the whole video is shown 6s).
I have tried both "if ‘space’ in resp.keys: " and "if resp.keys == ‘space’: " and none of them work.
You could also add print(‘exiting now?’) just above continueRoutine=False.
Also, I wonder whether you need to add video_1.stop() there as well (above continueRoutine=False) where video_1 is the name of your video component. It might be that videos stop the continueRoutine=False command being executed.
will keep evaluating to True on every screen refresh after the key is first pushed. Hence exittime will keep getting bumped up and so t will keep chasing it but never catch up.
So add some code to only increment exittime once per trial. Firstly put this in “begin routine”:
time_incremented = False
and then in “each frame”:
if 'space' in resp.keys and not time_incremented:
exittime = math.ceil(t)
time_incremented = True