-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex12_mouse_log_responses.m
185 lines (146 loc) · 8.31 KB
/
ex12_mouse_log_responses.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%% Example 12: Mouse responses
% We are going to do a similar thing to example 10, only here rectangles
% can be either on the left or right of the screen and can be either red or
% green. We will use the mouse to collect responses.
% Clear the workspace and close screens
sca; % closes all screens
clear all; % clear all variables
HideCursor; % hides the mouse cursor
input ('start>>>','s'); % prints to command window
try
%% SETUP TRIALS %%
% First we are going to setup our trial structure.
% We will be presenting rectangles on either the left or right of the
% screen, and we want equal numbers of each but in a shuffled order
nTrials = 4; % how many trials there will be
% we want equal numbers of green and red when it is on the left, and
% same on the right
trial = []; % make a struct array, we will put all the trial info and responses in here
trial.position = repmat(1:2,1,nTrials/2); % fill a vector with alternating 1s and 2s, representing the left and right side of the screen respectively
trial.colour = [ones(1,nTrials/2) repmat(2,1,nTrials/2)]; % fill the first half of the vector with 1s (red) and the second half with 2s (green)
trialParams = [ trial.position; trial.colour ]; %combine them so we can shuffle
trialParams = trialParams(:,randperm(size(trialParams,2))); %shuffle columnwise so that we preserve balance
% lets put it back in the struct.. we would normally condense this all
% down but I'm leaving it as multiple steps so it's easier to see
% what's going on
trial.position = trialParams(1,:);
trial.colour = trialParams(2,:);
%% SETUP SCREEN %%
PsychDefaultSetup(2); % Executes the AssertOpenGL command & KbName('UnifyKeyNames') & normalizes colour range
Screen('Preference', 'SkipSyncTests', 2); % DO NOT KEEP THIS IN EXPERIMENTAL SCRIPTS!
% Setup screens
getScreens = Screen('Screens'); % Gets the screen numbers, typically 0 = primary and 1 = external
chosenScreen = max(getScreens); % Chose which screen to display on (here we chose the external)
rect = []; % Full screen
% Get luminance values
white = WhiteIndex(chosenScreen); % 255
black = BlackIndex(chosenScreen); % 0
grey = white/2;
% Open a psychtoolbox screen
[w, scr_rect] = PsychImaging('OpenWindow',chosenScreen,black,rect); % here scr_rect gives us the size of the screen in pixels
[centerX, centerY] = RectCenter(scr_rect); % get the coordinates of the center of the screen
% Get flip and refresh rates
ifi = Screen('GetFlipInterval', w); % the inter-frame interval (minimum time between two frames)
hertz = FrameRate(w); % check the refresh rate of the screen
waitframes = 1; %how many frames to wait before flipping
%set the font size
Screen('TextSize',w,40);
%% SETUP STIMS %%
% We will define our 4 responses boxes, 2 on each side and of each
% colour. We will shift them on the y axis.
boxColours = [255 0 0; 255 0 0; 0 255 0; 0 255 0]; %define the colours for our 4 boxes
boxPositions = [centerX-100 centerY-100 centerX-50 centerY-50;... %top left box
centerX+50 centerY-100 centerX+100 centerY-50;... %top right box
centerX-100 centerY+50 centerX-50 centerY+100;... %bottom left box
centerX+50 centerY+50 centerX+100 centerY+100]; %bottom right box
%% BEGIN EXPERIMENT %%
DrawFormattedText(w,'Press any key to start','center','center',white); %draw some text
Screen('Flip',w);
KbWait; %wait for any key press to begin trial loop
for n = 1:nTrials
%____ our inter-trial interval ____%
Screen('Flip',w); %lets just show a blank screen for a second
WaitSecs(1); %our inter-trial interval
%____ get position ____%
if trial.position(n) == 1 % if this is a LEFT trial
rectPosition = [centerX-400 centerY-25 centerX-350 centerY+25]; %rect coordinates
elseif trial.position(n) == 2 % if this is a RIGHT trial
rectPosition = [centerX+350 centerY-25 centerX+400 centerY+25]; %rect coordinates
end
%____ get colour ____%
if trial.colour(n) == 1 % if it is a red box
rectColour = [255 0 0];
elseif trial.colour(n) == 2 % if it is a green box
rectColour = [0 255 0];
end
%____ present the stimulus ____%
% now lets present the rectangle for 500ms, using the colour and
% position we chose above
Screen('FillRect',w,rectColour,rectPosition); % draw the rect
vbl = Screen('Flip',w);
WaitSecs(.5); %leave it on the screen for 500ms
Screen('Flip',w); %flip back to the blank screen
WaitSecs(1); %wait for 1 second
%% GET RESPONSES %%
resp=0; mouseMove = 0; %we will update this in the loop
while resp==0
%____ track mouse ____%
%for the first frame, position mouse in the center of the screen
if mouseMove == 0
SetMouse(centerX, centerY, w); %put the mouse in the center
Screen('DrawDots',w, [centerX centerY],10,white,[],2); %draw a dot in the center
responseStart = Screen('Flip',w); %first flip to sync to retrace and get timestamp
mouseMove = 1; %so that we don't try to put it back in the center after the first flip
end
[mx, my, buttons] = GetMouse(w); %Get the current coordinates of the mouse on the screen
if mouseMove == 1 %redraw the mouse cursor to follow movement (but not on the first frame)
Screen('DrawDots',w, [mx my],10,white,[],2); %draw a dot in the coords of the mouse
end
Screen('FillRect',w,boxColours',boxPositions'); % draw all the boxes again
%check if the mouse inside any of the rectangles
insideBox(1) = IsInRect(mx, my, boxPositions(1,:)); % is it inside the top left box?
insideBox(2) = IsInRect(mx, my, boxPositions(2,:)); % top right box?
insideBox(3) = IsInRect(mx, my, boxPositions(3,:)); % bottom left box?
insideBox(4) = IsInRect(mx, my, boxPositions(4,:)); % bottom right box?
if insideBox(1)
Screen('FrameRect',w,white,boxPositions(1,:),3); %draw a frame around the box
if any(buttons) %if there is a mouse click
trial.respPosition(n) = 1; %record press as left
trial.respColour(n) = 1; %record press as red
trial.RT(n) = GetSecs-responseStart; %reaction time
resp = 1; %exit loop
end
elseif insideBox(2)
Screen('FrameRect',w,white,boxPositions(2,:),3); %draw a frame around the box
if any(buttons) %if there is a mouse click
trial.respPosition(n) = 2; %record press as right
trial.respColour(n) = 1; %record press as red
trial.RT(n) = GetSecs-responseStart; %reaction time
resp = 1; %exit loop
end
elseif insideBox(3)
Screen('FrameRect',w,white,boxPositions(3,:),3); %draw a frame around the box
if any(buttons) %if there is a mouse click
trial.respPosition(n) = 1; %record press as left
trial.respColour(n) = 2; %record press as green
trial.RT(n) = GetSecs-responseStart; %reaction time
resp = 1; %exit loop
end
elseif insideBox(4)
Screen('FrameRect',w,white,boxPositions(4,:),3); %draw a frame around the box
if any(buttons) %if there is a mouse click
trial.respPosition(n) = 2; %record press as right
trial.respColour(n) = 2; %record press as green
trial.RT(n) = GetSecs-responseStart; %reaction time
resp = 1; %exit loop
end
end
vbl = Screen('Flip',w, vbl+ (waitframes - .05) * ifi);
end
end
sca;
catch
sca;
ShowCursor;
psychrethrow(psychlasterror);
end