%%%% Script for SART%%%%%
%%% Miriam Maan %%%
%%% 28-01-2015 %%%%%

% This script shows instruction screeens, a practice session and a test
% session of the SART
% it starts up Eyelink and send event markers to the data
% it also records physiological data (GSR)
% response button = SHIFT


%% udpA coding
% d = target stimuli (3)
% o = other stimuli (1-2,4-9)
% r = response
% l = no response 
% f = fixation screen
% i = instruction screen
% p = probe1
% k = probe2


%% Eyelink Coding
% Synctime = marks start of trial
% Trial VAR = stores all trail variables
%   trial type, trial start, resptime, respbutton, 
% TRIAl END = ends a trial

%command out the physiological software
phys = 'on';

%command out the practice session
practice = 'on';

% command out the probes
probe_on = 'on';

% command out eyelink
eye_on = 'on';

% command out instruction
inst_on = 'on';

%command out experiment
exp = 'on';

clear mex; 

if strcmp(phys, 'on')
    % set up host and receiver ports for transfering data
    ipA='150.100.100.20'; portA=9091;
    ipB='150.100.100.21'; portB=9090;
    udpA=udp(ipB, portB, 'LocalPort', portA, 'InputBufferSize', 8);
    udpA.terminator='LF/CR';
    fopen(udpA)
end

addpath(genpath(cd));

%Participant_ID
fail1= 'Participant nunber not entered';
prompt = {'Enter participant number:'};
dlg_title = 'New Participant SART';
num_lines= 1;
def = {'0'};
answer = inputdlg(prompt,dlg_title,num_lines,def);



switch isempty(answer)
    case 1
        error(fail1)
    case 0
        thissub=(answer{1});
end

%Safe results
%ResultsFolder = 'C:\Users\Miriam\Documents\Second Internship\Matlab\Results\Matlab\';
ResultsFolder = 'C:\Documents and Settings\Eyelink Display\My Documents\MATLAB\miriam\Results';
Outputfile = [ResultsFolder 'SART_ses1_', thissub '.mat'];
OutputfileEDF = ['SART_', thissub '.edf'];                           %%%%%%

%%% Present Instruction Screen %%%
[win, rect] = Screen('OpenWindow',0,128);                   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%change this for eyelink
CompScreen = rect;
[xCenter,yCenter] = RectCenter(rect);

if strcmp(eye_on, 'on')
    %prepare Eyelink, send messages to the Eyelink file
    el = prepEyelink(win, OutputfileEDF);
    el.height = CompScreen(4);
    el.width = CompScreen(3);

    %write screen size to Eyelink message file
    Eyelink('message', 'DISPLAY_COORDS %d %d %d %d',CompScreen(1), CompScreen(2), ...
       CompScreen(3)-1, CompScreen(4)-1);
    % Disable sync testing (needed because we're opening a small window on the screen)
    Screen('Preference', 'SkipSyncTests', 1);
end
%[win,rect] = Screen('OpenWindow',0, 128); 
 Screen('TextSize',win, 15);
 Screen('TextFont',win,'Arial');

 escapeKey = KbName('q');
  %Collect keybord response
         [keyIsDown, secs,keycode]=KbCheck;
         WaitSecs(0.001); 
  total_events = 1;       
       
 if keyIsDown   
      if keyIsDown == escapeKey
         sca;
         close all;
      end
 end
 if strcmp(inst_on, 'on')
txt_1 = [ 'INSTRUCTIONS ' sprintf('\n\n\n') ...
       'Welcome to the experiment.' sprintf('\n\n')...  
       'The point of the experiment is to respond to all digital numbers (1,2,4-9) except the number 3, and to avoid hitting the SHIFT-key when the 3 appears.' sprintf('\n\n') ... 
       'So please do not rush but respond carefully so that you make as few errors as possible. We would like you to slow down, so that you reduce the number of errors that you make.' sprintf('\n\n') ... 
       'You have approximately one second to respond before the next digit appears, so you’ll still have to respond fairly quickly, but we would like you to take as much time as you can, before responding to the digit. As long as you respond to one digit before the next appears, your response will count.' sprintf('\n\n\n\n') ...
       'Press "SHIFT" to go to the next instruction screen '];
        
DrawFormattedText(win, txt_1, 'center', 'center', [0 0 0],100 ,[],[],2);
Screen('Flip',win);
if strcmp(phys, 'on')
    fprintf(udpA, 'i');
end
WaitSecs(.5);% this avoids participants accidently pressing too quicklyand moving the experiment on
KbWait;

txt_2 =['INSTRUCTIONS ' sprintf('\n\n\n') ...
       'At some points during the experiment, you will be asked to report where your attention was during the previous trials. You can choose between "off-task" and "on task".' sprintf('\n')...
       'Being "on-task" means thinking only about task-related things (e.g., one’s performance on the task, the relative time of the button press, etc.).' sprintf('\n')... 
       'Being "off-task" means thinking about internally focused, task- unrelated things (e.g., plans with friends, an upcoming exam, etc...).' sprintf('\n\n')...
       ' Furthermore, we want you to report your confidence in this rating.' sprintf('\n')...
       ' You can choose between  not at all, somewhat,very or extremely confident.' sprintf('\n')...
       'You make your choice by pressing the SHIFT-key, that will move the pointer along the bar.' sprintf('\n')...
       'You have approximately 5 seconds to answer both questions.' sprintf('\n\n\n')...
       'Press "SHIFT" to continue to the next instruction screen '];
 
 DrawFormattedText(win, txt_2, 'center', 'center', [0 0 0],100 ,[],[],2);
 Screen(win,'Flip');
 if strcmp(phys, 'on')
    fprintf(udpA, 'i');
 end
 WaitSecs(.5);
 KbWait;
 %HideCursor;
 WaitSecs(.5);

 % have a probe trial
 expStart = GetSecs;   
 keyDownFlag = 0;
 numButtonEvents = 0;
 buttonEvents = [];
 ProbeDur = 5.0;
 Results.data=[];
 trial=1;
 lasttrial=0;
 last_digit=1;
 stim=1;
 RightKey = KbName('SHIFT');
 escapeKey = KbName('q');
 [xCenter,yCenter] = RectCenter(rect);
 pixelsPerPress=200;
 LineX = ((xCenter-100) + (200 * randint(1,1,2)));
 ctr = 5;
 ctr_num=num2str(ctr);
    
        Screen('Textsize',win,20);
        Screen('TextFont',win,'Arial');
        txt_5= ['Where was your attention during the last 3 to 5 trials?' sprintf('\n\n')...
          'on-task              off-task']; % sprintf('\n\n')...             
        Screen ('DrawText', win,'|', LineX,425,[0 0 0]); 
        Screen ('DrawText', win,ctr_num, xCenter,650,[0 0 0]); 
        DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
        Screen('Flip',win);
        Probestart = GetSecs;
        
       while  GetSecs < Probestart + expStart + ProbeDur

  
            [keyIsDown, secs, keyCode]=KbCheck;
            WaitSecs(0.001);
            pressedKeys = find (keyCode);
     
         if keyIsDown
            
            if pressedKeys == escapeKey
                exitExperimentFlag = 1;
                resp =2;
                break;
             
            elseif keyCode(RightKey)>0
                LineX = LineX + pixelsPerPress; 
                 ctr = ctr - 1;

                        
                if LineX < (xCenter -100)
                    LineX=(xCenter+100);
                elseif LineX > (xCenter + 100)
                    LineX=(xCenter -100);
                end
                          
                      end
            end


                while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                    [keyIsDown, ~, ~]=KbCheck;
                    WaitSecs(0.001);
                end
                
              if ctr == -1
                  break
              end
     ctr_num=num2str(ctr);         
     DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
     Screen ('DrawText', win,'|', LineX,425,[0 0 0]);
     Screen ('DrawText', win,ctr_num, xCenter,650,[0 0 0]);
     Screen('Flip', win);
     keyDownFlag = 0;
     
   end %end of while probe loop
   
   [xCenter,yCenter] = RectCenter(rect);
   pixelspp=150;
   linex = ((xCenter-225) + (150 * randint(1,1,4)));
   ctr = 10;
   Screen('Textsize',win,20);
   Screen('TextFont',win,'Arial');
        txt_5=['How confident are you in your answer?' sprintf('\n\n')...
             '  not at all      somewhat       very        extremely ' ];
   Screen ('DrawText', win,'|', linex,430,[0 0 0]); 
   Screen('Flip',win);
   Probe2 = GetSecs - expStart
  
 
          while  GetSecs < Probestart + expStart + ProbeDur

  
            [keyIsDown, secs, keyCode]=KbCheck;
            WaitSecs(0.001);
            pressedKeys = find (keyCode);
     
         if keyIsDown
            
            if pressedKeys == escapeKey
                exitExperimentFlag = 1;
                resp =2;
                break;
             
            elseif keyCode(RightKey)>0
                linex = linex + pixelspp; 
                 ctr = ctr - 1;

                        
                if linex < (xCenter -225)
                    linex=(xCenter+225);
                elseif linex > (xCenter + 225)
                    linex=(xCenter -225);
                end
                          
                      end
            end


                while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                    [keyIsDown, ~, ~]=KbCheck;
                    WaitSecs(0.001);
                end
                
              if ctr == -1
                  break
              end
     ctr_num=num2str(ctr);         
     DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],90 ,[],[],1.5);
     Screen ('DrawText', win,'|', linex,430,[0 0 0]);
     Screen ('DrawText', win,ctr_num, xCenter,650,[0 0 0]);
     Screen('Flip', win);
     keyDownFlag = 0;
     
   end %end of while probe loop
   
   
   
