nsession=1;
ind_rewardloc = Ind_rewardloc(nsession);
n_trial=1;
while 1
    %% Test trials
%     sound(sound_beep_start,sound_beep_fs);  % make a sound when a test trial starts
    sound(beep0,fs_sin);
    
    pause(sample_time);
    get_current_loc_v1  % get the current location and angle from on-line video  
    ts_test_start(n_trial,1)=time0;
    sign_enter=0;  % 1 = the rat was in reward zone
    sign_leave=0; % 1 = has stopped at one location ; 0 = not stopped at any location
    ind_loc=0;
    ind_loc_max=0;
    time_stay_reward=0;
    ang_ontrack=0;
    ang_ontrack_end=0;
    locationArray_end=[0,0];
    while 1
        pause(sample_time);
        get_current_loc_v1  % get the current location and angle from on-line video
        ang_ontrack=mod(ang0-Ang_StartZone_center,2*pi);
        vel_ontrack=(ang_ontrack-ang_ontrack_end)/sample_time;
        if ind_loc_max>0 && (ang_ontrack>Ang_StartZone_arrive_ontrack || ang_ontrack<Ang_StartZone_depart_ontrack)
            % if the rat is back entering the animal box, sample tests end
%             sound(sound_beep_start,sound_beep_fs);
            break
        end
        if sign_leave==0
            % this rat hasn't stopped at either locations yet
            
            ind_loc_temp=find(ang_ontrack>Ang_RewardLoc_ontrack_zone_narrow(:,1) & ang_ontrack<Ang_RewardLoc_ontrack_zone_narrow(:,2));
            if ~isempty(ind_loc_temp)
                % rat is walking in location zone, and reaching this loc
                sign_enter=1;
                if ind_loc_temp > ind_loc_max
                    ind_loc_max=ind_loc_temp;
                    time_stay_reward=0;
                elseif ind_loc_temp >= ind_loc_max-1 && dist0 <= R_inner_RewardLoc(ind_loc_temp)
                    time_stay_reward=time_stay_reward+sample_time;
                else
                    time_stay_reward=0;
                    sign_enter=0;
                end
            end
            
            if time_stay_reward >= time_stay_test
                % if the rat has stopped at reward location for "time_stay_test"
                if ind_loc_max==ind_rewardloc
                    % correct reward location
                    sound(tone_correct,fs_tone_correct);
                    sign_correct0=1;
                elseif ind_loc_max~=ind_rewardloc
                    % first time get to incorrect reward location
                    if abs(ind_loc_max-ind_rewardloc) <= 2
                        sound(tone_incorrect,fs_tone_incorrect);
                        sign_correct0=0;
                    else
                        % if the rat stopped at a location very far away from the reward location,
                        % we don't mark here as incorrect location
                        time_stay_reward=0;
                        ang_ontrack_end=ang_ontrack;
                        continue
                    end
                end
                ind_rewardloc_test(n_trial,1)=ind_loc_max;
                ang_test_reward_ontrack(n_trial,1)=Ang_RewardLoc_ontrack(ind_loc_max);
                ts_test_reward(n_trial,1)=time0;
                sign_correct_test(n_trial,1)=sign_correct0;
                sign_enter=0;
                sign_leave=1;
            end
        end
        ang_ontrack_end=ang_ontrack;
    end
    if sign_leave==0  % if didn't stopped at any location
        ind_rewardloc_test(n_trial,1)=nan;
        ang_test_reward_ontrack(n_trial,1)=nan;
        ts_test_reward(n_trial,1)=nan;
        sign_correct_test(n_trial,1)=nan;
    end
    ts_test_end(n_trial,1)=time0;
    
end