Goals

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

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

Overview

This challenge will use the InfraredSensor Library. You will use conditions with the Infrared Sensor in order to avoid the wall.

Goal 1: Creating Objects

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 the Quorum InfraredSensor and Motor libraries inside the Lego library.

Activity 1: Instantiate the Motor and the InfraredSensor objects.

Call the Motor object motor and the InfraredSensor object infraredSensor.

Now you must set the port to work with. That will be the port that the Infrared Sensor is connected to on the robot. You will do that using the SetPort statement.


Example: Set the port your Infrared Sensor is connected to.

// This line of code will set the port that your robot will work with
infraredSensor:SetPort(4) //This line is a looping condition; once the condition doesn’t match anymore the program stops 
repeat while true 

After that you should set the speed of the motors, so despite any condition the motors will work at this speed when they’re active.

Activity 2: Write the code to set the speed of motors B and C.

Goal 2: Using conditional and looping statements

The robot should go forward and, if the infrared sensor detects a wall, it will turn to the right first. If there is a free space the robot will move forward, but if there is also a wall after turning right, it will turn to the left and move forward. If there is also a the wall there, the robot will stop because it has nowhere to go!

Let’s start with the conditions now. In order to get the distance between the robot and an obstacle you will use the GetDistance command. To make sure we have enough space to turn, let’s use 25 cm as the maximum distance.


Example: Create the conditions that will check the distance between the sensor and the wall and then make the robot move accordingly.

//Before the conditions, to make it easier, let's set the distance values the sensor will take
distance = infraredSensor:GetDistance()
//This line states that in case the distance is greater than 40 cm
if infraredSensor:GetDistance() > 40
//the robot should move forward
motor:RotateBackward("B")
motor:RotateBackward("C")
//This line states that in case the distance isn’t greater than 25 cm
else
//the robot will stop and turn right
motor:Stop("B")
motor:Stop("C")
motor:RotateByDegrees("C", -655)
motor:Wait("C")
//You will also use an if statement inside an if statement to measure the distance again after turning right
//This line of code will do that again
distance = infraredSensor:GetDistance()

Activity 3: Using if and else statements, create the other conditions.

Don’t forget that you can (and in this case will need to) have an if inside another. You’ll do these conditions now: If after the robot turns to the right, there is a free space, the robot should move forward. Else the robot should turn 180 degrees to the left. If there is free space the robot should move forward. After these conditions we still need one more: in case the left also has a wall.


Example: Last condition: stop the motors.

//This line of code states that in case there is no free space to the left 
else
//These lines will make the robot stop
motor:Stop("B")
motor:Stop("C")
//These lines finish each one of the conditional (if) statements and the looping (repeat) statement
end
end
end
end

Hopefully your program is running great at this point! Run it and see if you get any errors. If you do, go back and fix them. Otherwise, you just finished your challenge! Congrats!

Next Tutorial

In the next tutorial, we will discuss Actions, which describes how work actions in Quorum..