An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to...

24
An Introduction to Programming using the NXT Robot: exploring the LEGO ® MINDSTORMS ® Common palette. Student Workbook for independent learners and small groups The following tasks have been completed by: ________________________________________ Task 1 Measuring the Move boxes pages 4, 5 Task 2 Distance Challenge page 5 Task 3 Distance Questions page 7 Task 4 Turns boxes page 8, 9, 10 Task 5 Robot Walk Loop page 11 Task 6 Little Red Riding Robot Challenge page 12 Task 7 Clap and Go Challenge page 14 Task 8 Tiger Robot Challenge page 16 Task 9 Round the Garage Challenge page 17 Task 10 Runaway Robot Challenge page 18 Task 11 Racing Robot page 19 Task 12 Yo-yo Robot page 21 Task 13 Line Follower Challenge page 22 Task 14 Blocked Up Challenge page 24 written by Sandy Garner

Transcript of An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to...

Page 1: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

An Introduction to Programming using the NXT Robot:

exploring the LEGO® MINDSTORMS® Common palette.

Student Workbookfor independent learners and small groups

The following tasks have been completed by:

________________________________________

Task 1 Measuring the Move boxes pages 4, 5

Task 2 Distance Challenge page 5

Task 3 Distance Questions page 7

Task 4 Turns boxes page 8, 9, 10

Task 5 Robot Walk Loop page 11

Task 6 Little Red Riding Robot Challenge page 12

Task 7 Clap and Go Challenge page 14

Task 8 Tiger Robot Challenge page 16

Task 9 Round the Garage Challenge page 17Task 10 Runaway Robot Challenge page 18

Task 11 Racing Robot page 19

Task 12 Yo-yo Robot page 21

Task 13 Line Follower Challenge page 22

Task 14 Blocked Up Challenge page 24

written by Sandy Garner

Page 2: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Getting started:

If you see a screen like this:

type a name for your program in the Start New Program box, and press the Go button

otherwise: choose New Program from theMINDSTORMS Toolbar

The program will now have a name but it still isn't saved onthe computer.

1. Choose Save Program, and you will see a Save As window.

Type the name for your program in the File Name box if it isn't there already. Keep .rbt at the end of the name, because that's how the computer knows that this is aMINDSTORMS NXT program.

2. Press the Save button to save your program with the name you have chosen.

An Introduction to Programming using the NXT Robot 2 Exploring the LEGO® MINDSTORMS® Common Palette

The exercises in this book expect an NXT robot built with motor outputs on ports B and C, a touch sensorbehind, a light sensor pointing down, an ultrasonic sensor pointing forward and a sound sensor somewhere.The standard setup as in the building guide that comes with the educational set is ideal.

Page 3: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

You should now be able to see something like the picture below.

This is the space where you write your robot program. Theprogram starts on the left, where it says Start, and is built upto the right by connecting blocks together in a line. Theblocks you can use are chosen from the left hand side.

These blocks are from the Common Palette.

Any changes you make to your program won't be savedon the computer unless you select Save Program. It is agood idea to save your program often.

Move The robot can move forward and backward (reverse). It has one motor for each wheel. The motors areconnected to the B and C ports of the robot by cables.

1. Drag a Move block (cog) to the positionoutlined around the word Start.

The Move block lets you control how far your robotwill move but it doesn’t use metres or centimetres, andyou might start to see why soon.

When the Move block is highlighted (you will see apale blue line around the outside) some choices aboutMove appear in the gray box underneath your programspace.

This gray box is called the Configuration Panel.

If you don't change anything, the robot is set to move the B motor and the C motor for 1 rotation.

Rotations A rotation is a turn. One rotation is one whole turn of the wheel.

Let's find out how far the robot will move with one rotation.You will need to download the program you have written on the computer (the Move block) to therobot. Download means to copy the program from your computer to the robot.

2. Download your program to the robot using the Download arrow.

The USB cable needs to be connected to the robot's USB port whilethe computer is downloading.

The robot needs to be turned on. Press the orange button on the robotif your robot isn't turned on.

The robot will beep when the downloading is finished. After the beep, you may pull out the USBcable.

3. Run your program on the robot.

