% edited on 01/14/2015
% track the border of the Starting-zone
% put the headstage on each border of the Starting-zone
% beep at the beginning of the tracking

fs_sin=8000;
fs0=800;
t=0 : 1/fs_sin : 0.1;
beep0 = sin(fs0*2*pi*t);

pause(10);
% Starting zone border1
fprintf('Starting zone border 1: ');
sound(beep0,fs_sin);
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x0_StartZone = double(locationArray(end - 1)).*scale_x;
y0_StartZone = (-1)*double(locationArray(end)).*scale_y;
x1_StartZone = x0_StartZone-x_center;
y1_StartZone = y0_StartZone-y_center;
if strcmp(rot,'Clockwise')
    if x1_StartZone>=0
        if x1_StartZone==0 x1_StartZone=0.1; end
        ang1_StartZone=mod(atan(-y1_StartZone./x1_StartZone),2*pi);
    else
        ang1_StartZone=mod(atan(-y1_StartZone./x1_StartZone)+pi,2*pi);
    end
else
    if x1_StartZone>=0
        if x1_StartZone==0 x1_StartZone=0.1; end
        ang1_StartZone=mod(atan(y1_StartZone./x1_StartZone),2*pi);
    else
        ang1_StartZone=mod(atan(y1_StartZone./x1_StartZone)+pi,2*pi);
    end
end

% Starting zone border2
fprintf('Starting zone border 2: ');
beep2=[beep0,zeros(size(beep0)),beep0];
sound(beep2,fs_sin);
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x0_StartZone = double(locationArray(end - 1)).*scale_x;
y0_StartZone = (-1)*double(locationArray(end)).*scale_y;
x2_StartZone = x0_StartZone-x_center;
y2_StartZone = y0_StartZone-y_center;
if strcmp(rot,'Clockwise')
    if x2_StartZone>=0
        if x2_StartZone==0 x2_StartZone=0.1; end
        ang2_StartZone=mod(atan(-y2_StartZone./x2_StartZone),2*pi);
    else
        ang2_StartZone=mod(atan(-y2_StartZone./x2_StartZone)+pi,2*pi);
    end
else
    if x2_StartZone>=0
        if x2_StartZone==0 x2_StartZone=0.1; end
        ang2_StartZone=mod(atan(y2_StartZone./x2_StartZone),2*pi);
    else
        ang2_StartZone=mod(atan(y2_StartZone./x2_StartZone)+pi,2*pi);
    end
end

Ang_StartZone=[ang1_StartZone;ang2_StartZone];

sound(beep0,fs_sin);