Screen('TextSize',win, 15);
 Screen('TextFont',win,'Arial');
 
 txt_3 = ['INSTRUCTIONS' sprintf('\n\n\n')...
     ' The total duration of the experiment is 30 minutes, including a practice session. We will track your skin conductance levels and eye gaze during the total duration of the experiment.' sprintf('\n\n\n\n')...
     'Press "SHIFT" to continue to eye track calibration.'];
 
 DrawFormattedText(win, txt_3, 'center', 'center', [0 0 0],100 ,[],[],2);
 Screen(win,'Flip');
 if strcmp(phys, 'on')
    fprintf(udpA, 'i');
 end
 WaitSecs(.5);
 KbWait;
 %HideCursor;
 WaitSecs(.5); 
 
 
 txt_6 = [ 'INSTRUCTIONS ' sprintf('\n\n') ...
       ' Start of Eye Calibration' sprintf('\n\n')...
       'Look at the screen and follow the moving dot.' sprintf('\n')...
       'Make sure your eyes stay fixated at the dot before it jumps to a different location.' sprintf('\n\n\n')...
       ' Wait for experimenter' ];
        
DrawFormattedText(win, txt_6, 'center', 'center', [0 0 0],85 ,[],[],2);
Screen('Flip',win);
if strcmp(phys, 'on')
    fprintf(udpA, 'i');
end
WaitSecs(.5);% this avoids participants accidently pressing too quicklyand moving the experiment on
KbWait;
 end
if strcmp(eye_on, 'on')
    err=EyelinkDoTrackerSetup(el, el.ENTER_KEY);
    % start recording eye position
    Eyelink('startrecording' );
    % record a few samples before we actually start displaying
    WaitSecs(0.1);
end
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Loop for the practice session
 %%
 
if strcmp(exp, 'on')
 %create variables
    NPracticeTrials = 15; % Amount of trials for testing
    NBlocks=3;
    ProbeDur1 = 3.5; %duration of probe
    ProbeDur2 = 4.5;
    FixDur=0.9;  %duration of fixation cross
    StimDur=0.45; %045%duration of stimulus
    TrialDur = FixDur+StimDur;
    Results.data=[];
    trial=1;
    expStartTime = GetSecs;
    lasttrial=0;
    last_digit=1;
    stim=1;
    RightKey = KbName('SHIFT');
    escapeKey = KbName('q');
    [xCenter,yCenter] = RectCenter(rect);
    pixelsPerPress=100;
    LineX = ((xCenter-150) + (100 * randint(1,1,2)));
    ctr =0;
    size = [10,12.5,15];
    thick = [3,3.5,4];
    distance = [1.25,2,2.5];
    k= randi(3);
    txt_size = size(k);
    width = thick(k);
    D = distance(k);
         if k==1
             t=0.75;
             s=1;
             v=0;
             u=0.25;
         elseif k==2 
             t=1;
             v=0.5;
             s=0;
             u=0;
         else
             t=0.5;
             v=1;
             s=0.25;
             u=0;
         end
   
    %% PRACTICE %%
 