An Introduction to Programming using the NXT Robot 3 Exploring the LEGO® MINDSTORMS® Common Palette

Page 4: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Watch the robot's screen while you push the robot's orange button 4 times. It should show

My Files then

Software files then

your file's name, then

Run.

Run the program again.

4. Stick a marker (e.g. chalk, blue tack) on the side of a wheel. Watch how far the wheelmoves when your program is run.

Task 1. Fill in the 10 Boxes about Measuring the Move

5. Measure how far your robot moves with 1 rotation.

6. Calculate how far your robot will move with 2 rotations. Tryit, and measure it.

Question: Does doubling the rotations double the distance travelled?

SecondsYou can choose how long the robot's axle turns for in seconds. You will have to experiment with howfar the robot travels.

1. Change Rotations to Seconds. Make your robot move for 1second on half power (50). Download and run. Measurehow far it goes.

2. Make your robot move for 1 second on full power (100).Download and run. Measure how far it goes.Compare the distance with step 1 above.

Question: Does doubling the power double the distance travelledexactly?

3. Make your robot move for 2 seconds on half power (50).Download and run. Measure how far it goes.

Question: Does doubling the time double the distance travelled exactly?

An Introduction to Programming using the NXT Robot 4 Exploring the LEGO® MINDSTORMS® Common Palette

1.2

1.1

1.5

1.6

1.7

1.8

1.4

1.3

Page 5: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

DegreesYou can measure how far the wheel will go round using a measurecalled degrees.

All the way round a circle (or a wheel) is 360 degrees, which iswritten 360o.

If you choose either 1 rotation or 360 degrees, your robot shoulddo exactly the same thing - turn the wheel 1 complete circle.

1. Change your Move block's Duration to 360 Degrees.

If you download and run your program now, the wheel should go round exactly once.

Can you work out how many degrees would make the wheel turn halfway round?

Can you work out how many degrees would make the wheel turn a quarterof the way round?

Degrees are really useful if you want the wheel to turn just a little.

Task 2. Distance Challenge

1. Make your robot travel exactly 8 cm. See if you can get it right first time, using themeasurements you have already made to calculate the settings required.

2. Make your robot travel exactly 1 metre. Once again, work it out first.

An Introduction to Programming using the NXT Robot 5 Exploring the LEGO® MINDSTORMS® Common Palette

1.9

1.10

Page 6: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Building programs.

Now that you are an expert on using theMove block, you can start to build longerprograms.

1. Drag another Move block to yourprogram space and connect it to theright of the one you already have.

2. Move your mouse over the Wait(hour glass) block, then click on theTime (clock) block. An orange Time block will be created.

3. Drag the orange Time block so it pushes between your two green Move blocks.

The program pictured above will make your robot perform three tasks, one after the other, from leftto right. They are connected by what MINDSTORMS® calls the sequence beam. (A sequence is anordered list. A beam is a long thin shape.)

Now that you have more than one block, to change the settings on any one block, click on it once tohighlight it then make the change in its Configuration Panel.

4. Change the settings on all 3 blocks in your program to make your robot move forward 1 rotation

stop and wait for 2 seconds

move backwards (reverse) for 1 rotation.

5. Download and run.

Have you noticed the little changes to the pictures onthe block when you change the settings?

When you write longer programs these pictures will help remind you which block is doing what.

An Introduction to Programming using the NXT Robot 6 Exploring the LEGO® MINDSTORMS® Common Palette

Page 7: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Task 3. Move Questions

3.1 Are you more likely to make the robot travel an exact distance using seconds or rotations?

3.2 If you want the wheel to turn just a little, should you use seconds, rotations or degrees?

3.3 What do you think would happen if you put smaller wheels on the robot then ran your “exactly 2 metre” program again?

3.4 What do you think would happen to your robot if one wheel was bigger than the other when you ran your “exactly 2 metre” program? Why?

3.5 Why do you think the MINDSTORMS program doesn’t just let you tell the robot how many centimetres or metres you want it to go?

3.6 When you make a change to your program and download it to the robot, what has happened to the old version of your program on the robot?

3.7 What do you think would happen if you put the brake on one wheel and turned the other wheel?

