function sample02(duration)
% Usage: sample02(duration_in_sec)
%
%       25/3/2015 Hiroshi Ashida

if nargin<1
    duration=4;
end
try
    %% opening screen
    wWidth=1024; wHeight=640;
    AssertOpenGL;
    screens=Screen('Screens');
    sNumber=max(screens);
    [sWidth,sHeight]=Screen('WindowSize',sNumber);
    w=Screen('OpenWindow',sNumber,0,[0 0 wWidth wHeight]); % window presentation
%    w=Screen('OpenWindow',sNumber,0);
    frameInterval=Screen('GetFlipInterval',w);
    %frameRate=Screen('NominalFrameRate',w);
    frameRate=round(1/frameInterval);

    %% drawing
    x=0; 
    dx=sWidth/(duration/frameInterval);
    y=sHeight/2-50;
    n=0;
    t=0.0;
    Priority(MaxPriority(w)); 
    [VBLTimestamp1 StimulusOnsetTime1 FlipTimestamp1 Missed1 Beampos1]=Screen('Flip',w);
    while t<duration
        x=round(x+dx); 
        Screen('FillRect',w,[255 0 0],[x y x+100 y+100]);
        [VBLTimestamp2 StimulusOnsetTime2 FlipTimestamp2 Missed2 Beampos2]=Screen('Flip',w);
        n=n+1;
        t=FlipTimestamp2-FlipTimestamp1;
    end
    Priority(0);
    Screen('CloseAll');
catch exception
    Priority(0);
    Screen('CloseAll');
    throw(exception);
end

%% results
fprintf(1,'1 frame = %10.3f s\n', frameInterval);
fprintf(1,'Duration = %10.3f s\n', duration);
fprintf(1,'        VBLTimestamp StimulusOnsetTime FlipTimestamp\n');
fprintf(1,'start   %12.3f %17.3f %13.3f\n', VBLTimestamp1, StimulusOnsetTime1, FlipTimestamp1);
fprintf(1,'end     %12.3f %17.3f %13.3f\n', VBLTimestamp2, StimulusOnsetTime2, FlipTimestamp2);
fprintf(1,'duraion %12.3f %17.3f %13.3f\n', VBLTimestamp2-VBLTimestamp1, StimulusOnsetTime2-StimulusOnsetTime1, FlipTimestamp2-FlipTimestamp1);
fprintf(1,'frames  %5d\n', n);