if strcmp(practice, 'on')
     
 txt_8 = [ 'Start of Practice Session.' sprintf('\n\n')...
     'Press "SHIFT" to start experiment.'];
        
DrawFormattedText(win, txt_8, 'center', 'center', [0 0 0],85 ,[],[],2);
Screen('Flip',win);
if strcmp(phys, 'on')
    fprintf(udpA, 'i');
end
WaitSecs(0.2);% this avoids participants accidently pressing too quicklyand moving the experiment on
KbWait;
        
 for blocks = 1:NBlocks;
     resp = 000;
     resptime=000;
     keyDownFlag = 0;
     numButtonEvents = 0;
     buttonEvents = []; 
     exitExperimentFlag = 0;
        size = [10,12.5,15];
         thick = [3,3.5,4];
         distance = [1.25,2,2.5];
         k =randi(3);
         txt_size = size(k);
         width = thick(k);
         D = distance(k);
         n = 1;
         if k==1
             t=0.75;
             s=1;
             v=0;
             u=0.25;
         elseif k==2 
             t=0.5;
             v=0.5;
             s=0;
             u=0;
         else
             t=0.25;
             v=0.75;
             s=0.25;
             u=0;
         end
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t-u),yCenter-(txt_size*2)+D-n,xCenter+txt_size-(n*D+t+t),yCenter-(txt_size*2)+D-n,[width]); %1-         
      Screen('DrawLine',win,[0 0 0],xCenter-txt_size+(n*D+t+t),yCenter,xCenter+txt_size-(n*D+t+t),yCenter,[width]); % 2 -
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t),yCenter+(txt_size*2)-D,xCenter+txt_size-(n*D+t+t),yCenter+(txt_size*2)-D,[width]);
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter-(0.5*D),xCenter-txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 1
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter-(0.5*D),xCenter+txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 2
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter+(0.5*D),xCenter-txt_size,yCenter+(txt_size*2),[width]); %| 3
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter+(0.5*D),xCenter+txt_size,yCenter+(txt_size*2),[width]); %| 4
      Screen('Flip', win);
      WaitSecs(1.2);
      
    for trial = 1: NPracticeTrials;

     resp = 000;
     resptime=000;
     keyDownFlag = 0;
     numButtonEvents = 0;
     buttonEvents = [];
     last_digit = stim;
    
     %Present stimuli
     stim=randi(9);
        while (stim == last_digit) 
         stim = randi(9);           
        end
      if stim ==3
          if strcmp(phys, 'on')
            fprintf(udpA,'d')
          end
      else
           if strcmp(phys, 'on')
             fprintf(udpA,'o')
           end
      end
      
       if (stim == 1)
           [a,b,c,d,f] = deal(110); %make grey
           [e,g]=deal(0); %make black
       elseif (stim == 2)
           [d,g] = deal(110);
           [a,b,c,e,f] = deal(0);
       elseif (stim == 3)
           [d,f]=deal(110);
           [a,b,c,e,g] = deal(0);
       elseif (stim == 4)
           [a,c,f]=deal(110);
           [b,d,e,g] = deal(0);
       elseif (stim == 5)
           [e,f]=deal(110);
           [a,b,c,d,g] = deal(0);
       elseif (stim == 6)
           [e ]=deal(110);
           [a,b,c,d,f,g] = deal(0);
       elseif (stim == 7)
           [b,c,d,f]=deal(110);
           [a,e,g] = deal(0);
       elseif (stim == 8)
         %   []=deal(110); 
           [a,b,c,d,e,f,g] = deal(0);
       elseif (stim == 9)
           [f]=deal(110);
           [a,b,c,d,e,g] = deal(0);
       end
       
      Screen('DrawLine',win,[a a a],xCenter-txt_size+(n*D+t+t-u),yCenter-(txt_size*2)+D-n,xCenter+txt_size-(n*D+t+t),yCenter-(txt_size*2)+D-n,[width]); %1-         
      Screen('DrawLine',win,[b b b],xCenter-txt_size+(n*D+t+t),yCenter,xCenter+txt_size-(n*D+t+t),yCenter,[width]); % 2 -
      Screen('DrawLine',win,[c c c],xCenter-txt_size+(n*D+t+t),yCenter+(txt_size*2)-D,xCenter+txt_size-(n*D+t+t),yCenter+(txt_size*2)-D,[width]);
      Screen('DrawLine',win,[d d d],xCenter-txt_size,yCenter-(0.5*D),xCenter-txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 1
      Screen('DrawLine',win,[e e e],xCenter+txt_size,yCenter-(0.5*D),xCenter+txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 2
      Screen('DrawLine',win,[f f f],xCenter-txt_size,yCenter+(0.5*D),xCenter-txt_size,yCenter+(txt_size*2),[width]); %| 3
      Screen('DrawLine',win,[g g g],xCenter+txt_size,yCenter+(0.5*D),xCenter+txt_size,yCenter+(txt_size*2),[width]); %| 4
      Screen('Flip', win);

     StimStart=GetSecs - expStartTime; % get the exact timing of when the stimuls was on the screen
     fixPresented = 0;
     recordFix = 0;
   
     while GetSecs < expStartTime + StimStart + TrialDur   %if total duration is less then 1000 ms
         
         if GetSecs-(StimStart+expStartTime) > StimDur     %only true if stim duration has passed, then present fix cross
                    
              %Present fixation cross

      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t-u),yCenter-(txt_size*2)+D-n,xCenter+txt_size-(n*D+t+t),yCenter-(txt_size*2)+D-n,[width]); %1-         
      Screen('DrawLine',win,[0 0 0],xCenter-txt_size+(n*D+t+t),yCenter,xCenter+txt_size-(n*D+t+t),yCenter,[width]); % 2 -
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t),yCenter+(txt_size*2)-D,xCenter+txt_size-(n*D+t+t),yCenter+(txt_size*2)-D,[width]);
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter-(0.5*D),xCenter-txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 1
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter-(0.5*D),xCenter+txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 2
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter+(0.5*D),xCenter-txt_size,yCenter+(txt_size*2),[width]); %| 3
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter+(0.5*D),xCenter+txt_size,yCenter+(txt_size*2),[width]); %| 4
      Screen('Flip', win);
      FixStart=GetSecs;
             
              if recordFix == 0
                 recordFix = 1;
             end
             
             WaitSecs(FixDur);
             fixPresented = 1;
         end
     
         %Collect keybord response
         [keyIsDown, secs,keycode]=KbCheck;
         WaitSecs(0.001); % delay to prevent CPU hogging
        
         if keyIsDown   
             
             if keyIsDown == escapeKey
                sca;
                close all;
            end
            if keycode(RightKey)>0
                if strcmp(phys, 'on')
                    fprintf(udpA, 'r');
                end
                 if keyDownFlag == 0
                   resp = 'm';
                    
                     if resp=='m'
                         resp=1;
                     else
                         resp=0;  
                     end                     
                   
                       % record data to results array
                   resptime = secs - (StimStart+expStartTime);
                   buttonEvents(numButtonEvents+1).resp = resp;
                   buttonEvents(numButtonEvents+1).rt = resptime;
                 
                   numButtonEvents = numButtonEvents +1;
                   keyDownFlag = 1;
                                    
                 end
           end
        else %if no key is pressed
            keyDownFlag = 0;
        end     
     end                  %end the while loop
   
             %to record a response if there is no key press
             if numButtonEvents>0
                 total_events= numButtonEvents + total_events ;
             else
                 total_events = total_events + 1;  
                 %
             end
         end                  %end the trial loop
 
         %%
         %%%%%%%%%%%%%%%%%%%%% trial Probe %%%%
        lasttrial = trial + lasttrial;
        keyDownFlag = 0;
        numButtonEvents = 0;
        buttonEvents = [];
        LineX = ((xCenter-100) + (200 * randint(1,1,2)));
        pixelsPerPress =200;
        %Present Probe
        Screen('Textsize',win,20);
        Screen('TextFont',win,'Arial');
        txt_5=['Where was your attention during the last 3 to 5 trials?' sprintf('\n\n')...
             'on-task              off-task' ];
        Screen ('DrawText', win,'|', LineX,425,[0 0 0]); 
        DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
        Screen('Flip',win);
        ProbeStart=GetSecs - expStartTime;
        txt_size = 00;
                   
       while GetSecs < ProbeStart + expStartTime + ProbeDur1

  
            [keyIsDown, secs, keyCode]=KbCheck;
            WaitSecs(0.001);
            pressedKeys = find (keyCode);
     
         if keyIsDown
            
            if pressedKeys == escapeKey
                exitExperimentFlag = 1;
                resp =2;
                break;
             
            elseif keyCode(RightKey)>0
                LineX = LineX + pixelsPerPress; 
   
                     if keyDownFlag == 0
                        resp = 'm';

                            if resp=='m'
                                resp=3;
                            else
                                resp=0;  
                            end                     
   
                        resptime = secs - (ProbeStart+expStartTime);
                        buttonEvents(numButtonEvents+1).resp = resp;
                        buttonEvents(numButtonEvents+1).rt = resptime;
                        numButtonEvents = numButtonEvents +1;
                        keyDownFlag = 1;
                        probe=8888;

                        
                if LineX < (xCenter -100)
                    LineX=(xCenter+100);
                elseif LineX > (xCenter + 100)
                    LineX=(xCenter -100);
                end
                         
                      end
            end


                while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                    [keyIsDown, ~, ~]=KbCheck;
                    WaitSecs(0.001);
                end
                
         else %if no key is pressed
            keyDownFlag = 0;
            buttonEvents = [];
         end    
         
               
     DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
     Screen ('DrawText', win,'|', LineX,425,[0 0 0]);
     Screen('Flip', win);
     keyDownFlag = 0;
    

     
   end %end of while probe loop
        if numButtonEvents == 0
           % ctr = ctr+1;
            % Results.data((ctr),:)= [ 686 ctr total_events numButtonEvents probe LineX ProbeStart resptime resp txt_size];
        end

        if exitExperimentFlag
            break;
        end


     
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Probe screen 2
       %%
             
        lasttrial = trial + lasttrial;
        keyDownFlag = 0;
        numButtonEvents = 0;
        buttonEvents = [];
        pixelsperpress = 150;
        linex = ((xCenter-225) + (150 * randint(1,1,4)));
        total_events = total_events +1;
        
        Screen('Textsize',win,20);
        Screen('TextFont',win,'Arial');
        txt_5=['How confident are you in your answer?' sprintf('\n\n')...
                   '  not at all      somewhat       very        extremely ' ];
        Screen ('DrawText', win,'|', linex,430,[0 0 0]); 
        Screen('Flip',win);    
        DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
        Screen('Flip',win);
       
        ProbeOne=GetSecs - expStartTime;
        txt_size = 00;
        probe = 9999;
       
        while GetSecs < ProbeOne + expStartTime + ProbeDur2
     
            [keyIsDown, secs, keyCode]=KbCheck;
            WaitSecs(0.001);
            pressedKeys = find (keyCode);
     
         if keyIsDown
            
            if pressedKeys == escapeKey
                exitExperimentFlag = 1;
                resp =2;
                break;
             
            elseif keyCode(RightKey)>0
                linex = linex + pixelsperpress; 
   
                     if keyDownFlag == 0
                        resp = 'm';

                            if resp=='m'
                                resp=3;
                            else
                                resp=0;  
                            end                     
   
                        resptime = secs - (ProbeOne+expStartTime);
                        buttonEvents(numButtonEvents+1).resp = resp;
                        buttonEvents(numButtonEvents+1).rt = resptime;
                        numButtonEvents = numButtonEvents +1;
                        ctr = ctr + 1;
                        keyDownFlag = 1;
                        probe=99999;

                        
                if linex < (xCenter -225)
                    linex=(xCenter+225);
                elseif linex > (xCenter + 225)
                    linex=(xCenter -225);
                end
                          
                      end
            end


                while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                    [keyIsDown, ~, ~]=KbCheck;
                    WaitSecs(0.001);
                end
                
         else %if no key is pressed
            keyDownFlag = 0;
            buttonEvents = [];
         end    
         
               
     DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
     Screen ('DrawText', win,'|', linex,430,[0 0 0]);
     Screen('Flip', win);
     keyDownFlag = 0;
    

   end %end of while probe loop
   

        if exitExperimentFlag
            break;
        end

             if numButtonEvents>0
                total_events= numButtonEvents + total_events  
             end
    end % command out prbe
    

  
 %%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END OF PRACTICE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end %comment outof practise session

 exitExperimentFlag =0;
