clear;
close all;
%******************************************************

ao1=analogoutput('nidaq');
addchannel(ao1, 1);
ch1=ao1.Channel;

sampleRate=1000;
set(ao1,'SampleRate',sampleRate);

set(ao1, 'TriggerType', 'Immediate');
set(ao1, 'RepeatOutput', 1);
    
dio = digitalio('nidaq', 1);
addline(dio, 0:4, 0, 'Out');

%*******************************************************


rgb_=vfm('grab',1);
imshow(rgb_)
rgb_1=(rgb_(:,:,2) >200);
figure
imshow(rgb_1)
[x y]=find(rgb_1);

xx1=max(x);
xx2=min(x);

xx_1=rgb_1(xx1,:);
xx_2=rgb_1(xx2,:);

[x_1 y_1]=find(xx_1);
[x_2 y_2]=find(xx_2);

[yy1]=max(y_1);

[yy2]=max(y_2);

coord=[xx1 yy1; xx2 yy2]

dist_y=yy2-yy1
dist_x=xx1-xx2

ang=atan2(dist_y,dist_x);
ang=(ang*180)/pi

Vref=-(ang*3)/85


%***************************************************


   
 
      putdata(ao1, [Vref]');
      start(ao1); 
      stop(ao1);  
     putvalue(dio, [1 0 0 0 0]);


%---------------------------------------

%   ENCERRAR COM OS CANAIS A ZERO.

putvalue(dio, [0 0 0 0 0]);
stop(dio);

z1=0;
putdata(ao1, [z1]');
start(ao1);
stop(ao1);

