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
    sign_run=0; % 1 = has finished running this lap; 0 = hasn't ran in this lap
    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_end>Ang_StartZone_arrive_ontrack-0.5 && ang_ontrack_end<=Ang_StartZone_arrive_ontrack && ang_ontrack>=Ang_StartZone_arrive_ontrack && ang_ontrack<Ang_StartZone_arrive_ontrack+0.5
            % the rat just finished this lap
            sign_run=1;
        end
        if sign_run==1 && ang_ontrack_end>2*pi-0.5 && ang_ontrack_end<=2*pi && ang_ontrack>=0 && ang_ontrack<0.5
            % 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 && dist0 <= R_inner_RewardLoc(ind_loc)
                    time_stay_reward=time_stay_reward+sample_time;
                else
                    time_stay_reward=0;
                end
                ind_loc=ind_loc_temp;
                if ind_loc_max<ind_loc
                    ind_loc_max=ind_loc;
                end
            else % the rat is out of any location zone
                if sign_enter==1
                    % rat just move out from this location zone
                    if dist0 <= R_inner_RewardLoc(ind_loc) && ang_ontrack>Ang_RewardLoc_ontrack_zone_wide(ind_loc,1) && ang_ontrack<Ang_RewardLoc_ontrack_zone_wide(ind_loc,2)
                        % this rat is turning towards to center to get food
                        time_stay_reward=time_stay_reward+sample_time;
                    else
                        sign_enter=0;
                        time_stay_reward=0;
                        ind_loc=0;
                    end
                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_loc
                    % if the rat is still running forward
                    if ind_loc==ind_rewardloc
                        % first time get to correct reward location
                        sound(tone_correct,fs_tone_correct);
                        sign_correct0=1;
                    elseif ind_loc~=ind_rewardloc
                        % first time get to incorrect reward location
                        sound(tone_incorrect,fs_tone_incorrect);
                        sign_correct0=0;
                    end
                    ind_rewardloc_test(n_trial,1)=ind_loc;
                    ang_test_reward_ontrack(n_trial,1)=Ang_RewardLoc_ontrack(ind_loc);
                    ts_test_reward(n_trial,1)=time0;
                    sign_correct_test(n_trial,1)=sign_correct0;
                    sign_enter=0;
                    sign_leave=1;
                else
                    % ind_loc_max>ind_loc_old, the rat turned back
                    if ind_loc_max>ind_rewardloc
                        % the rat has already passed by the reward location
                        time_stay_reward=0;
                        sign_enter=0;
                        sign_leave=1;
                    else
                        % the rat hasn't been to the reward location, but he turned back to some earlier locations
                        % he still has chance to get the reward
                        time_stay_reward=0;
                        sign_enter=0;
                        sign_leave=0;
                    end
                end
            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