function log = afc_response_ud(cfg,Gparams)

t0 = GetSecs;

log = [];
log.button_up         = 'UpArrow';
log.button_down        = 'DownArrow';

Keys = [];

button_up           = KbName(log.button_up);
button_down         = KbName(log.button_down);
button_pause        = KbName('p');
button_exit         = KbName('e');

if (isfield(cfg, 'maxtime'))
	log.maxtime = cfg.maxtime;
else
	log.maxtime = inf;
end


while(1)
    
    current_time = GetSecs - t0;
    
    if (current_time >= log.maxtime)
        log.button = nan;
        log.decision = nan;
        log.rt = nan;
        return;
    end
    
    %% Present button mapping
    Screen('DrawDots', Gparams.pWindow, [(Gparams.ScreenResX - 1)/2, (Gparams.ScreenResY-1)/2], Gparams.FixDotSize, 255, [], 1);
    
    if cfg.button_mapping == 1
        DrawFormattedText(Gparams.pWindow, '<', 'center', 'center', 255, [],[],[],[],[],[0, 420, (Gparams.ScreenResX - 1), (Gparams.ScreenResY - 1)/2]);
        DrawFormattedText(Gparams.pWindow, '>', 'center', 'center', 255, [],[],[],[],[],[0, (Gparams.ScreenResY - 1)/2, (Gparams.ScreenResX - 1), (Gparams.ScreenResY - 1) - 445]);
    elseif cfg.button_mapping == 2
        DrawFormattedText(Gparams.pWindow, '>', 'center', 'center', 255, [],[],[],[],[],[0, 420, (Gparams.ScreenResX - 1), (Gparams.ScreenResY - 1)/2]);
        DrawFormattedText(Gparams.pWindow, '<', 'center', 'center', 255, [],[],[],[],[],[0, (Gparams.ScreenResY - 1)/2, (Gparams.ScreenResX - 1), (Gparams.ScreenResY - 1) - 445]);
    end
    
    Screen('Flip', Gparams.pWindow);
    
    
    oldKeys = Keys;
	[~, ~, keyCode] = KbCheck;
	Keys = find(keyCode, 1);
    
    if(~isempty(Keys))
        
        switch Keys
            case button_up
                log.button = 1; % up
                if cfg.button_mapping == 1
                    log.decision = 0; % left
                else
                    log.decision = 1; % right
                end
                log.rt = GetSecs - t0;
                break;
            case button_down
                log.button = 0; % down
                if cfg.button_mapping == 1
                    log.decision = 1; % right
                else
                    log.decision = 0; % left
                end
                log.rt = GetSecs - t0;
                break;
            case button_pause
                log.button = nan;
                log.decision = nan;
                log.rt = GetSecs - t0;
                DrawFormattedText(Gparams.pWindow, 'De onderzoeker heeft het expimeriment gepauzeerd.\n\n Even wachten a.j.b.', 'center', 'center', 255);
                Screen('Flip', Gparams.pWindow);
                WaitSecs(0.5);
                keyCode = 0;
                while(~ismember(button_pause, find(keyCode)))
                    [~, ~, keyCode] = KbCheck;
                    WaitSecs(0.01);
                end
                break;
            case button_exit
                log.button = nan;
                log.decision = nan;
                log.rt = nan;    
                Screen('CloseAll');
                ShowCursor;
                ListenChar(0);
                Priority(0);
                error('Experiment terminated by user!');                
        end
    end
end

Screen('DrawDots', Gparams.pWindow, [(Gparams.ScreenResX - 1)/2, (Gparams.ScreenResY-1)/2], Gparams.FixDotSize, 255, [], 1);
Screen('Flip', Gparams.pWindow);

if  log.maxtime < inf
    Screen('DrawDots', Gparams.pWindow, [(Gparams.ScreenResX - 1)/2, (Gparams.ScreenResY-1)/2], Gparams.FixDotSize, 255, [], 1);
    Screen('Flip', Gparams.pWindow, t0 + log.maxtime - Gparams.flip_lag);
end


end

