function [new_agent, keepTurning, lastDistanceStraight, lastDistanceRight,...
    lastDistanceLeft, test] = MultipleIRControl( t, dt, agent, coords, rectangles, numberOfRectangles, ...
    target, keepTurning, lastDistanceStraight, lastDistanceRight, lastDistanceLeft, uMax, test)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
    ignoreDistanceStraight = 2/uMax;
    ignoreDistanceSides = 3/uMax;
    noise = 6;
    
    xLeft = coords(1, 1);
    yLeft = coords(1, 2);
    thetaLeft = coords(1, 3);
    xRight = coords(3, 1);
    yRight = coords(3, 2);
    thetaRight = coords(3, 3);
    
    %get agent's infor
    x = agent(t-1, 1);
    y = agent(t-1, 2);
    v = agent(t-1, 3);
    theta1 = agent(t-1, 4);   
    u = agent(t-1, 5);  
    
    %calculate new info except curvature
    theta2 = theta1 + u*v*dt;
    agent(t, 1) = x + v*dt*cos(theta1);
    agent(t, 2) = y + v*dt*sin(theta1);
	agent(t, 3) = v;
	agent(t, 4) = theta2;
    
    %calculate theta to target
    targetx = target(1);
    targety = target(2);
    thetaToTarget = atan((targety-y)/(targetx-x)) - theta1;
    if (targetx - x) < 0
        thetaToTarget = thetaToTarget + pi;
    end
    thetaToTarget = mod(thetaToTarget, 2*pi);
    %stop if very near target
    if (abs(x - targetx) < 0.3 && abs(y - targety) < 0.3)
        agent(t, 3) = 0;
    end
        
    %find distances to barriers, straight, right and left
    distanceStraightWithoutNoise = IRSensorAll(coords, rectangles, numberOfRectangles, ...
        x, y, theta1);
    distanceRightWithoutNoise = IRSensorRight45All(coords, rectangles, numberOfRectangles,...
        xRight, yRight, thetaRight);
    distanceLeftWithoutNoise = IRSensorLeft45All(coords, rectangles, numberOfRectangles,...
        xLeft, yLeft, thetaLeft);

    %find distances, taking into account noise
    readingStraight = (7600/distanceStraightWithoutNoise) -noise/2 + noise*rand();
    distanceStraight = (7600/readingStraight);
    readingLeft = (7600/distanceLeftWithoutNoise) - noise/2 + noise*rand();
    distanceLeft = (7600/readingLeft);
    readingRight = (7600/distanceRightWithoutNoise) - noise/2 + noise*rand();
    distanceRight = (7600/readingRight);
    
    
    %if keep turning is on, see if should be turned off
    if (keepTurning == 1 && (distanceRight > 0 && distanceRight <= ignoreDistanceSides)&&...
        (distanceStraight < 0 || distanceStraight>ignoreDistanceStraight))
        keepTurning = 0;
    end
    if (keepTurning == -1 && (distanceLeft > 0 && distanceLeft <= ignoreDistanceSides) &&...
        (distanceStraight < 0 || distanceStraight>ignoreDistanceStraight))
        keepTurning = 0;
    end
     
    
    %CALCULATING CURVATURE:
    %if keep turning is on, keep turning with curvature as before
    if keepTurning ~= 0
        agent(t, 5) = agent(t-1, 5);
        
    %if very close on either side, turn (supercedes all other control)
    elseif ((distanceLeft > 0 && distanceLeft < ignoreDistanceSides) || ...
            (distanceRight > 0 && distanceRight < ignoreDistanceSides))
        %if close on either side, turn away from whichever side is closest
        if (distanceLeft < distanceRight)
            agent(t, 5) = -uMax;
        else
            agent(t, 5) = uMax;
        end
        
        