if exitExperimentFlag == 0
    txt= [''];
    Screen('FillRect',win, 128);% fill the screen with grey
    Screen('Textsize',win,35);
    DrawFormattedText(win,txt,'center','center',[0,0,0], 1,[],[],1.5);
    Screen('Flip', win);
    WaitSecs(0.8);
     if strcmp(phys, 'on') 
            fprintf(udpA,'f')
     end  

     Screen('TextSize',win, 16);
     Screen('TextFont',win,'Arial');
     txt_6 = ['INSTRUCTIONS ' sprintf('\n\n\n') ...
    'The practice session is now complete and you are about to begin the task. Please remember that the point of the task is to make as few errors as possible; that is, to  respond to all numbers except 3 and avoid hitting the Right Shift button when the 3 appears. So please DO NOT RUSH, but take your time, SLOW DOWN, and respond carefully so that you make as few errors as possible.' sprintf('\n\n\n\n')...
    'Wait for experimenter. '];
     DrawFormattedText(win, txt_6, 'center', 'center', [0 0 0],75 ,[],[],2);
     Screen(win,'Flip');
     if strcmp(phys, 'on')
         fprintf(udpA, 'i');
     end
     WaitSecs(.5);
     KbWait;
     WaitSecs(.5);


     %show a fixation screen such that there is some time before the start
     %of the blcoks
     txt= [''];
     Screen('FillRect',win, 128);% fill the screen with grey
     Screen('Textsize',win,35);
     DrawFormattedText(win,txt,'center','center',[0,0,0], 1,[],[],1.5);
     Screen('Flip', win);
     WaitSecs(0.4);


     %%%% Loop for experimental session %%%%
