Search code examples
psychopy

Update stimulus attribute every ... ms or frame in PsychoPy


I'm trying to update the orientation of a gratingStim every 100 ms or so in the psychopy coder. Currently, I'm updating the attribute (or trying to) with these lines :

orientationArray = orientation.split(',') #reading csv line as a list
selectOri = 0 #my tool to select the searched value in the list
gabor.ori = int(orientationArray[selectOri]) #select value as function of the "selectOri", in this case always the first one
continueroutine = True
while continueroutine:
   if timer == 0.1: # This doesn't work but it shows you what is planned
        selectOri = selectOri + 1 #update value
        gabor.ori = int(orientationArray[selectOri]) #update value
        win.flip()

I can't find a proper way to update in a desired time frame.


Solution

  • A neat way to do something every x frames is to use the modulo operation in combination with a loop containin win.flip(). So if you want to do something every 6 frames (100 ms on a 60 Hz monitor), just do this in every frame:

    frame = 0  # the current frame number
    while continueroutine:
        if frame % 6 == 0:  # % is modulo. Here every sixth frame
            gabor.ori = int(orientationArray[selectOri + 1])
    
        # Run this every iteration to synchronize the while-loop with the monitor's frames.
        gabor.draw()
        win.flip()
        frame += 1