clear all
close all

mybox = CedrusResponseBox('Open', 'COM3');

boxinfo = CedrusResponseBox('GetDeviceInfo', mybox);

% Trial loop:
ntrials=2;
figure
for j=1:ntrials
    % Clear all pending button events (e.g., button release from previous trial):
    CedrusResponseBox('FlushEvents', mybox);

    % Wait for subject to initiate a trial by pressing any key:
    'press any key to begin trial'
    evt = CedrusResponseBox('WaitButtonPress', mybox);
    
    % Make sure subject has released key before starting the trial:
    buttons = 1;
    while any(buttons)
        buttons = CedrusResponseBox('FlushEvents', mybox);
    end

    % Begin trial
    evt=[];
    tonset = GetSecs;
    while isempty(evt)
    % Present the stimulus until the subject presses a button:
        imagesc(rand(10,20));
        drawnow
        % see if subject pressed a button while we were doing that:
        evt = CedrusResponseBox('GetButtons', mybox);
    end       
    evt
    % evt.action ==0 -> Button release, ==1 --> Button press.
    % evt.button == Button number of pressed button.
    % evt.rawtime == Time of reaction time timer.
    presstime = evt.ptbtime; % Time of button press in PTB's time system.

    reactionTime(j) = presstime - tonset;
    fprintf('subject pressed button %s\n',evt.buttonID)
    
end

CedrusResponseBox('Close', mybox);