An Introduction to Programming using the NXT Robot 7 Exploring the LEGO® MINDSTORMS® Common Palette

Page 8: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

TurningTask 4. Fill in the 11 Boxes about Turns

How can your robot turn when it doesn't have a steering wheel?

The picture shows a robot moving in a circle. It shows 2 wheels which we can call the insidewheel and the outside wheel.The inside wheel makes the small circle and the outsidewheel makes the big circle as the robot goes around.

Which wheel moves thefurthest?

Which wheel turns the fastest?

Curve Turn

1. Delete all blocks in your program except one Move block.

2. Look at the Curve Turn picture below, and make your Steering match. Make yourrobot do the turn for 1 Rotation. Download and run.

Curve Turn

3. Put a marker on the side of each wheel. Watch each wheel in turn as the program runs.Do they both do one whole rotation?

If not, describe what happens.

4. Change the Duration to 10 rotations. Download and run. Can you see the circle the robotis trying to travel around?

An Introduction to Programming using the NXT Robot 8 Exploring the LEGO® MINDSTORMS® Common Palette

4.2

4.1

4.3

4.4

Page 9: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Point Turn

1. Make your robot do an all-the-wayturn for 1 rotation. Download andrun.

Point Turn

Watch each wheel in turn as the program runs. Do they both do one whole rotation?

Did you notice anything else?

2. Change the Duration to Degrees. Without changing the steering, try different numbersin the degrees box until you get the robot to make a quarter turn.

How many degrees will give you a quarterturn?

Wheel Turn 1

1. This time, take the slider back one position towards the centre. Set the Duration to 1rotation or 360 degrees. Download and run.Watch each wheel in turn as the programruns.

Wheel Turn 1

Describe what you see.

2. Try different numbers in the Degrees box until you get the robot to make a quarterturn.

How many degrees will give you a quarter turn?

An Introduction to Programming using the NXT Robot 9 Exploring the LEGO® MINDSTORMS® Common Palette

4.5

4.6

4.7

4.8

4.9

Page 10: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Wheel Turn 2

1. Try one more way of making a turn. Turn the C motor off. Download and run.

Wheel Turn 2

Watch each wheel in turn as the program runs.Describe what you see.

2. Try different numbers in the Degrees box until you get the robot to make a quarterturn.

How many degrees will give you a quarter turn?

The numbers you have written down will be very useful for the Little Red RidingRobot challenge, and other challenges ahead.

Turn summary:

These are the three different types of turn your robot can make.

1. The curve turn - the robot will move in a circle.

This turn is made by turning the robot's wheels at different speeds.

The further the slider is from the centre position, the smaller thecircle will be.

2. The point turn - the robot spins around a point under its middle.

This turn is made by turning the robot's wheels in differentdirections , one forward, the other backwards.

3. The wheel turn - the robot spins around one wheel.

This turn is made by one wheel staying where it is and the other wheelturning.

An Introduction to Programming using the NXT Robot 10 Exploring the LEGO® MINDSTORMS® Common Palette

4.10

4.11

Page 11: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Task 5. Robot Walk Loop

The task is to make your robot look as much as it can like it is walking by moving one wheeljust a little, then the other.

1. Program the robot's first two 'steps'. 2. Drag a Wait for Time block after each 'step'. How long should your robot wait between

'steps'?

LoopsLoops are very useful for saving boring repetition. All the other robot steps will just be the sameas these two. Rather than drag more and more Move and Wait blocks down, you can make theprogram repeat these steps over and over by putting them in a loop.

3. Drag a Loop block to the first position of your program space.

4. Change the Loop control from Forever to Count. Type 4 in the Count box.

5. Drag your program blocks, keeping them in the same order, inside the Loop block.

6. Download and run. Did your robot stop after 8 (4 pairs of) steps?

7. Save your program using File > Save As because you will want to use it again later. Callit Walk.rbt.

An Introduction to Programming using the NXT Robot 11 Exploring the LEGO® MINDSTORMS® Common Palette

Page 12: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Task 6. Little Red Riding Robot Challenge

Your Little Red Riding Robot has been to visit Grandma and it is time to go home. The map showsyou the shape of the turns she has to make.

