function manga1_v5
%Verao 5

clear;
disp('Ligado');
pause;

global ao;

%****************************************************
ao=analogoutput('nidaq');
addchannel(ao, 0);
ch0=ao.Channel;

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


sampleRate=1000;

set(ao,'SampleRate',sampleRate);
set(ao1,'SampleRate',sampleRate);

set(ao, 'TriggerType', 'Immediate');
set(ao, 'RepeatOutput',0);

set(ao1, 'TriggerType', 'Immediate');
set(ao1, 'RepeatOutput', 0);

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

z=0;
putdata(ao, [z]');
start(ao);

stop(ao);


Vmotor=2.5;
ganho=0.8;
    
    putdata(ao, [Vmotor]');
    start(ao); 
    stop(ao);
    
    
dio_out = digitalio('nidaq', 1);
addline(dio_out, 0:4, 0, 'Out');
putvalue(dio_out, [1 1 1 1 0]);

dio_in = digitalio('nidaq', 1);
addline(dio_in, 0, 1, 'In');

manga_1=1;

% calc_traj;
yy1_xm =47-15;
yy1_xM =59-15;
yy2_xm =47;
yy2_xM =59;

passadeira1=(getvalue(dio_in));
passadeira2=(getvalue(dio_in));
passadeira3=(getvalue(dio_in));
passadeira4=(getvalue(dio_in));

while manga_1==1,
 tic

 passadeira1=test_passadeira(dio_in);

% if passadeira1==1 | passadeira2==1 | passadeira3==1 | passadeira4==1
%     passadeira1
%     passadeira2
%     passadeira3
%     passadeira4
%     z=0;
%     putdata(ao, [z]');
%     start(ao);
%     stop(ao);
%     manga_1=0;
% end

rgb_=vfm('grab',1);
% imshow(rgb_)
rgb_1=(rgb_(:,:,1) >250);
rgb_2=(rgb_(:,:,2) >250);
% rgb_3=(rgb_(:,:,3) >250);
rgb_4 = rgb_1 & rgb_2;

img1=rgb_4(:,1:79);

img2=rgb_4(:,80:160);

[x1 y1]=find(img1);
xx1=max(x1);

[x2 y2]=find(img2);
xx2=max(x2);


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

if xx1>75
    
    passadeira2=test_passadeira(dio_in);

    xx_M1=img1(xx1-10,:);
    [x1_M y1_M]=find(xx_M1);

    [yy1_m]=min(y1_M);
    [yy1_M]=max(y1_M);

   
 %************************
    if (yy1_M>=1 & yy1_m>=1) & length(y1_M)<=15,
        
        Vref=(yy1_M-yy1_xM)*(-ganho/(yy1_xM-1));
        putdata(ao1, [Vref]');
        start(ao1); 
        stop(ao1);
    
    elseif (yy1_M>1 & yy1_m~=[])
        yy1_M=yy1_m+10;
        Vref=(yy1_M-yy1_xM)*(-ganho/(yy1_xM-1));
        putdata(ao1, [Vref]');
        start(ao1); 
        stop(ao1);
    
    
    elseif (yy1_M==[] & yy1_m==[])
        %******************************* 2ºlinha ******************
        if xx2>75
            
           passadeira3=test_passadeira(dio_in)           
           xx_M2=img2(xx2-10,:);
           [x2_M y2_M]=find(xx_M2);

           [yy2_m]=min(y2_M);
           [yy2_M]=max(y2_M);
           
           if yy2_m>=1 & yy2_M>=1 & length(y2_M)<=15,
               Vref=(yy2_m-yy2_xm)*(-ganho/(yy2_xm-1));
               putdata(ao1, [Vref]');
               start(ao1); 
               stop(ao1);
           
           elseif yy2_m>=1
               yy2_m=yy2_M-10;
               Vref=(yy2_m-yy2_xm)*(-ganho/(yy2_xm-1));
               putdata(ao1, [Vref]');
               start(ao1); 
               stop(ao1);
           end
        end   
           
        
    end
%**************************    
    
    
end % do primeiro if!

toc
passadeira4=test_passadeira(dio_in); 
end % do ciclo while

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

%   ENCERRAR COM OS CANAIS A ZERO.



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

putvalue(dio_out, [0 0 0 0 0]);
stop(dio_out);
disp('concluido');

return
%end

%==================================================

function emcima =test_passadeira(io_add)
%Subfuncao auxiliar

global ao;

emcima = getvalue(io_add); 

if emcima == 1
%     z=0;
%     putdata(ao, [z]');
%     start(ao);
%     stop(ao);
    disp('+')
end


end



