function phua(flag)

    close all
    delete(instrfind);
    H=initCom;

    globalVar;
    N=200;
    Nsensors=4;
    Nsamples=1000;
    hPlot=zeros(Nsensors+1);
    nSampPlot=50;
    y=zeros(Nsensors,nSampPlot);

    middleX=7;
    totalX=14;
    middleY=4;
    totalY=8;

    val=zeros(Nsensors,1);
    calibSensor=zeros(Nsensors,1);
    weight=zeros(1,5);
    
    nMoves=0;
    posArray=zeros(3,N);
    posArraySaved=zeros(3,Nsamples);
    t=zeros(1,N);
    tSaved=zeros(nMoves,Nsamples);
    timeAdjust=zeros(1,Nsamples);
    valSensor=zeros(N,4);
    calibSensor=zeros(4,1);
    
    if(flag)
        initMsg = [hex2dec('FF') hex2dec('00') hex2dec('00') hex2dec('FF') hex2dec('00') hex2dec('00') hex2dec('FF')];
        fwrite(H,initMsg,'uint8');
    end
        
        displayOptions;         %%%% DISPLAY OPTIONS %%%%
        
    while (1)
        disp(' ');
        disp('                                     Display Options Again - 55');
        opt=input('Option: ');
    
        switch opt
            case 55
                displayOptions;
                
            case 90
                sendServo(H,TESTAR,123,0,0,0,0,0);
                [command,return1] = readServo(H);
                disp('Valor dado pelo Matlab: 123');
                disp(['Valor que a PIC devolveu: ', num2str(return1)]);
            
            case 0
                addr = input('Address (Dec): ');
                data=readEeprom(H,addr);
                disp(['Data (hex): ', num2str(dec2hex(data))]);
                
            case -1
                disp('Address  Data(hex)');
                for addr=0:6
                    data=readEeprom(H,addr);
                    disp([ '  0x0', num2str(addr), '   ', num2str(dec2hex(data))]);
                end
              

            case 1
                addr = input('Address (Dec): ');
                data = input('Data (Dec): ');
                writeEeprom(H,addr,data);

            case 2
                addr = input('Address (Dec): ');
                data=readMem(H,addr);
                disp(['Data (hex): ', num2str(dec2hex(data))]);

            case 3
                addr = input('Address (Dec): ');
                data = input('Data (Dec): ');
                writeMem(H,addr,data);

            case 4
                pos = readPos(H);
                disp(['Position: ', num2str(pos), 'º']);

            case 5
                pos = input('Position [-90 ; 90]º: ');
                setPosAll(H,pos);

            case 6
                [version,id] = readVersionId(H);
                disp(['Version: ', num2str(version)]);  
                disp(['ID: ', num2str(id)]);                 

            case 7
                [pw,v] = readPwV(H);
                disp(['Pulsewidth: ', num2str(pw)]);  
                disp(['Voltage: ', num2str(v)]);  

            case 8
                id = input('ID: ');
                speed = input('Speed [0 ; 100]%: ');
                pos = setSpeedReadPos(H,id,speed);
                disp(['Position: ', num2str(pos), 'º']);

            case 9
                set = input('Control Parameter Set (1/2/3): ');
                ctrlParamSet(H,set);

            case 10
                set = input('Go/Stop (1/0): ');
                goStop(H,set);

            case 11
                release(H);
                
            case 12
                id = input('ID: ');
                pos = input('Position [-90 ; 90]º: ');
                setPosId(H,id,pos);
                
            case 13
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);

            case 14
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);

                Pos0 = input('Motor 0 Position [-90 +90]º: ');
                Pos1 = input('Motor 1 Position [-90 +90]º: ');
                Pos2 = input('Motor 2 Position [-90 +90]º: ');
                setPosId(H,0,Pos0);
                setPosId(H,1,Pos1);
                setPosId(H,2,Pos2);
                
%%%%%%%%%%%% LEG %%%%%%%%%%%%%            
            case 20
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
                move(H,-60,10,20);
                pause(1.5);
                move(H,-60,10,-90);
                pause(1.5);
                move(H,80,10,-90);
                pause(1.5);
                move(H,80,70,0);
                pause(2);
                move(H,80,-40,0);
                pause(1.5);
                move(H,80,10,0);     % Posição final
                pause(1.5);
                
            case 21
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
                for i=1:2
                    move(H,-60,10,20);
                    pause(3);                
                    move(H,20,10,-80);
                    pause(3);
                    move(H,80,10,-80);
                    pause(3);
                    move(H,80,10,0);
                    pause(2);
                end
                
            case 22
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
                move(H,-60,10,20);
                pause(2);                
                move(H,80,10,-90);
                pause(2);
                move(H,80,10,0);
                pause(2);
                
