Options = {'Autonomous','Manual Control'}
Prompt = menu('How would you like to control the rover?',Options)
m = mobiledev
r = roomba(19)
if Prompt == 1
    Explore(r)
else
    while true
  
pause(.5)
PhoneData=m.Orientation;
Azi=PhoneData(1);
Pitch=PhoneData(2);
Side=PhoneData(3);
    
    if Side>130 || Side<-130 %if the phone is flipped face down stop the roomba and exit loop
        r.stop
        r.beep('C,C,C,C')
        break
    elseif Side>25 && Side<40 %if the phone is turned sideways between 25 and 40 deg turn left 5 deg
        r.turnAngle(-5);
    elseif Side>40 %if the phone is turned sideways over 40 deg turn left 45 deg
        r.turnAngle(-45)
    elseif Side<-25 && Side>-40 %if the phone is turned sideways between -25 and -40 deg turn right 5 deg
        r.turnAngle(5);
    elseif Side<-40 %if the phone is turned sideways less than -40 deg turn left 45 deg
        r.turnAngle(45)
    end
    
%If phone is held near verticle take an image and plot it
    if Pitch<-60 && image<=9
        r.beep
        img=r.getImage;
        subplot(3,3,image)
        imshow(img)  
    end
    
%move forward and backward based on the front and back orientation
    if Pitch>15 && Pitch<35 %if pitch between 15 and 35 deg move forward short distance
        
        %get light bumper data before moving
        litBump=r.getLightBumpers;
        if litBump.leftFront>500 || litBump.leftCenter>500 || litBump.rightCenter>500 || litBump.rightFront>500 %if something is in front of the roomba and will hit if it moves forward make noise and display message
            r.beep('C^^,F#^,C^^,F#^')
            
        else %move
        r.moveDistance(.03);
        
        %Get bumper data after moving
        Bump=r.getBumpers;
        if Bump.right==1 || Bump.left==1 || Bump.front==1
           r.beep('A,C,E')
            
            r.moveDistance(-.01)
        end
        
        %get cliff sensor data
        Cliff=r.getCliffSensors;
        if Cliff.left>1500 || Cliff.leftFront>1500 || Cliff.rightFront>1500 || Cliff.right>1500 %if something triggers the cliff sensor treat it as lava and back up
            r.beep('C^^,C,C^^,C,C^^,C,C^^,C,C^^,C,C^^,C')
            
            r.moveDistance(-.031)
        end
        end
        
    elseif Pitch>35 %if pitch greater 35 deg move forward longer distance
        
        %get light bumper data before moving
        litBump=r.getLightBumpers;
        if litBump.leftFront>15 || litBump.leftCenter>15 || litBump.rightCenter>15 || litBump.rightFront>15 %if something is in front of the roomba and will hit if it moves forward make noise and display message      
            r.beep('C^^,F#^,C^^,F#^')
           
        else %move
        r.moveDistance(.3)
        
        %Get bumper data after moving
        Bump=r.getBumpers;
        if Bump.right==1 || Bump.left==1 || Bump.front==1 %if you hit something make a noise, display message, and back up
           r.beep('A,C,E')
         
            r.moveDistance(-.01)
        end
        
        %get cliff sensor data after moving
        Cliff=r.getCliffSensors;
        if Cliff.left>1500 || Cliff.leftFront>1500 || Cliff.rightFront>1500 || Cliff.right>1500 %if something triggers the cliff sensor treat it as lava and back up
            r.beep('C^^,C,C^^,C,C^^,C,C^^,C,C^^,C,C^^,C')
            
            r.moveDistance(-.31)
        end
        end
        
    elseif Pitch<-15 && Pitch>-35 %if pitch between -15 and -35 deg move back short distance
        r.moveDistance(-.03);
        
        %get cliff sensor data after moving
        Cliff=r.getCliffSensors;
        if Cliff.left>1500 || Cliff.leftFront>1500 || Cliff.rightFront>1500 || Cliff.right>1500 %if something triggers the cliff sensor treat it as lava and back up
            r.beep('C^^,C,C^^,C,C^^,C,C^^,C,C^^,C,C^^,C')
            
            r.moveDistance(.04)
        end
        
    elseif Pitch<-35 && Pitch>-60 %if pitch between -35 and -60 deg move back longer distance
        r.moveDistance(-.3)
        
        %get cliff sensor data after moving
        Cliff=r.getCliffSensors;
        if Cliff.left>1500 || Cliff.leftFront>1500 || Cliff.rightFront>1500 || Cliff.right>1500 %if something triggers the cliff sensor treat it as lava and back up
            r.beep('C^^,C,C^^,C,C^^,C,C^^,C,C^^,C,C^^,C')
            
            r.moveDistance(.31)
        end
    end
end
end