Goals

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 create and use actions with robots. You will move the robot and use the Color Sensor to tell what color it is reading, indicating that on the screen and with different sounds.

First, open the provided template and click on the main.quorum file. You will see that the libraries are all declared and also a ColorSensor object called colorSensor. The ColorSensor object is before the action because all the objects that are going to be used in more than one action should be declared in the class before all the actions. Inside the class Main, is the action called Main and two other actions that you are going to created.

Activity: Declare a Motor, Sound, Screen objects under the ColorSensor object.
You will use these objects later in your program.

Goal 1: Creating actions

The Main action is where the principal code is going to be, so we will leave it for after you've created the other two actions. We will start creating the second action, on line 21, that we will call Move and it will take two text parameters, that we will call motor1 and motor2.

Example: Create the action Move and declare the parameters.

//This line will create an action called Move.
//The parentheses declare two parameters
action Move(text motor1, text motor2)

We will call this action to move the robot, so you will have to write the code to move the robot inside action Move.

Activity: Write the proper code to move the robot forward.
Use the RotateByDegrees statement to move your robot.

The robot should stop before reading the color. Add this line to your action, so the program will wait for the motor to stop before continuing.

motor:Wait(motor1)

You have completed the first action. Let's create the action ColorSensor. This action won't take a parameter.

Activity: Create action ColorSensor.
Create the action. Remember to use an end.

To read the colors using the Color Sensor we will need to use the GetColor command. Remember that the sensor can return only 8 colors options: none, black, white, brown, red, green, blue and yellow. In the example below you can see the first two conditional statements.

Example: Start the conditional statements.

//This line gives the text returned from GetColor to the variable color
text color = colorSensor:GetColor()
//This is the first conditional statement. It checks the color "none."
if color = "none"
     sound:Buzz()
     screen:Output("No color read!"
//This line is a second conditional statement to check if the color is "black".
elseif color = "black"
     sound:PlayTone(20, 250)
     screen:Output("The color is:" + color, 4)

Activity: Write six more conditional statements.
Add elseif statements for the six other color possibilities. Make each sound unique, and remember to output information to the screen. Also, remember to end the conditional.

Activity: Create a Utility object. Also use SetPort to set the Color Sensor.

Goal 2: Using actions

Go to action Main to write the code to run the robot. You will use actions to make the robot move, stop, read and tell the color 5 times.
To call an action, you write the action name with any parameters in the parentheses. If there are no parameters, you leve the parentheses empty. () Use the name of your Motor objects in the parentheses of the Move action. You can use a repeat statement to simplify your program.

Activity: Use actions to make the robot move, stop, read and report color five times.
You may want to use the Utility object with the DelayMilliseconds() action to make your robot pause.

Next Tutorial

In the next tutorial, we will discuss Actions Lesson, which describes more on actions..