# Issue with Timing While Using Screen('Flip') in A Loop And Recording Keyboard

**URL:** <https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025>\
**Category:** Programming Help\
**Tags:** experiment-design\
**Created:** [February 5, 2020, 6:02pm UTC](https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025 "2020-02-05T18:02:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amirrezas](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@amirrezas](https://psychtoolbox.discourse.group/u/amirrezas)\
**Post date:** [February 5, 2020, 6:02pm UTC](https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025/1 "2020-02-05T18:02:46Z")

</div>

I have a code in Psychtoolbox calling Screen(‘flip’) at each iteration of some for loop. To explain briefly, the screen refresh rate is acquired at the beginning and the experiment time is divided by the refresh rate to give a timing array for each refresh rate.

Total\_experiment\_time=(1 minutes)=60 seconds;  
Refresh Rate=ifi=16.6ms;  
timeline=0:0.016:60;

%Block design stimulus  
stimulus=zeros(length(timeline),1);  
stimulus(625:1250)=1;  
stimulus(1875:2500)=1;  
vbl = screen(‘flip’, grey\_window);

Now, he starts a for loop that calls Screen(‘flip’) in each loop and starts recording keyboard right before finishing of the stimulus. Each loop is representing a vertical retrace and we want to get the reaction time when stimulus turns off (reaction to stimulus turning off).

> for i=1:(length(timeline)-1)  
> if (i==1249 || i==2549) %representing one iteration before finish of stimulus  
> KbQueueStart;  
> else if (i==1874 || i==(length(timeline)-2))  
> %representing one iteration before start of next stimulus or end of scan  
> (save keyboard data)  
> KbQueueRelease;  
> end
> 
> if stimulus(i)==1  
> window=(something bright)  
> else  
> window=(something grey)  
> end
> 
> vbl = screen(‘flip’, window, vbl + 0.5\*ifi);  
> SaveTimeAtEachLoop(i)=GetSecs();  
> end

I am totally aware that this is not the right way to write an experiment in Psychtoolbox but my reaction times are all messed up. They are long! and they are somehow showing a multimodal distribution! Any help to invert this would be appreciated.

 ![histogramRT](https://global.discourse-cdn.com/free1/uploads/psychtoolbox/original/1X/7fac287c9835037802f6f3454f32b0c7cc2260c4.jpeg)

Thanks in advance all

OS: windows 7, Hardware: DELL latitude E5570

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [February 6, 2020, 8:14am UTC](https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025/2 "2020-02-06T08:14:26Z")

</div>

As I think you appreciate, in general you shouldn’t use a basic loop of frame counts, as this is vulnerable to dropped frames and delays distorting the timing (especially relevant if you are stuck on Windows 7). You can use the `vbl` returned by `flip` itself as the timer, and use a set of defined real times to partition your experiment.

```auto
vbl = Screen('Flip',w); tStart = vbl;

while vbl <= tStart + 60

    % draw stimuli here, better at the start of the drawing loop
    Screen('DrawingFinished',w); % small ptb optimisation
    % your logic to record keyboard here, also based on comparing timestamps, not frame-number

    vbl = Screen('Flip',w,vbl+halfisi);

end

```

This will be more robust timing wise…

---

<div class="post-metadata">

**Author:** ![amirrezas](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@amirrezas](https://psychtoolbox.discourse.group/u/amirrezas)\
**Post date:** [February 6, 2020, 5:12pm UTC](https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025/3 "2020-02-06T17:12:44Z")

</div>

You are absolutely right. Unfortunately, this code is written by someone else with minimal experience with PTB. I would have written the code myself this way if I wanted to. But the problem is that the data is already collected and I need to find a way to see if I can invert them.

Note that the written code saves all the time stamps for each loop which could help correcting the reaction times.

The question that arises in my mind is that:  
At the time when Screen(‘flip’) is called with ‘when’ argument, it waits (pauses) for the next video retrace. What does this pause do to the Keyboard recording? What happens if keyboard response happens during this pause?

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [February 7, 2020, 8:02am UTC](https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025/4 "2020-02-07T08:02:56Z")

</div>

As I understand it, KbQueue is asynchronous, i.e. it collects in the background unaffected by other things MATLAB is doing, at least the demo shows WaitSecs doesn’t block it.

Your inter-peak interval is around 10ms, which is not your flip interval (assuming 60Hz), so I surmise your keyboard hardware polling is what is responsible for the 10ms interval? If this is the case you probably need to use a best fitting gaussian to find the real peak.

If timing is important for your study (for your future studies), computer keyboards should **NOT** be used as they have lousy temporal fidelity. Use something like the RTBox, which has dedicated PTB support…

---

<div class="post-metadata">

**Author:** ![amirrezas](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@amirrezas](https://psychtoolbox.discourse.group/u/amirrezas)\
**Post date:** [February 7, 2020, 5:54pm UTC](https://psychtoolbox.discourse.group/t/issue-with-timing-while-using-screen-flip-in-a-loop-and-recording-keyboard/3025/5 "2020-02-07T17:54:15Z")

</div>

Thank you very much Ian-Max,  
I have found the problem and it was about saving our data and has nothing to do with PTB so… problem resolved.

I am not sure about the forum rules but should I delete this thread?
