clear;
clc;

global epic;
    epic=ePicKernel;
global ControllerState;
    ControllerState = 0;
display('Lets gooooo');

epic=connect(epic,'COM3');
display('e-puck succesfully connected');

epic=activate(epic,'speed');
epic=activate(epic,'proxi');
epic=deactivate(epic,'light');
epic=deactivate(epic,'image');
epic=deactivate(epic,'accel');
epic=deactivate(epic,'micro');
epic=deactivate(epic,'pos');
epic=deactivate(epic,'odom');


if get(epic,'connectionState') == 1
   display('Successfully Connected')	
   [val,up]=get(epic,'speed');
   
   
   while (1)
       epic=set(epic,'speed',[900 900]);
       [prox,prox_up]=get(epic,'proxi');
        
        if (prox(1)||prox(8)>900) && (prox(2)||prox(3)>prox(6)||prox(7))
           [prox,prox_up]=get(epic,'proxi');
            epic=set(epic,'speed',[900 100]);
            display('Move to left')
            
        elseif (prox(1)||prox(8)>900) && (prox(2)||prox(3)<prox(6)||prox(7))
            epic=set(epic,'speed',[100 900]);
            display('Move to Right')
            [prox,prox_up]=get(epic,'proxi');
        elseif ((prox(1)&& prox (2) && prox (3) && prox(6)&& prox(7) && prox(8))>900)
            epic=set(epic,'speed',[-500 -500]);
            display('retreat')
            [prox,prox_up]=get(epic,'proxi');
        elseif prox(1:8)>700
            epic=set(epic,'speed',[0 0]);
            display('I was surrounded')
            [prox,prox_up]=get(epic,'proxi');
        else
            display('error')
        end
		epic=update(epic);
    end

    epic=set(epic,'speed',[0 0]);
    epic=disconnect(epic);
    display('There are no connection')
    display('Thank you')
else
    display('Connection Failed')
end