Make sure she stops and waits to look for cars at the end of Grandma's driveway before turning intothe main road.

She always reverses into hergarage, because she likes tocome out facing forwards.

Program your robot to makethe journey home.

Use the table to help withyour planning.

If you have been carefulfilling in the boxes so faryou might be able to makethe journey correctly on thefirst attempt.

Now there's a challenge!

An Introduction to Programming using the NXT Robot 12 Exploring the LEGO® MINDSTORMS® Common Palette

List of journey actions, in order Measureddistanceor turn

Block used Block Setting(degrees, rotations,seconds, power, B/C . . .)

along Grandma's driveway

stop

wait

Page 13: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Sound SensorThe robot can detect (sense) noises that are made around it. It can be made to wait until it detects aloud noise before it carries on with its program.

The program you are about to write will make the robot wait for a sound, like a clap, before it startsto move.

1. Start a New Program. Drag a Wait for Sound block to the Start position.

2. The Configuration Panel lets you pick how loud a sound to wait for. Choose Sound >(greater than) 20.

3. Drag a Move block to your program. Download and run.

Your program will do nothing until . . . a clap, a sneeze, a door slam, a shout . . . then off it goes.

Reading the sound level on the computer.The robot can help you choose a number for the sound level by showing you number readings forsounds. Readings can be shown on the computer or on the robot screen. The following steps willshow you how to get a reading on the computer.

4. Connect the USB (download) cable to the robot, and select (click on) the orange Soundblock in your program.

5. Select the NXT window - it is above the Download button.

6. Connect and Close the window that pops up.

An Introduction to Programming using the NXT Robot 13 Exploring the LEGO® MINDSTORMS® Common Palette

Page 14: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

7. Look under the Hourglass at the bottom left corner of the Configuration Panel. There isa box with a number in it.

This is the feedback box. Now that you have connected your robot to the computer, the feedback boxis showing how loud the noises around the robot are right now. Watch it quietly for a minute andwrite down the highest number you see.

8. Make a noise and watch the number in the feedback box change. If your noise is finished very quickly, like a clap, it may not show unless you make a few of them oneafter the other. A continuous noise should give you a steady reading. Try humming.

Task 7. Clap and Go Challenge

Make your robot move in a square.

It doesn't start to move until you clap.

Each time you clap it moves 1 rotation along one side, turns a 90o (square) corner and waitsfor the next clap.

Use a loop.

It stops after 4 sides.

An Introduction to Programming using the NXT Robot 14 Exploring the LEGO® MINDSTORMS® Common Palette

clap

clap

clap

clap

7.1

Page 15: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Light SensorYour robot can detect when it comes to a dark line on a light coloured floor.

1. Start a new program. Drag a Move block to the Start position and set it to Unlimited.

2. Drag a Wait for Light block to the program.

3. In the Light Sensor Configuration Panel, set the Until box to Light < 50

4. Make sure the Generate light box has a tick in it.5. Drag another Move block to the end of the program, and set it's Direction to Stop.

6. Download and run. Set your robot off towards a black stripe.

Does it stop when it reaches the stripe?

What happens if you start it on black?

What happens if you take the last Move block (Stop) away?

The light sensor is sending a light out and measuring how much light is reflected back. Differentcolours will reflect different amounts of light.

Other things will also change the reading. If the sun comes out, a light in the room is turned on orsomebody makes a shadow, the reading will change.

An Introduction to Programming using the NXT Robot 15 Exploring the LEGO® MINDSTORMS® Common Palette

8.1

8.2

8.3

Page 16: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

It is useful to know what readings the light sensor makes for the black line and the whitebackground. You could use the feedback box as you did for the Sound sensor, but probablyyour USB cable won't reach far enough. There is another way to get a reading that doesn'tneed the computer to be attached to the robot.

Reading the light level on the NXT using View.1. Get My Files showing on the robot's screen. If you have just run a program, press the

darker gray button 3 times so you see My Files. If you have just turned the robot on, MyFiles will be showing.

2. Find View by pressing the grey right-arrow button 2 or 3 times. NXT Program, then View OR NXT Program, then NXT Datalog, then View

3. With View showing, press the orange button to select it.

