There is a delay in starting the break period

Hello, I am a student who has been learning the program for over a month. I have a question because there is a part that I can’t solve while writing the code.

It flips by 1 second after reaching the halfway point before starting the 5-second rest period. What I want is for the break to start as soon as the circle reaches the halfway point.

sca;
close all;
clear;
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 0);
screens = Screen('Screens');
screenNumber = max(screens); 

white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);

[w, rect] = PsychImaging('OpenWindow', screenNumber, white);

[screenXpixels, screenYpixels] = Screen('WindowSize', w);
[xCenter, yCenter] = RectCenter(rect); 

Screen('BlendFunction',w,'GL_SRC_ALPHA','GL_ONE_MINUS_SRC_ALPHA');

ifi = Screen('GetFlipInterval', w);
topPriorityLevel = MaxPriority(w);
Priority(topPriorityLevel);

% 1. Draw a red ellipse and a blue ellipse that move a random distance from right to right each time the screen is switched.
% Set the screen switching method directly, keyboard input, mouse input, etc. 
% When the ellipse reaches the far right end or an arbitrary destination, display a result such as 'Red Win' in text.

% 2. Save the match results and the distance traveled for each ellipse in Excel or Notepad.
% When an ellipse passes the midpoint of the screen, make it move after a 5-second break. Show the timer for the remaining rest time.
% Display the distance the two ellipses have moved.
% ex) red sum: 300pixel, now: 5pixel

KbName('UnifyKeyNames');
escapeKey = KbName('ESCAPE');
leftKey = KbName('LeftArrow');
rightKey = KbName('RightArrow');

%----------------------------------------------------%
%    Set circle color and size, initial position
%----------------------------------------------------%
dim = 100;
baseRect = [0 0 dim dim];
red = [1 0 0];
blue = [0 0 1];
xpos = [dim/2 dim/2];
ypos = [screenYpixels*4/8, screenYpixels*6/8];

vector = zeros(4, 2);
colors = [red', blue'];

Screen('TextSize', w, 50);
text = ['Red '; 'Blue'];
text_pos = [100 150];

vbl = Screen('Flip', w);
waitframes = 1;
waitsecs = round(waitframes/ifi);

goal_line = 1800;
time = 5;
sum = [0, 0];

exit = false;

HideCursor;


while exit == false

    [keyIsDown, secs, keyCode] = KbCheck;
    pixelsPerPress = randi([10 20], 1, 2);

    %-----------------------------------------------%
    %    You win when you reach a certain point
    %-----------------------------------------------%

    if vector(1, 1) >= goal_line && vector(1, 2) < vector(1,1)
        Win_Text = 'Red Win!';
        pixelsPerPress = [0, 0];
        DrawFormattedText(w, Win_Text, 'center', 'center', red);
    
    elseif vector(1, 2) >= goal_line && vector(1, 1) < vector(1, 2)
        Win_Text = 'Blue Win!';
        pixelsPerPress = [0, 0];
        DrawFormattedText(w, Win_Text, 'center', 'center', blue);
    end
  
    if keyCode(escapeKey)
        exit = true;

    elseif keyCode(leftKey)
        xpos = xpos - pixelsPerPress;
        sum = sum + pixelsPerPress;

    elseif keyCode(rightKey)
        xpos = xpos + pixelsPerPress;
        sum = sum + pixelsPerPress;
    end

    %----------------------------------------------%
    %                   goal post
    %----------------------------------------------%
    Screen('DrawLine', w, black, goal_line, 0, goal_line, screenYpixels, 1);
    
    %----------------------------------------------------------%
    %     The circle's coordinate matrix and location text
    %----------------------------------------------------------%
    for i = 1:2
        if xpos(i) < 0
            xpos(i) = 0 + dim/2;
        end
        vector(:, i) = CenterRectOnPointd(baseRect, xpos(i), ypos(i));        

        text_pixel = [text(i, :) 'Sum = ', num2str(round(sum(i))) 'pixel  ', text(i, :) '=', num2str(round(xpos(i)-50)) 'pixel'];
        DrawFormattedText(w, text_pixel, 'center', text_pos(i), black);        
    end

    Screen('Filloval', w, colors, vector);  
    vbl  = Screen('Flip', w, vbl + (waitframes - 0.5) * ifi);
    %-----------------------------------------------------------------------%
    %     Rest for 5 seconds after passing half the width of the screen
    %-----------------------------------------------------------------------%
    if vector(1, 1) >= screenXpixels/2  || vector (1, 2) >= screenXpixels/2
        while time >= 0
            Screen('FillRect', w, white);
            if time > 0
                DrawFormattedText(w, num2str(time), 'center', 'center', black);
            end
            vbl = Screen('Flip', w, vbl + (waitsecs - 0.5)*ifi);
            time = time - 1;
        end
    end
end

%---------------------------------%
%       save to excel sheet
%---------------------------------%
red_pixel = round(vector(1, 1));
blue_pixel = round(vector(1, 2));
winner = {Win_Text};
T = table(red_pixel, blue_pixel, winner);
filename = 'windata.xlsx';
writetable(T, filename);

sca;

Solved!
But if there is a way to make my code simpler, please help!

 if vector(1, 1) >= screenXpixels/2  || vector (1, 2) >= screenXpixels/2
      while time >= 0
           Screen('FillRect', w, white);
           if time > 0
               DrawFormattedText(w, num2str(time), 'center', 'center', black);
           end
           vbl = Screen('Flip', w, vbl + (waitsecs - 0.5)*ifi);
           time = time - 1;
      end
end

I changed the above code as below.

if vector(1, 1) >= screenXpixels/2 || vector (1, 2) >= screenXpixels/2
      while time >= 0 
            if time > 0
                DrawFormattedText(w, num2str(time), 'center', 400, black);
            end
            if time < 5
                waitsecs = round(waitframes/ifi);
            end
            vbl  = Screen('Flip', w, vbl + (waitsecs - 0.5) * ifi);
            time = time - 1;
       end
 end

There are mistakes in how you calculate the target presentation times. Check against our demos or Peter’s tutorials. Other than that → For up to 30 minutes paid support, help PsychPaidSupportAndServices - our special 99 Euros + tax christmas offer is still active for probably another month.
-mario

Thank you. I also wrote the code by reading and referring to the demo. However, my teacher recommended that I code by referring to the Wait Frames Demo rather than the Accurate Timing Demo.

Hi,

Glad the demos are useful, please do let me know if they are being used in teaching and where. This will help me provide evidence that I should be given time to work on these (other than in my spare time).

Regarding your questions, they are quite vague and it would help if you could be more specific.

Or, use the PsychPaidSupportAndServices support.

P

hello. I am learning to code by solving various tasks in order to eventually design an experiment I want to do. I used the demo as a reference when solving assignments, or to familiarize myself with when I first started learning to code. The demo is a good textbook for students like me who are not familiar with Matlab and psychtoolbox.