%%
     %create variables
    NRBlocks=40;
    ProbeDur1 = 2.5; %duration of probe
    ProbDur2 = 3.5;
    FixDur=0.9;  %duration of fixation cross
    StimDur=0.35; %duration of stimulus
    TrialDur = FixDur+StimDur;
    Results.data=[];
    total_events=0;
    trial=1;
    probe =9999;
    lasttrial=0;
    last_digit=1;
    stim=1;
    RightKey = KbName('SHIFT');
    escapeKey = KbName('q');
    [xCenter,yCenter] = RectCenter(rect);
    slack = Screen('GetFlipINterval',win);
    vbl = Screen('Flip',win);
    pixelsPerPress=200;
    waitframes = 5;
    LineX = ((xCenter-100) + (150 * randint(1,1,4)));
    LineY = yCenter;
    ProbeStart=0;
    NProbes =40; 
    ctr =0;
    last_digit =3;
    prev_digit =3;
   % buttonEvents = [];'
   expStartTime = GetSecs;
%%%%% EXPERIMENT LOOP %%%%
%%
     for blocks = 1:NRBlocks
         
         
         if blocks == 20;    % have a break
                
                shiftkey = 5;
             shift_num=num2str(shiftkey);
            Screen('Textsize',win,20);
            Screen('TextFont',win,'Arial');
            txt_0=['Break' sprintf('\n\n')...
              'Press "SHIFT" 5 times to continue with the experiment.' sprintf('\n\n')...
              'Please keep your head in the headrest'];
            Screen ('DrawText', win,shift_num, xCenter,650,[0 0 0]); 
            DrawFormattedText(win, txt_0, 'center', 'center', [0 0 0],75 ,[],[],1.5);
            Screen('Flip',win);
            if strcmp(phys, 'on')
                fprintf(udpA, 'i');
            end
            KbWait;
            PauseStart = GetSecs;
            PauseDur = 10;
                  
                          
                 while  GetSecs < PauseStart + PauseDur

                    [keyIsDown, secs, keyCode]=KbCheck;
                    WaitSecs(0.001);
                    pressedKeys = find (keyCode);
     
                          if keyIsDown
            
                                if pressedKeys == escapeKey
                                    exitExperimentFlag = 1;
                                    resp =2;
                                    break;
             
                                elseif keyCode(RightKey)>0
                                    shiftkey = shiftkey - 1;
                                end
                          end


                      while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                            [keyIsDown, ~, ~]=KbCheck;
                            WaitSecs(0.001);
                      end
                
                      if shiftkey == -1
                          break
                      end
                 shift_num=num2str(shiftkey);         
                 DrawFormattedText(win, txt_0, 'center', 'center', [0 0 0],75 ,[],[],1.5);
                 Screen ('DrawText', win,shift_num, xCenter,650,[0 0 0]);
                 Screen('Flip', win);
                 keyDownFlag = 0;

                end %end of while loop
         end   % end of pause loop
         
         resp = 000;
         resptime=000;
         keyDownFlag = 0;

         size = [10,12.5,15];
         thick = [3,3.5,4];
         distance = [1.25,2,2.5];
         k =randi(3);
         txt_size = size(k);
         width = thick(k);
         D = distance(k);
         n = 1;
         if k==1
             t=0.75;
             s=1;
             v=0;
             u=0.25;
         elseif k==2 
             t=0.5;
             v=0.5;
             s=0;
             u=0;
         else
             t=0.25;
             v=0.75;
             s=0.25;
             u=0;
         end
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t-u),yCenter-(txt_size*2)+D-n,xCenter+txt_size-(n*D+t+t),yCenter-(txt_size*2)+D-n,[width]); %1-         
      Screen('DrawLine',win,[0 0 0],xCenter-txt_size+(n*D+t+t),yCenter,xCenter+txt_size-(n*D+t+t),yCenter,[width]); % 2 -
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t),yCenter+(txt_size*2)-D,xCenter+txt_size-(n*D+t+t),yCenter+(txt_size*2)-D,[width]);
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter-(0.5*D),xCenter-txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 1
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter-(0.5*D),xCenter+txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 2
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter+(0.5*D),xCenter-txt_size,yCenter+(txt_size*2),[width]); %| 3
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter+(0.5*D),xCenter+txt_size,yCenter+(txt_size*2),[width]); %| 4
      Screen('Flip', win);
      WaitSecs(1.2);
        
       if strcmp(phys, 'on') 
          fprintf(udpA,'f')
       end    
          NRTrials = randi(9)+ 20; % amount of trials in a block
        
                  
        for trial = 1:NRTrials;
           total_events = total_events +1;
                              
            if strcmp(eye_on, 'on')
                 Eyelink('Startrecording');
                 % mark zero-plot time in data file
                 Eyelink(' message', 'SYNCTIME' );%% should this be here or at he start of the experiment?
            end
         resp = 000;
         resptime=000;
         keyDownFlag = 0;
         numButtonEvents = 0;
         buttonEvents = [];
         last_digit = stim;
        
         %Present stimuli
           if strcmp(phys, 'on') 
                        fprintf(udpA,'f')
                   end  
           

            stim = randi(9) ; 
            if (stim == last_digit)
                stim = randi(9);
            elseif (stim == prev_digit)
                 stim = randi(9);
            end

            prev_digit = last_digit;
            last_digit = stim;
          
            if strcmp(phys, 'on') 
                if stim == 3
                    fprintf(udpA,'d')
                else
                    fprintf(udpA,'o')
                end
            end
            if strcmp(eye_on, 'on')
                 Eyelink('message','TRIAL_ID %d', stim);
            end
            
       if (stim == 1)
           [a,b,c,d,f] = deal(110); %make grey
           [e,g]=deal(0); %make black
       elseif (stim == 2)
           [d,g] = deal(110);
           [a,b,c,e,f] = deal(0);
       elseif (stim == 3)
           [d,f]=deal(110);
           [a,b,c,e,g] = deal(0);
       elseif (stim == 4)
           [a,c,f]=deal(110);
           [b,d,e,g] = deal(0);
       elseif (stim == 5)
           [e,f]=deal(110);
           [a,b,c,d,g] = deal(0);
       elseif (stim == 6)
           [e ]=deal(110);
           [a,b,c,d,f,g] = deal(0);
       elseif (stim == 7)
           [b,c,d,f]=deal(110);
           [a,e,g] = deal(0);
       elseif (stim == 8)
         %   []=deal(110); 
           [a,b,c,d,e,f,g] = deal(0);
       elseif (stim == 9)
           [f]=deal(110);
           [a,b,c,d,e,g] = deal(0);
       end 
             
             
      Screen('DrawLine',win,[a a a],xCenter-txt_size+(n*D+t+t-u),yCenter-(txt_size*2)+D-n,xCenter+txt_size-(n*D+t+t),yCenter-(txt_size*2)+D-n,[width]); %1-         
      Screen('DrawLine',win,[b b b],xCenter-txt_size+(n*D+t+t),yCenter,xCenter+txt_size-(n*D+t+t),yCenter,[width]); % 2 -
      Screen('DrawLine',win,[c c c],xCenter-txt_size+(n*D+t+t),yCenter+(txt_size*2)-D,xCenter+txt_size-(n*D+t+t),yCenter+(txt_size*2)-D,[width]);
      Screen('DrawLine',win,[d d d],xCenter-txt_size,yCenter-(0.5*D),xCenter-txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 1
      Screen('DrawLine',win,[e e e],xCenter+txt_size,yCenter-(0.5*D),xCenter+txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 2
      Screen('DrawLine',win,[f f f],xCenter-txt_size,yCenter+(0.5*D),xCenter-txt_size,yCenter+(txt_size*2),[width]); %| 3
      Screen('DrawLine',win,[g g g],xCenter+txt_size,yCenter+(0.5*D),xCenter+txt_size,yCenter+(txt_size*2),[width]); %| 4
      Screen('Flip',win); %Present to the screen
     
      % Eyelink('message', 'STIMSTART = %d', StimStart); %Eyelink MIGHT NOT LIKE THIS
      StimStart=GetSecs; % get the exact timing of when the stimuls was on the screen
      fixPresented = 0;
      recordFix = 0;        
       
       
         while GetSecs < StimStart + TrialDur   %if total duration is less then 1200 ms   
             
             [keyIsDown, secs,keycode]=KbCheck;
             WaitSecs(0.001); % delay to prevent CPU hogging
             pressedKeys = find (keycode);
             
             if fixPresented == 0 && GetSecs-(StimStart) > StimDur     %only true if stim duration has passed, then present fix cross

                  % Present fixation cross
        
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t-u),yCenter-(txt_size*2)+D-n,xCenter+txt_size-(n*D+t+t),yCenter-(txt_size*2)+D-n,[width]); %1-         
      Screen('DrawLine',win,[0 0 0],xCenter-txt_size+(n*D+t+t),yCenter,xCenter+txt_size-(n*D+t+t),yCenter,[width]); % 2 -
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size+(n*D+t+t),yCenter+(txt_size*2)-D,xCenter+txt_size-(n*D+t+t),yCenter+(txt_size*2)-D,[width]);
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter-(0.5*D),xCenter-txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 1
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter-(0.5*D),xCenter+txt_size,yCenter-(txt_size*2)-(0.5*D-v),[width]); %| 2
      Screen('DrawLine',win,[110 110 110],xCenter-txt_size,yCenter+(0.5*D),xCenter-txt_size,yCenter+(txt_size*2),[width]); %| 3
      Screen('DrawLine',win,[110 110 110],xCenter+txt_size,yCenter+(0.5*D),xCenter+txt_size,yCenter+(txt_size*2),[width]); %| 4
      Screen('Flip',win); %Present to the screen
         
      FixStart=GetSecs;
                   if strcmp(phys, 'on') 
                        fprintf(udpA,'f')
                   end  
                 
                 if recordFix == 0
                  %   Eyelink('message', 'FIXCROSS ONSET %d', FixStart);
                     recordFix = 1;
                 end

                 %WaitSecs(FixDur);
                 fixPresented = 1;
             end
             
          
             if keyIsDown   

                 if pressedKeys == escapeKey
                     exitExperimentFlag = 1;
                     resp = 2;
                     break;
         
                 elseif keycode(RightKey)>0
                     if keyDownFlag == 0
                        resp = 'm';
                             if resp=='m'
                                 resp=1
                             else
                                 resp=0;  
                             end                     
                        %record data to  array
                        resptime = secs - (StimStart);
                        buttonEvents(numButtonEvents+1).resp = resp;
                        buttonEvents(numButtonEvents+1).rt = resptime;      
                        numButtonEvents = numButtonEvents +1;
                        ctr = ctr +1;
                        keyDownFlag = 1;
                           
                        for j=1:numButtonEvents
                           Results.data((ctr),:)= [ 77 ctr total_events numButtonEvents trial+lasttrial stim (StimStart-expStartTime) resptime resp txt_size];
                        end  
                           
                     end
                     
                 end  
                      while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                         [keyIsDown, ~, ~]=KbCheck;
                         WaitSecs(0.001);
                           
                     end 
     
             else  %if ~keyIsDown %if no key is pressed % solves that a response is stored after numButtonEvents =1
                keyDownFlag = 0;   
                buttonEvents =[];
             end

         end  %end the while loop to record key presses
         numButtonEvents;
         
    if numButtonEvents == 0
        ctr = ctr+1;
        Results.data(ctr,:)= [11 ctr total_events numButtonEvents trial+lasttrial stim (StimStart-expStartTime) resptime resp txt_size];
     end

                 if numButtonEvents>0
                    total_events= numButtonEvents + total_events;
                 end
           
        if strcmp(eye_on, 'on')
              WaitSecs (0.001);
              if stim == 3
                 Eyelink('Message', '!V TRIAL_VAR trialtype target');
              else
                 Eyelink('Message', '!V TRIAL_VAR trialtype no_target');
              end

              Eyelink('Message', '!V TRIAL_VAR trialnum %d', trial+lasttrial);
              Eyelink('Message', '!V TRIAL_VAR stimValue %d', stim);        %might be to difficult for eylink
              Eyelink('Message', '!V TRIAL_VAR trialstart %4.4f', (StimStart-expStartTime));
              Eyelink('Message', '!V TRIAL_VAR ctr %d', ctr);
              
              
              if numButtonEvents >0 
                 for j=1:numButtonEvents
                     Eyelink('Message', '!V TRIAL_VAR RESP_TIME %d=%d',numButtonEvents, resptime);
                     Eyelink('Message', '!V TRIAL_VAR RESP_NUM %d=%d',numButtonEvents, resp);
                 end 
              else
                   Eyelink('Message', '!V TRIAL_VAR RESP_TIME %d=%d',000, 000);
                   Eyelink('Message', '!V TRIAL_VAR RESP_NUM %d=%d',000, 000);
              end
               Eyelink('Stoprecording');
               Eyelink(' message', 'ENDTIME' ); %mark end of a trial
               Eyelink('Message', 'TRIAL_RESULT 0'); 
        end

   end
        ctr=ctr+1;
        %end the trial loop
