Problem with finishing the routine in a specific moment

Win10
PsychoPy 2020.2.10

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’:

In the code component I added:

Begin Routine:

myClock = core.Clock()
currtime = myClock.getTime()
exittime = 999

Each frame:

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).

No error appears, it just doesn’t work properly!

Is there any error in my code?

Thank you in advance!

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).

Good luck.

1 Like

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.Psychopy code

Try using t (the automatic routine clock) instead of myClock and currtime

1 Like

Thank you for your suggestion!

I have also tried with that and it doesn’t work either.

Psychopy code 2
(the routine is called “car_approaching”)

You mean this, right?

Delete line 1

t is automatically created

Are you getting an error message?

You could add print commands to try to work out what is happening.

Okay, I’ve already deleted the line 1!

No error appears! I ended the experiment.

Right now, the car_approching Routine ends when the whole video is displayed.

I’m not sure how print commands work, can you give me a hint how to add them?

I also attach the excel file with the data.
_Chicken’s task_version_final_GOOD_2021_Feb_09_1414.csv (1.4 KB)

Try

print('keys',resp.keys)
print('exittime',exittime)
if 'space' in resp.keys:
     exittime = math.ceil(t)
1 Like

Thanks for your help!

I have been able to add print commands, and it seems that it continues pressing space until the end of the Routine:

image
exittime 3,4.,5,6,7…
image

until 8.

Looking good.

Try print(‘exittime’,t,exittime)

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.

1 Like

I added what you said, and here is the result:

Each frame code:

print('keys',resp.keys)
print('exittime',t,exittime)

if 'space' in resp.keys:
     exittime = math.ceil(t)
    
if t >= exittime:
    print('exiting now?')
    movie.stop()
    continueRoutine = False
    exittime = 999

The exittime seems to work:

image

but it still goes up to 8 (also, “exiting now?” was never printed, and the video didn’t stop):

image

Ahh, OK, this check:

if 'space' in resp.keys:

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
2 Likes

Thank you very much for your help and explanation, that worked!!

1 Like

Glad that helped, I only realised what was happening after seeing the printed diagnostic messages suggested by @wakecarter