%%%%%%%%%%%% Arm %%%%%%%%%%%%%            
            case 30
                %maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,30,0,0,0,0,0);

                move(H,0,85,-90);
                pause(2);
                move(H,0,90,-35);
                pause(2);
                move(H,0,-90,-35);
                pause(2);
                move(H,0,85,45);    % Posição final
                pause(2);
                
                for i=0:5
                    move(H,75,90,-35);
                    pause(0.3);                
                    move(H,75,60,-35);
                    pause(0.3);
                end
                move(H,0,85,45);    % Posição final
                pause(2);
                
                sendServo(H,23,50,0,0,0,0,0);   % repete com velocidade 50

                move(H,0,85,-90);
                pause(2);
                move(H,0,90,-35);
                pause(2);
                move(H,0,-90,-35);
                pause(2);
                move(H,0,85,45);    % Posição final
                pause(2);
                
                for i=0:5
                    move(H,75,90,-35);
                    pause(0.3);                
                    move(H,75,60,-35);
                    pause(0.3);
                end
                move(H,0,85,45);    % Posição final
                pause(2);
                
%%%%%%%%%%%% Body %%%%%%%%%%%%%            
            case 40
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
%                 move(H,0,-50,0);    % Motor 1
%                 pause(4);
%                 move(H,0,20,0);
%                 pause(4);
%                 move(H,0,0,10);     % Motor 2
%                 pause(4);
%                 move(H,0,0,-10);
%                 pause(4);

%                 move(H,0,-50,0);    % Motor 1
%                 pause(4);
%                 move(H,0,10,0);
%                 pause(4);
%                 move(H,0,0,0);      % Posição final
%                 pause(4);
                move(H,0,0,20);     % Motor 2
                pause(4);
                move(H,0,0,-20);
                pause(4);
                move(H,0,0,0);      % Posição final
                pause(4);
                
            case 41
                maxSpeed = input('Maximum Speed: ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
                for i=1:3
                    move(H,35,-50,0);
                    pause(4);
                    move(H,0,0,0);
                    pause(4);
                    move(H,-35,-50,0);
                    pause(4);
                    move(H,0,0,0);
                    pause(4);
                end
                
%%%%%%%%%%%% 2 Legs %%%%%%%%%%%%%            
            case 25
                maxSpeed = input('Maximum Speed (30): ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
              % move2(H,anca1,pé1,joelho1,anca2,pé2,joelho2);
                for i=1:2
                    move2(H,-45,0,0,-60,-80,80);
                    pause(2);
                    move2(H,60,80,-90,45,0,0);
                    pause(2);
                end
                move2(H,-10,0,-90,20,10,80);    % Posição final
                pause(1);

            case 26
                maxSpeed = input('Maximum Speed (60): ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
              % move2(H,anca1,pé1,joelho1,anca2,pé2,joelho2);
                for i=1:2
                    move2(H,60,80,45,-80,-80,-45);
                    pause(2);
                    move2(H,-10,-80,-90,20,80,80);
                    pause(2);
                end
                move2(H,-10,0,-90,20,10,80);    % Posição final
                pause(1);

%%%%%%%%%%%% Legs and Arms %%%%%%%%%%%%%            
            case 35
                maxSpeed = input('Maximum Speed (30): ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
                % move4(H,  perna1,    perna2,    braço1,    braço2);
                move4(H,  -10,90,-90,  20,-90,80,  0,0,-45,  0,0,45);
                pause(2);
                move4(H,  -10,0,-90,  20,10,80,  -90,-90,45,  90,85,-45);
                pause(2);
                move4(H,  -10,0,0,  20,10,-10,  0,0,-45,  0,0,45);
                pause(2);
                move4(H,  -10,0,-90,  20,10,80,  -5,-85,-45,  0,85,45);
                pause(1);       % Posição final
                
            case 36
                maxSpeed = input('Maximum Speed (30): ');
                sendServo(H,23,maxSpeed,0,0,0,0,0);
                
                for i=1:2
                    move4(H,  -45,0,0,  -60,-80,80,  -45,-85,0,  0,85,90);
                    pause(2);
                    move4(H,  60,80,-90,  45,0,0,  -5,-85,-90,  45,85,0);
                    pause(2);
                end
                move4(H,  -10,0,-90,  20,10,80,  -5,-85,-45,  0,85,45);    % Posição final
                pause(1);
                
            otherwise
                break;

        end
    end
    
killCom(H);
end