%% end of trials start of probe           %%%%%%%%%%%%%  SCREEN 1 %%%%%%%%%%%%5
if strcmp(probe_on, 'on')
    
        lasttrial = trial + lasttrial;
        keyDownFlag = 0;
        numButtonEvents = 0;
        buttonEvents = [];
        LineX = ((xCenter-100) + (200 * randint(1,1,2)));
    
        %Present Probe
        Screen('Textsize',win,20);
        Screen('TextFont',win,'Arial');
        txt_5=['Where was your attention during the last 3 to 5 trials?' sprintf('\n\n')...
             'on-task              off-task' ];
        Screen ('DrawText', win,'|', LineX,425,[0 0 0]); 
        DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
        Screen('Flip',win);
        ProbeStart=GetSecs - expStartTime;
        txt_size = 00;
       
        probe = 8888;
        Results.data((ctr),:)= [ 88 ctr total_events numButtonEvents probe LineX ProbeStart resptime resp txt_size];

        if strcmp(eye_on, 'on')
            Eyelink('Startrecording');
            Eyelink(' message', 'SYNCTIME' ); 
            Eyelink('message','TRIAL_ID %d', probe);
        end
        if strcmp(phys, 'on')
            fprintf(udpA, 'p');
        end
       
            
       while GetSecs < ProbeStart + expStartTime + ProbeDur1

  
            [keyIsDown, secs, keyCode]=KbCheck;
            WaitSecs(0.001);
            pressedKeys = find (keyCode);
     
         if keyIsDown
            
            if pressedKeys == escapeKey
                exitExperimentFlag = 1;
                resp =2;
                break;
             
            elseif keyCode(RightKey)>0
                LineX = LineX + pixelsPerPress; 
   
                     if keyDownFlag == 0
                        resp = 'm';

                            if resp=='m'
                                resp=3;
                            else
                                resp=0;  
                            end                     
   
                        resptime = secs - (ProbeStart+expStartTime);
                        buttonEvents(numButtonEvents+1).resp = resp;
                        buttonEvents(numButtonEvents+1).rt = resptime;
                        numButtonEvents = numButtonEvents +1;
                        ctr = ctr + 1;
                        keyDownFlag = 1;
                        probe=88888;

                        
                if LineX < (xCenter -100)
                    LineX=(xCenter+100);
                elseif LineX > (xCenter + 100)
                    LineX=(xCenter -100);
                end
                            for K = 1:numButtonEvents
                              Results.data((ctr),:)= [888 ctr total_events numButtonEvents probe LineX ProbeStart resptime resp txt_size ];
                            end                           
                      end
            end


                while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                    [keyIsDown, ~, ~]=KbCheck;
                    WaitSecs(0.001);
                end
                
         else %if no key is pressed
            keyDownFlag = 0;
            buttonEvents = [];
         end    
         
               
     DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
     Screen ('DrawText', win,'|', LineX,425,[0 0 0]);
     Screen('Flip', win);
     keyDownFlag = 0;
    
    if strcmp(eye_on, 'on')
     WaitSecs (0.001);
     Eyelink('Message', '!V TRIAL_VAR trialtype probe');
     Eyelink('Message', '!V TRIAL_VAR trialnum %d', 9999);
     Eyelink('Message', '!V TRIAL_VAR stimValue %d', LineX);
     Eyelink('Message', '!V TRIAL_VAR trialstart %4.4f', ProbeStart);
     Eyelink('Message', '!V TRIAL_VAR ctr %d', ctr);
     Eyelink('Stoprecording');      
     Eyelink(' message', 'ENDTIME' ); %mark end of a trial
     Eyelink('Message', 'TRIAL_RESULT 0');    %%%% should this be here?
    end
     
   end %end of while probe loop
        if numButtonEvents == 0
            ctr = ctr+1;
             Results.data((ctr),:)= [ 686 ctr total_events numButtonEvents probe LineX ProbeStart resptime resp txt_size];
        end

        if exitExperimentFlag
            break;
        end

        if numButtonEvents>0
             total_events= numButtonEvents + total_events  
        end
     
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Probe screen 2
       %%
             
        ctr= ctr + 1; %%%%%%%%%%%%%%%%%%%%important
        keyDownFlag = 0;
        numButtonEvents = 0;
        buttonEvents = [];
        pixelsperpress = 150;
        linex = ((xCenter-225) + (150 * randint(1,1,4)));
        total_events = total_events +1;
        
        Screen('Textsize',win,20);
        Screen('TextFont',win,'Arial');
        txt_5=['How confident are you in your answer?' sprintf('\n\n')...
                   '  not at all      somewhat       very        extremely ' ];
        Screen ('DrawText', win,'|', linex,430,[0 0 0]); 
        Screen('Flip',win);    
        DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
        Screen('Flip',win);
       
        ProbeOne=GetSecs - expStartTime;
        txt_size = 00;
        probe = 9999;
        Results.data((ctr),:)= [ 99 ctr total_events numButtonEvents probe linex ProbeOne resptime resp txt_size];

          if strcmp(eye_on, 'on')
            Eyelink('Startrecording');
            Eyelink(' message', 'SYNCTIME' ); 
            Eyelink('message','TRIAL_ID %d', probe);
          end
          
          if strcmp(phys, 'on')
            fprintf(udpA, 'k');
          end
                
             
        while GetSecs < ProbeOne + expStartTime + ProbDur2
     
            [keyIsDown, secs, keyCode]=KbCheck;
            WaitSecs(0.001);
            pressedKeys = find (keyCode);
     
         if keyIsDown
            
            if pressedKeys == escapeKey
                exitExperimentFlag = 1;
                resp =2;
                break;
             
            elseif keyCode(RightKey)>0
                linex = linex + pixelsperpress; 
   
                     if keyDownFlag == 0
                        resp = 'm';

                            if resp=='m'
                                resp=3;
                            else
                                resp=0;  
                            end                     
   
                        resptime = secs - (ProbeOne+expStartTime);
                        buttonEvents(numButtonEvents+1).resp = resp;
                        buttonEvents(numButtonEvents+1).rt = resptime;
                        numButtonEvents = numButtonEvents +1;
                        ctr = ctr + 1;
                        keyDownFlag = 1;
                        probe=99999;

                        
                if linex < (xCenter -225)
                    linex=(xCenter+225);
                elseif linex > (xCenter + 225)
                    linex=(xCenter -225);
                end
                            for K = 1:numButtonEvents
                              Results.data((ctr),:)= [999 ctr total_events numButtonEvents probe linex ProbeOne resptime resp txt_size ];
                            end                           
                      end
            end


                while keyIsDown% make sure a movement only happens when the key is released and then pressed again
                    [keyIsDown, ~, ~]=KbCheck;
                    WaitSecs(0.001);
                end
                
         else %if no key is pressed
            keyDownFlag = 0;
            buttonEvents = [];
         end    
         
               
     DrawFormattedText(win, txt_5, 'center', 'center', [0 0 0],75 ,[],[],1.5);
     Screen ('DrawText', win,'|', linex,430,[0 0 0]);
     Screen('Flip', win);
     keyDownFlag = 0;
    
    if strcmp(eye_on, 'on')
     WaitSecs (0.001);
     Eyelink('Message', '!V TRIAL_VAR trialtype probe');
     Eyelink('Message', '!V TRIAL_VAR trialnum %d', 999);
     Eyelink('Message', '!V TRIAL_VAR stimValue %d', linex);
     Eyelink('Message', '!V TRIAL_VAR trialstart %4.4f', ProbeOne);
     Eyelink('Message', '!V TRIAL_VAR ctr %d', ctr);
     Eyelink('Stoprecording');      
     Eyelink(' message', 'ENDTIME' ); %mark end of a trial
     Eyelink('Message', 'TRIAL_RESULT 0');    %%%% should this be here?
    end
    
  
   end %end of while probe loop
   
        if numButtonEvents == 0
            Results.data((ctr),:)= [ 696 ctr total_events numButtonEvents probe linex ProbeOne resptime resp txt_size];
        end

        if exitExperimentFlag
            break;
        end

             if numButtonEvents>0
                total_events= numButtonEvents + total_events  
             end
    end % command out prbe
    
    


end  
end

%% end of experimnet
Screen('Textsize',win,20);
Screen('TextFont',win,'Arial');
txt_7 = [ ' This is the end of the experiment.' sprintf('\n')...
    ' You can leave the room and get Miriam.'  sprintf('\n')...
       'Thank you for your participation!'];
        
DrawFormattedText(win, txt_7, 'center', 'center', [0 0 0],85 ,[],[],2);
Screen('Flip',win);
if strcmp(phys, 'on')
    fprintf(udpA, 'i');
end

WaitSecs(.5);% this avoids participants accidently pressing too quicklyand moving the experiment on

save(Outputfile,'Results');
end;
%quit event marking
if strcmp(phys, 'on')
    fprintf(udpA,'q')
    fclose(udpA);
end
% finish up: stop recording eye-movements,
% close graphics window, close data file and shut down tracker
if strcmp(eye_on, 'on')
Eyelink(' stoprecording' );
Eyelink(' closefile' );
WaitSecs(1.0);
Eyelink('ReceiveFile', OutputfileEDF);
Eyelink('shutdown');
end
%stop
sca;