4. Now you may choose which sensor reading you wish to view. Press the right-arrowbutton twice to find Reflected Light. Press the orange button to select it.

5. You are asked to choose which port this sensor is plugged in to. Use the arrows to selectPort 3 (or whichever port your light sensor is in).

6. If you have pressed the orange button, you should see a little box with a number in iton the robot's screen. This is the Light Sensor reading.

Put your robot's light sensor over the blackline and write down the reading.

Black

Put your robot's light sensor over thewhite background and write down thereading. White

It is important to know these numbers so that you can make good choices about the numbers to usein your program.

Task 8. Tiger Robot Challenge

First, work through pages 15 and 16 and fill in boxes 8.1 – 8.5.Write a program to keep your tiger robot pacing in its cage. Make your robot move in astraight line across its square black tiger cage. It turns when it meets the edge of its cage thenkeeps on in a straight line until it meets another edge. Repeat this for ever.You will need to use a loop, but this time set it to Unlimited rather than a Count.

An Introduction to Programming using the NXT Robot 16 Exploring the LEGO® MINDSTORMS® Common Palette

8.58.4

Page 17: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Touch SensorThe back of your robot has a touch sensor, which will be pressed when it bumps in tosomething.

1. Start a New Program. Drag a Move block to the Start position and set it to move anUnlimited distance in reverse direction.

2. Drag a Wait for Touch block to your program.

3. Drag a green Sound block to your program.

4. Change the sound file it will play from Good Job to Ouch 02.

5. Drag another Move block to finish your program. This time move the robot forwardsfor 2 rotations. Download and run. The robot is going to reverse until it bumps something,so put it near a wall or hold a book up for it to bump into.

Task 9. Round the Garage Challenge

Get your robot to move backwards until it bumps into a box or pile of books on the floor.When it has bumped, get it to

move forward (not more than 40cm) turn a little go back on this new angle (not more than10cm) straighten up go backwards again until it bumps

It repeats these five steps forever. It will eventually work its way around the box.

An Introduction to Programming using the NXT Robot 17 Exploring the LEGO® MINDSTORMS® Common Palette

Page 18: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Ultrasonic SensorThe 'eyes' on your robot can detect objects as far as two and a half metres away. It uses anultrasonic sensor, like a bat or a radar. A signal is sent out, and if an object is in front, thesignal bounces back. The time taken for the signal to come back is a measure of the distancebetween the object and the robot.

1. Start a New Program. Drag a Move block to the Start position and set it to move anUnlimited distance forwards.

2. Drag a Wait for Distance block to your program.

3. Set it to Distance < 30 cm.

4. Drag a Move block and set it to Stop. Download, point your robot towards a wall, and run.You will have to be careful not to let the robot 'see' your hands when you press the runbutton. The robot may also see its own cables if they are in front of its 'eyes'.

Measure how far away your robot stops from the object it hasdetected.

Where should you measure from on the robot?

Task 10. Runaway Robot Challenge

Make your robot move forward slowly. When you move in front of it, it should turn a halfturn away from you and keep moving forward (away from you). Repeat these actions for 10seconds.

An Introduction to Programming using the NXT Robot 18 Exploring the LEGO® MINDSTORMS® Common Palette

Large sized objects with hard surfaces return the best readings. Objects made of soft fabricor that are curved [like a ball] or are very thin or small can be difficult for the sensor todetect. Two or more Ultrasonic Sensors operating in the same room may interrupt each other’sreadings. Source: http://mindstorms.lego.com/overview/Ultrasonic_Sensor.aspx

10.1

10.2

Page 19: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Loops and Switches

You have already used loops that never finish, loops that repeat a certain number of times, and loopsthat repeat for a number of seconds. Loops are a very useful way of repeating a code pattern. Usuallya 'stop condition' will stop the loop when something has been achieved (a length of time, a number oftimes, a sensor reading . . .)

Let's get some more practice with loops.

Task 11. Racing Robot

Racing Robot needs to:

walk to the starting line (a black stripe) steps 1 & 2 below

wait for the orange Enter button to be pressed step 3 below

race off to the finish line (another black stripe) step 4 below

beep and stop. step 5 below

