PreAP Computer Science Lego Lab Assignments
Lego Lab 04 / Using Data Constants and Data Wires
80 & 100 Point Versions
Lab 04 Objectives
Learn about different data types.
Use the Constant program block.
Connect program block data bars with data wires.
Perform and display arithmetic binary operations.
# / Actions
01 / Start LegoLab04a
Open the NXT Programming software.
Start a new program, called LegoLab04a and click Go, shown in Figure 01.
Click File, Save As, browse to the LegoLab04 folder and then click Save.
Figure 01

02 / Add a Constant Block
Click on the orange Data group.
Select, and drag, a Constant block to the sequence beam, like Figure 02a.
The Constant block stores a value that cannot be changed, hence the name constant.
Click on the Constant block.
This displays attributes of the Constant block.
Remember, attributes are the values in a block that specify the behavior of the block.
Click on the Data Type window and select Text.
Enter HELLO WORLD in the Value window, like it is shown in Figure 02b.
Figure 02a

Figure 02b

03a / Add a Display Block and a Wait Block
Click on the green Common group.
Drag a Display block and a Wait block on the sequence beam.
Click on the Display block to open the attribute window.
Drag a Display block and a Wait block on the sequence beam.
You now have three blocks, like Figure 03a.
Click on the Wait block to open the attribute window.
Change the Control window value to Time.
Change the Until window to 10, like Figure 03c.
Figure 03a

Figure 03b

Figure 03c

05 / Check the Program
Make sure that the Lego NXT Program block is attached to your computer with the USB cable.
Compile and execute the program.
Check the NXT screen. It displays Mindstorms NXT.
It is our intention to display HELLO WORLD, stored in the Constant block, but the blocks have no connection.
06 / Display the Data Hub
Some blocks, like the Display block have a data bar used for data input/output.
This bar is initially hidden.
Click the bottom-left tab on the Display block.
This will lower the data bar, like Figure 04b.
The Constant block already has a data hub.
Figure 06

07 / Route data from the Constant Block to the Display Block
You need to drag a connection between the Text output of the Constant block and Text input of Display.
You can left-click at the output plug and then left-click at the input plug.
You can also create your own path by releasing mouse button at any desired turn.
Your teacher will demonstrate the technique.
Now compile and execute the program. This time it should display HELLO WORLD.
Figure 07

08 / Make Sure That Data Types Match
Click on the Constant block to display and alter the attribute values.
In the Data Type window change the type to Number.
In the Value window enter 100.
Look at Figure 08a, you will see that the orange data wire now has become gray.
Right-Click on the data wire and look at the bottom-right window, shown in Figure 08b.
It explains the problem.
Figure 08a

Figure 08b

Alert students may notice that the data wire is connecting a number, indicated by # to text, indicated by T.
You have probably noticed the number symbol #, at the bottom of the Display' data hun.
That plug is meant for a Radius value, which has a very different purpose.
Right now you are only concerned with the output display and that can only be done with text.
09 / Add the Number-To-Text Block
If the Display block can only display text, then how is it possible to display numerical values?
This is possible with a special block that can convert numbers to text.
Click on the red Advanced group bar, shown in Figure 09a.
The block with two 3s converts numbers to text.
Click-drag the Number-To-Text block in between the Constant block and the Display block, like Figure 09b.
Figure 09a

Figure 09b

10 / Correct the Data Wires
Figure 09b shows that the Constant block still is connected to the Display block with a gray data wire.
With the new block inserted, wires must be rearranged.
First connect the number plug of the Constant block with the number plug of the Number-To-Text block.
This wire will appear yellow, indicating that it is a number data wire.
Second connect the text plug of the Number-To-Text block to the text plug of the Display block.
This wire will appear orange, indicating that it is a text data wire.
Any time that you want to remove a data wire, right-click the input plug, followed by a left-click.
Now compile and execute again.
This time your display should be 100.
Figure 10

11 / Start LegoLab04b with six Program Block
Start a new file, called LegoLab04b.
Add six program blocks, as shown in Figure 11a.
This includes the new Math block, which is part of the orange Data group.
For the first five blocks click the tab on the bottom-left corner of the block to pull down the data hub.
The five data bars with their data plugs now appear as Figure 11b.
Figure 11a

Figure 11b

12 / Enter Attribute Values
You will not be shown the pictures of the attribute windows.
Four blocks require the following attribute values
The first Constant block stores the Number data type value 100.
The second Constant block stores the Number data type value 200.
The Math block needs the Operation value Addition.
The Wait block needs the Control value Time for 10 seconds.
13 / Connecting the Data Wires
Connect the two Constant blocks with data wires to the A & B plugs of the Math block.
Connect the Number plug of the Math block to the Number plug of the Number-To-Text block .
Connect the Text plug of the Number-To-Text block to the Text plug of the Display block.
Make sure you have no gray wires.
The data wires should look like Figure 13.
Compile and execute the program.
Figure 13

14 / The LegoLab04 80-Point Requirements.
Save the program with the file name LegoLab04v80.rbt.
Write a program that performs addition and subtraction.
The program starts with two constant numbers.
The same two numbers are used for addition and subtraction.
The sum and difference are displayed.
15 / The LegoLab04 100-Point Requirements.
Save the program with the file name LegoLab04v100.rbt.
Write a program that performs addition, subtraction, division and multiplication.
The program starts with two constant numbers.
The same two numbers are used for all four binary operations.
The results of each operation are displayed.

Lego Mindstorms 2010, PreAPCS Edition Lego Lab 04 Page 32 05-26-10