Objectives

In this lab, we will learn the following computer science concepts:

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

Overview

This lab demonstrates how to use the InfraredSensor Library. You will use the Infrared Sensor to move the robot in the direction of the remote.

Goal 1: Taking actions based on user input

Create a new Robot project. Name it properly and open the main.quorum file. In the top of the file type the appropriate code to use both the Quorum InfraredSensor Library,and the Motor library inside the Lego library. After that, you will create objects from each library.

Example: Declare a Motor object.

// this line declares a Motor object called "motor"
Motor motor

Activity: Declare an InfraredSensor object called infraredSensor.

Sensors work differently than motors, in that you must set the port that the sensor is connected to. You will do that using the SetPort statement.

Example: Tell the port your Infrared Sensor is connected.

// This line of code will set the port to work with
infraredSensor:SetPort(4)  //Inside the parentheses you should put the number of the port that your Infrared Sensor is connected

Activity: Set the default speed of the motors. You may choose your speed.

Goal 2: Using conditional and looping statements

You should have some lines of code before the conditions. These lines and why you should use each of them are below:

//This first line gives a value for the loop to start. It will change once the loop starts so the value doesn't matter
integer direction = 0 
//This line starts the loop; it keeps the program working unless something break the loop
repeat while true 
//This line gives the number variable "direction" the value that comes back from the GetRemoteDirection statement, it takes as parameter the channel you're using on the remote 
number direction = infraredSensor:GetRemoteDirection(1)

The GetRemoteDirection statement returns a value that represents the relative angle between the remote and the sensor. These values will be between -180 and 180.

For the GetRemoteDirection statement to work, you need to press the large button (top center button) in the remote. Every condition, and the whole program, will only work if you press the button. That is what makes the repeat statement true.

You will use conditional statements to define how the robot will respond to the position of the remote.

The values between -180 and 180 represent the relative angle between the sensor and the remote. These values will be used in the conditional statements. If the value is negative, it means the remote is in the left of the robot; if the value is positive, the remote is to the right of the robot, and if the value is 0, the remote is directly ahead of the robot.

Example: Negative values.

//This line checks to see if the condition returns a negative number
if direction < 0 
//These lines make the robot turn left, towards the remote
motor:RotateBackward("B")
motor:Stop("C")

Activity: Add another statement to respond to positive values.

Use an elseif statement to make the robot turn left if direction > 0.


Our next condition also uses elseif, but now the only possible option it will be the value 0, so we don't need to specify that. We will define a minumum and maximum range the robot can be from the remote, so it will stop.

Example: Condition using the GetRemoteDistance statement.

//This line of code only runs if direction = 0, and the distance from the remote is between 10 cm and 50 cm (the maximum distance it could be from the remote) 
elseif infraredSensor:GetRemoteDistance(1) > 10 and infraredSensor:GetRemoteDistance(1) <= 50

Activity: Write the code to make each motor move.

You've already done the three possible direction conditions, so it's time to write the last condition, which is if none of these conditions are true. In that case the robot should stop both motors, to write that you'll use the else statement.


Example: Last condition, in case none of the above is true.

//These lines of code is to tell the motors to stop in any case that doesn't match the previous conditions
else
motor:Stop("B")
motor:Stop("C")
//These lines you use to finish the if loop and the repeat loop
end
end

You now have a working program, run and see if you get any errors. If you do, go back and fix it, otherwise just try to see if the robot is doing what you've told it to do when you keep the first button pressed with the remote in each position. Have fun!

Next Tutorial

In the next tutorial, we will discuss Assignment 3.1, which describes how work Travel Reservation in Quorum..