1. Start by writing a robot walk program or opening the one you made from page 11.Don't make the steps too big – you'll see why shortly. Try 80 degrees.

2. If you haven't already, put the steps in a Loop. The robot needs to walk until it sensesthe start line, so make the Loop control Sensor > Light Sensor. Set the Loop to finishwhen the light sensor sees black.

The robot will now walk to the Start line.

3. Put another loop after the walk loop. Control thisloop by Sensor > NXT button. Choose the Enterbutton. You don't need to put anything inside thisloop. The robot will just wait for the button press.The next action the program needs is to race to the finish line.

4. Add another Loop to your program. Put an unlimited Move block inside the loop, andfinish the loop when the robot sees black. This is a race, so set the Power to 100.

5. Finish the program with a Beep and stop the wheels.

This program looks like it should do the job, but it has at least one and probably two seriousproblems. Before you read on, run the program and see if you can work out what the problems are.

An Introduction to Programming using the NXT Robot 19 Exploring the LEGO® MINDSTORMS® Common Palette

Page 20: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Fixing problem 1

6. Put another Loop in your program after the NXT button Loop. Control this loop withTime and give the robot long enough to get past the start line before it starts checkingfor black again. A setting of 0.5 seconds is plenty. There is still nothing inside the loop which will actually move the robot past the start line.

7. Put a Move block inside the loop and set it to Unlimited.

Fixing problem 2

8. Drag a Switch block and put it between the robot's 2 steps. A Switchgives the program a choice of 2 paths to follow.

9. Set this Switch block to be controlled by the Light Sensor. Make it switch for a lightlevel of < 35 (or whatever you setting you need to be sure of it being on black).The top path (sequence beam) is for the comparison being true (light level < 35), so the toppath is for what you want to happen if the robot is on black after the first step in its loop.(Nothing. It has found the start line and is ready to race.)The bottom path is for a robot which is still on white after its first step. You want it to takethe second step in the loop.

10. Drag the “second step”blocks to the bottom pathof the Switch.

The robot is less likely tomiss seeing (sensing) thestart line now.

11. Finish the Racing Robot challenge by making sure your robot takes steps small enough that it NEVER misses the start line putting a message on the screen saying “Enter to start” so that other people will know

what the robot is waiting for on the starting line. take this message off the screen when the race has started. You could replace it with

one which says “Racing now”

Extra challenges if you are keen:

The NXT stops well after the finish line because it makes thebeep before it puts the brake on. One way you can makethese things happen at the same time is by splitting thesequence beam into two paths which will be followed at thesame time. Drag the Move block down first, then hold theshift key down and draw the extra beam.

Make the NXT say Three Two One ( one number each second ) before it waits for theEnter button to be pushed. Should the numbers be in the loop or before it?

An Introduction to Programming using the NXT Robot 20 Exploring the LEGO® MINDSTORMS® Common Palette

Page 21: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Task 12. Yo-yo robot

This program moves the robot forward a little if there is nothing infront of it, and backwards if there is something in front of it.

1. Drag a Switch block to a new program.2. It will be showing a Touch sensor. Change this to the Ultrasonic

(distance) sensor.3. In the Configuration Panel, set the Ultrasonic sensor to Distance less than 30cm.

You will see there are 2 paths (MINDSTORMScalls them sequence beams) in the Switch block. Your program will choose which one to followdepending on the Distance setting on the Switchblock and the information received by the sensor. In this case, if the robot is less than 30cm from anobject, it will follow the top sequence beam.Otherwise it will follow the lower sequence beam.

4. Drag a Move block into the top sequence beam. Set it to move backwards 3 rotations.(If the robot is closer than 30cm to an object, it will move backwards.)

5. Drag a Move block into the lower sequence beam. Set it to move forward 1 rotation. (Ifthe robot is further than 30 cm away from an object, it will move forward 1 rotation.)There is one further step needed. The ultrasonic sensor is likely to see your hand as a closeobject when you push the run button.

6. Drag a Wait for Time block to the beginning of your program (before the Switch block).Set the time for long enough to get your hand away.Download. Put the robot near (and pointing towards) a wall and run the program. If yourrobot is very close to the wall, it should move backwards. Otherwise it will move forward.Shift the robot and run the program again, and again, and again . . .

