function mv = trackHand(nPos)
%Function to track the hand nPos times
%Inputs: 
%nPos: Number of times to track the hand
%outputs: movie object of the last 8 frames captured for debugging purposes
%Results are automatically  displayed in a new figure and displayed on the command line

%Open up a connection to the serial port

serialConn = serial('COM1');

set(serialConn,'BaudRate',9600,'DataBits',8,'Parity','none','StopBits',1);

fopen(serialConn);
fprintf(serialConn,'%u',0);
%Open up a connection to the DV cam
dvIn = videoinput('winvideo',1,'RGB24_720x576');
%while more postions are still required

for k =1:nPos,
    tic;
    % Capture required frames
    %mv(1) = im2frame(medfilt2(getsnapshot(dvIn)),[5 5]);
    for i = 0:3,
        %Turn on the led
        fprintf(serialConn,'%u',i);
        %Capture the frame
        mv(i+1) = im2frame(getsnapshot(dvIn));
        %Turn off the led
        fprintf(serialConn,'%u',0);
    end
    mv(5) = im2frame(getsnapshot(dvIn));
    
    for i = 4:6,
        %Turn on the led
        fprintf(serialConn,'%u',10 - i);
        %Capture the frame
        mv(i+2) = im2frame(getsnapshot(dvIn));
        %Turn off the led
        fprintf(serialConn,'%u',0);
    end
    
     %Run glove detect
     [handPosX, handPosY, pitch] = gloveDetect(mv);
     toc;
end
%close DV connection
delete(dvIn);

%Close Serial connection
fclose(serialConn);
delete(serialConn);
