function tracking(obj,event)
if obj.FramesAvailable > 30 % a second behind!
   flushdata(obj);
end
Mov=getdata(obj,2); %get 7 frames from buffer
p=max(max(Mov(:,:,1))); %finds the maximum intensity value of the 1st frame in Mov
[r1,c1]=find(Mov(:,:,1)==p); % gets the index value of max intensity pt but note index is the (y,x)coordinate not (x,y)
R1=median(r1); % median is taken to try to remove the adjescent values in the case where more than  one value is got
C1=median(c1);
n  = evalin('base', 'n');
PATH  = evalin('base', 'PATH');
    d=(abs(C1-(PATH(:,1,1)))+abs(R1-(PATH(:,2,1)))); %find the distance of each point from the track pt
 serialOne  = evalin('base', 'serialOne');  
dmin=min(d); %the minimum distance
indx=find(d==dmin); %find the index of dmin in the d matrix
Xc=PATH(indx,1,n); %current x,y path coordinate
Yc=PATH(indx,2,n);
indxn=indx+1;
if indxn >= 21
    indxn=20;
    fprintf (serialOne, '%c', 'Z')
end
    
Xn=PATH(indxn,1,n); %next x,y path coordinate
Yn=PATH(indxn,2,n);
ddist=sqrt(((C1-Xc)^2)+((R1-Yc)^2)); %distance between the track and current path coordinate
adist=sqrt(((Xn-Xc)^2)+((Yn-Yc)^2)); %distance between the path coordinates
slope=(ddist/adist);
C = [Xn - C1, Yn - R1, 0]';
A = [Xn-Xc, Yn-Yc, 0]';

t = cross(A,C);

theta=atand(slope);
%theta = atan2(adist, ddist);
cdist=sqrt(((Xn-C1)^2)+((Yn-R1)^2)); %distance between track point and next point
%Y21=((cdist)/(sqrt(((slope^2)+1)/(slope^2))))+R1;
%Y22=-((cdist)/(sqrt(((slope^2)+1)/(slope^2))))+R1;
 
%if t(3)<0 %&& theta <= 30
 %      fprintf (serialOne, '%c', 'I');
%elseif t(3)==0 
 %  fprintf (serialOne, '%c', 'U');
 %elseif t(3)>0 && theta > 30
  % fprintf (serialOne, '%c', 'H');
%elseif t(3)>0 && theta <= 30
 %  fprintf (serialOne, '%c', 'Y');
%end
%fprintf (serialOne, '%c', 'send')
C1
R1
Xc
Yc
Xn
Yn
%ddist
%adist
%slope
theta
%cdist
%Y21
%Y22
t
%send



    