7. Put this program in a loop so that the robot moves backwards and forwards forever.

An Introduction to Programming using the NXT Robot 21 Exploring the LEGO® MINDSTORMS® Common Palette

The Switch block can be thought of as an 'if ' then an 'else' : If the robot is closer than 30 cm it will go back, or else it will go forwards.

Page 22: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

Task 13. Line Follower Challenge

You can use the Switch block in a loop to make a your robot follow a black line. Theprocess can be described in word like this:Do this forever: If the sensor is on black, turn to the right.

If the sensor is on white, turn to the left.Write a program which uses a Switch block to make the robot follow a black line. The following tips may be helpful.

The safest way is to just use one wheel when making each turn. You will need to stop the other wheel first or it will keep on moving. Lower power settings will give you a more reliable line follower. Fast robots make

mistakes by overshooting the line, especially on curves. Try Power of 60.

Question: Is the robot following the middle of the line or the edge?

DisplayThe Display block lets you display images or text on the Robot's screen.

1. Drag a Display block to your program. Look at the Configuration Panel.If you don't change anything, it will choose aSmiley Face (which is saved in a file calledSmile 01), and will draw it with its bottom leftcorner at position X=12, Y=8 on the robot'sscreen. You may change the position by dragging the smiley facearound or typing new numbers for X and Y.If you increase the X value, the picture will move to theright on the screen. The maximum X value is 99.If you increase the Y value, the picture will move up onthe screen. The maximum Y value is 63.

You can also display Text (words) or a Drawing.If you want to put both text and an image on thescreen, you can do this by using a second Displayblock with its Clear field unchecked.

How MINDSTORMS uses these words:Image - what you might think of as a picture or a photographText - letters, words, numbers, symbols e.g. was 23 %Drawing - dots (points), lines and shapes e.g. circles, ovals, squaresGraphic - the Image files

An Introduction to Programming using the NXT Robot 22 Exploring the LEGO® MINDSTORMS® Common Palette

13.1

63

Y

0 0 99 X

Page 23: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

BlocksWhen you have a piece of program working, you can save it in a Block so that you can use it again.

1. Open your walking robot program (Walk.rbtfrom page 11) or write another one. It shouldlook something like this:

2. Drag your cursor over a rectangle which contains the whole program (see the Start andFinish positions in the picture below).

3. Select the Create My Block button from the Toolbar at the top of the program space.

A new window will appear.

4. Type a name for yourblock e.g. Walker.

5. Type a description ofwhat your block does e.g.walks 8 steps.

6. Click on Next and choosea picture for your block.

7. Click on Finish.

Your code sequence is now tidied away neatly into a block. You can use this block as often as youlike and in as many programs as you like.

1. Open a New Program. 2. Click on the Custom Palette button. 3. Click on My Blocks.

An Introduction to Programming using the NXT Robot 23 Exploring the LEGO® MINDSTORMS® Common Palette

Page 24: An Introduction to Programming using the NXT Robot · 2021. 8. 1. · An Introduction to Programming using the NXT Robot: exploring the LEGO® MINDSTORMS® Common palette. Student

4. Choose your block and drag it to your program.

You could put this block in a loop, or put 2 of them side by side . . .

If you want to make changes to your block, double-click on it to open it. If it is already open, just clickon its name tab above the program space. You must save any changes for them to have aneffect. Make sure you download your program, not your block - click on the program's tab beforeyou download.You can add and delete My Blocks from the CustomPalette by selecting the Manage Custom Palettecommand in the Edit menu.

Task 14. Blocked Up Challenge

Make the blocks inside the loop of your line follower program into a block. Using your line follower block and the walker block from the previous exercise, write a newprogram which follows the line for 3 seconds then walks for 8 steps, then follows the lineagain. Use only blocks and loops.

* * * * *

If you have worked through this book and completed all the tasks, well done! I hope youhave enjoyed your introduction to programming and robotics. What you do now is onlylimited by your imagination. Go and play!

An Introduction to Programming using the NXT Robot 24 Exploring the LEGO® MINDSTORMS® Common Palette

Sandy GarnerDepartment of Computer Science

University of OtagoDunedin

New Zealand