%     elseif (distanceLeft > 0 && distanceLeft < ignoreDistance)
%         agent(t, 5) = -uMax;
%         
%     elseif (distanceRight > 0 && distanceRight < ignoreDistance)
%         agent(t, 5) = uMax;
        
    %if pretty much staight on to target...
    elseif (thetaToTarget < pi/20 || thetaToTarget > 2*pi - pi/20)
        %if nothing is in the way in front...
        if (distanceStraight < 0 || distanceStraight>ignoreDistanceStraight)
            %go toward target (left)
            agent(t, 5) = 0;
        %if something is in front...
        else
            %if there's nothing to the left and something right, turn
            %left and keep going until can go straight:
            if ((distanceLeft < 0 || distanceLeft>ignoreDistanceSides) && ...
                    (distanceRight > 0 && distanceRight<ignoreDistanceSides))
                agent(t, 5) = uMax;
                keepTurning = 1;
            %if there's something on the left but nothing on the right,
            %turn right and keep going until can go straight:
            elseif ((distanceRight < 0 || distanceRight>ignoreDistanceSides) &&...
                    (distanceLeft > 0 && distanceLeft<ignoreDistanceSides))
                agent(t, 5) = -uMax;
                keepTurning = -1;
            %if there's something on all sides or nothing on either side...
            else
                %turn to random side
                randomSign = sign(rand() - .5);
                agent(t, 5) = randomSign*uMax;
                keepTurning = randomSign;
            end
        end
        
    %with keep turning off:
    %if target is left by less than 45 degrees (wnat to go +uMax)
    elseif thetaToTarget < pi/4
        %if nothing is in the way in front...
        if (distanceStraight < 0 || distanceStraight>ignoreDistanceStraight)
            %go toward target (left)
            agent(t, 5) = uMax;
        %if something is in front...
        else
            %if there's nothing to the left (where car wants to go), turn
            %left and keep going until can go straight:
            if (distanceLeft < 0 || distanceLeft>ignoreDistanceSides)
                agent(t, 5) = uMax;
                keepTurning = 1;
            %if there's something on the left but nothing on the right,
            %turn right and keep going until can go straight:
            elseif (distanceRight < 0 || distanceRight>ignoreDistanceSides)
                agent(t, 5) = -uMax;
                keepTurning = -1;
            %if there's something on all sides (and not too near either side)...
            else
                %turn and keep turning
                agent(t, 5) = sign(rand() - .5)*uMax;
            end
        end
    
	%if target is below by less than 45 degrees (wnat to go -uMax)
    elseif thetaToTarget > 7*pi/4
        %if nothing is in the way in front...
        if (distanceStraight < 0 || distanceStraight>ignoreDistanceStraight)
            %go toward target (right)
            agent(t, 5) = -uMax;
        else
            if (distanceRight < 0 || distanceRight>ignoreDistanceSides)
                agent(t, 5) = -uMax;
                keepTurning = -1;
            elseif (distanceLeft < 0 || distanceLeft>ignoreDistanceSides)
                agent(t, 5) = uMax;
                keepTurning = 1;
            else
                %turn and keep turning
                agent(t, 5) = -sign(rand() - .5)*uMax;             
            end
        end
        
    elseif thetaToTarget < pi
        if (distanceLeft < 0 || distanceLeft>ignoreDistanceSides)
            agent(t, 5) = uMax;
        else
            if (distanceStraight < 0 || distanceStraight>ignoreDistanceSides)
                if distanceLeft < lastDistanceLeft
                    agent(t, 5) = -uMax;
                elseif distanceLeft > lastDistanceLeft
                    agent(t, 5) = uMax;
                else
                    agent(t, 5) = 0;
                end 
            elseif (distanceRight < 0 || distanceRight>ignoreDistanceSides)
                agent(t, 5) = -uMax;
            else
                agent(t, 5) = uMax;
            end
        end
        
    else
        if (distanceRight < 0 || distanceRight>ignoreDistanceSides)
            agent(t, 5) = -uMax;
        else
            if (distanceStraight < 0 || distanceStraight>ignoreDistanceStraight)
                if distanceRight < lastDistanceRight
                    agent(t, 5) = uMax;
                elseif distanceRight > lastDistanceRight
                    agent(t, 5) = -uMax;
                else
                    agent(t, 5) = 0;
                end 
            elseif (distanceLeft < 0 || distanceLeft>ignoreDistanceSides)
                agent(t, 5) = uMax;
            else
                agent(t, 5) = -uMax;
            end
        end
    end

    test(t, :) = [distanceLeft, distanceStraight, distanceRight, x, y, thetaToTarget, agent(t, 5)];
        
    lastDistanceLeft = distanceLeft;
    lastDistanceRight = distanceRight;
    lastDistanceStraight = distanceStraight;
    
    
    new_agent = agent;
end


