LEGO Mindstorms NXT 2.0 For Teens (2011)
LEGO Mindstorms NXT 2.0 For Teens (2011)
Lego Mindstorms
NXT 2.0 for Teens
Australia . Brazil . Japan . Korea . Mexico . Singapore . Spain . United Kingdom . United States
Lego MindstormsTM NXT 2.0 for Teens © 2011 Course Technology, a part of Cengage Learning.
Jerry Lee Ford, Jr. ALL RIGHTS RESERVED. No part of this work covered by the copyright
Publisher and General Manager, herein may be reproduced, transmitted, stored, or used in any form or
Course Technology PTR: Stacy L. Hiquet by any means graphic, electronic, or mechanical, including but not
limited to photocopying, recording, scanning, digitizing, taping, Web
Associate Director of Marketing:
distribution, information networks, or information storage and retrieval
Sarah Panella
systems, except as permitted under Section 107 or 108 of the 1976
Manager of Editorial Services: United States Copyright Act, without the prior written permission of the
Heather Talbot publisher.
Marketing Manager: Jordan Castellani
Senior Acquisitions Editor: Emi Smith For product information and technology assistance, contact us at
Project Editor: Jenny Davidson Cengage Learning Customer & Sales Support, 1-800-354-9706
Copyeditor: Sandy Doell For permission to use material from this text or product,
Technical Reviewer: JT Hiquet submit all requests online at www.cengage.com/permissions
Further permissions questions can be emailed to
Interior Layout Tech: MPS Limited, a Macmillan
[email protected]
Company
Cover Designer: Mike Tanamachi
Indexer: Sharon Shock Lego Mindstorms is a trademark of the LEGO Group. All other
trademarks are the property of their respective owners.
All images © Cengage Learning unless otherwise noted.
ISBN-13: 978-1-4354-5480-4
ISBN-10: 1-4354-5480-4
e-ISBN-10: 1-4354-5480-4
This book represents the hard work of a number of talented individuals to whom I
owe a great many thanks. For starters I want to thank Emi Smith, who as
acquisitions editor, helped make this book a reality. I especially need to express
my gratitude to Jenny Davidson, who served as this book’s project editor. As she
has demonstrated so many times in the past, Jenny’s guidance and editorial skills
were essential to the success of this book. Thanks also go out to this book’s
technical editor, JT Hiquet, for providing invaluable insights and technical advice
and to Sandy Doell, who served as the book’s copyeditor. Lastly, I’d be remiss if I
did not remember to thank all the people working behind the scenes at Course
Technology PTR for all their contributions and hard work.
About the Author
Jerry Lee Ford, Jr. is an author, educator, and an IT professional with over
21 years of experience in information technology, including roles as an auto-
mation analyst, technical manager, technical support analyst, automation engi-
neer, and security analyst. He is the author of 36 books and co-author of two
additional books. His published works include Getting Started with Game Maker,
DarkBASIC Programming for the Absolute Beginner, Scratch Programming for
Teens, Microsoft Visual Basic 2008 Express Programming for the Absolute
Beginner, and Phrogram Programming for the Absolute Beginner. Jerry has a
master’s degree in business administration from Virginia Commonwealth Uni-
versity in Richmond, Virginia, and he has over five years of experience as an
adjunct instructor teaching networking courses in information technology.
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
vi
Contents vii
Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Programming Palette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Configuration Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
NXT Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
My Portal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Robo Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Little Help Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Work Area Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Work Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Other NXT 2.0 GUI Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Calibrate Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Update NXT Firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Create Pack and Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Block Import and Export Wizard . . . . . . . . . . . . . . . . . . . . . . . . 34
Image Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Sound Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Managing NXT Brick Communication and Memory . . . . . . . . . . . . . 41
Managing Connections to NXT Bricks . . . . . . . . . . . . . . . . . . . . 41
Managing NXT Brick Memory . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Creating Your First NXT Program . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Connecting Your NXT to Your Computer . . . . . . . . . . . . . . . . . . . . 45
Downloading Your NXT-G Program . . . . . . . . . . . . . . . . . . . . . . . . 47
Running Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
The Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Color Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Touch Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Ultrasonic Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Using NXT Brick Buttons as Sensors . . . . . . . . . . . . . . . . . . . . . . 68
Servo Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Cables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Introduction
Welcome to Lego Mindstorms NXT 2.0 for Teens! Lego Mindstorms NXT 2.0 is a
programmable robotics kit made by Lego. Lego Mindstorms NXT 2.0 is the latest
version of this kit, released in August 2009. The kit is made up of a micro-
computer, electronic motors and sensors, as well as various Lego bricks and Lego
Technic pieces like beams, axles, and gears, 619 pieces in total.
The Lego Mindstorms NXT 2.0 kit comes equipped with everything you need to
build all sorts of robotic creations. It includes a software programming language
called NXT-G, which is used to write the programs that are downloaded into your
robotic creations in order to bring them to life. NXT-G is an icon-based, drag-
and-drop language, which means that there is no complex programming language
that must be mastered.
Until the advent of Lego Mindstorms, robotic development was the realm in
which only an exclusive group of highly educated and technically proficient
engineers and programmers were able to play. Lego Mindstorms NXT 2.0
changes all that. Lego Mindstorms NXT 2.0 provides you with a complete fra-
mework for robotic development. It does not require you to have an advanced
degree in computer science in order to use it. This book will provide you with
everything you need to know in order to get up and running with Lego Mind-
storms NXT 2.0 and NXT-G.
By following along with this book’s step-by-step, hands-on instruction, you will
learn how to create all sorts of robotic creations and to develop the program code
xii
Introduction xiii
that will make them work. Although Lego Mindstorms NXT 2.0 comes in both
retail and an education kit, this book will focus on robotic development using the
retail kit (set number 8527). This kit is available for purchase at www.lego.com or
at your local TOYS ’R US store. It costs around $280. The educational kit (set
number 9797) is almost identical to the retail kit, except that it includes a few
additional components.
Of course, Lego Mindstorms NXT 2.0 doesn’t limit you to the types of robotic
creations listed above. You can use it to create pretty much anything you can
imagine.
xiv Introduction
n Mac OS X 10.3
n Mac OS X 10.4
n Mac OS X 10.5
Although various versions of Microsoft Windows and Mac OS X are supported,
this book will demonstrate the execution of NXT-G software on a system running
Windows Vista. Regardless of which operating systems you use, the steps you
follow and the program code that is generated is the same.
In order to install and operate Lego Mindstorms NXT 2.0 on a Microsoft Windows
system, your computer must meet the minimum hardware requirements listed in
the following table. However, for good performance, you will want to make sure
that your computer exceeds these requirements.
xvi Introduction
If, on the other hand, your computer runs on Mac OS X, it must meet the
following requirements.
Trap
Areas where problems are likely to occur and advice on how to stay away from or deal with those
problems, hopefully saving you the pain of learning about them on your own the hard way.
Trick
Shortcuts designed to help make you a better and more efficient robotic developer.
CD-ROM Downloads
If you purchased an ebook version of this book, and the book had a companion
CD-ROM, you may download the contents from www.courseptr.com/downloads.
This page intentionally left blank
Chapter 1
1
2 Chapter 1 n Lego Mindstorms NXT 2.0 Overview
Hint
If you want to learn more about the Robotics Invention System and the previous version of Lego
Mindstorms NXT, visit http://en.wikipedia.org/wiki/Robotics_Invention_System and http://en.
wikipedia.org/wiki/Lego_Mindstorms_NXT.
Lego Mindstorms NXT 2.0 was released by Lego in August 2009. Though similar
to the previous version, a number of major improvements were made, and some
pieces were changed. The version 2.0 set no longer comes with a sound sensor or
a light sensor. Instead, it has two touch sensors and a new color sensor. The
color sensor supports three functions. Not only can it detect different colors, but
it can also serve as both a lamp and a light sensor. In addition, the Mindstorms
NXT 2.0 software has been enhanced. It now has new sound and image editors
and a remote control application.
The sound editor allows you to convert sound files on your computer into a
format supported by the NXT Brick and then to download them. Downloaded
sound files can be played through the NXT Brick’s built-in speaker. This way
you can make your robots play all kinds of sound effects or even talk. The image
editor lets you convert and download graphic files onto your NXT, where they
can then be loaded and displayed on the brick’s LCD screen. The remote control
Getting to Know Lego Mindstorms NXT 2.0 3
application allows you to control your robotic creations from your computer.
On top of all this, there is a new packaging tool that helps make sharing NXT-G
programs a lot easier.
Hint
If you are feeling nostalgic, you can purchase the Robotics Invention System online via eBay for
roughly the same price that the Mindstorms NXT 2.0 kit sells for today.
Servo Motors
NXT Brick
The Lego Mindstorms NXT 2.0 kit also consists of a large collection of Lego
pieces, including gears, beams, pegs, axles, and so on, as depicted in Figure 1.2.
Figure 1.2
An example of the many different types of pieces included in the Lego Mindstorms NXT 2.0 kit.
Figure 1.3
The Mindstorms NXT 2.0 IDE consists of an assortment of different specialized windows.
6 Chapter 1 n Lego Mindstorms NXT 2.0 Overview
Hint
You will learn more about the Mindstorms NXT 2.0 IDE in Chapter 2, “Getting Started.”
Using the IDE, you can create and edit NXT-G programs and download them to
your NXT Brick. In addition, the IDE contains features like the Robo Center
window, which provides easy access to building instructions for creating differ-
ent types of robots and the My Portal window that provides access to all kinds of
online resources and helps Mindstorms NXT developers to stay in touch with
the latest happenings in the Mindstorms NXT community.
The software also includes an editor that allows you to record sounds on your
computer that can then be used by programs in the NXT Brick and an editor
that can be used to create, edit, and download graphics to the NXT Brick.
Figure 1.4
NXT-G programs consist of various combinations of program blocks.
Installing the Mindstorms NXT 2.0 Software 7
Step-by-Step
1. Insert the Lego Mindstorms NXT 2.0 CD-ROM into your computer’s CD-ROM drive.
2. The window shown in Figure 1.5 should appear within a few moments. If it does not,
locate the autorun.exe file on the CD-ROM and double click on it. Select your preferred
language.
Figure 1.5
Beginning the installation procedure for Lego Mindstorms NXT 2.0 on Microsoft Windows.
4. The next window that appears specifies the features being installed and the directory into
which the application files are to be copied. Click on Next to accept the default settings.
5. The Lego Mindstorms NXT Software license agreement is displayed next. Review the
terms of the agreement and then click on the I accept the License Agreements(s) option
and click on Next.
7. Over the next several minutes, the Lego Mindstorms NXT 2.0 application files are copied
onto your computer. Once complete, you’ll see a window prompting you to view the
application’s Readme file and to register the software with Lego.
Hint
You can view the Readme file at any time by clicking on Start > All Programs > Lego Mindstorms
NXT 2.0 > Read Me. You can register your copy of the software any time by clicking on Help >
Register Product.
Once installed, you can start Mindstorms NXT 2.0 by clicking on Start > All
Programs > Lego Mindstorms NXT 2.0 > Lego Mindstorms NXT 2.0.
Step-by-Step
1. Insert the Lego Mindstorms NXT 2.0 CD-ROM into your computer’s CD-ROM drive.
2. Locate and open the Mindstorms NXT folder and double click on the Install file.
3. The Lego Mindstorms NXT v2.0 window appears. Select your preferred language.
5. The Lego Mindstorms Read Me file is displayed. Review its content and then click on
Continue.
6. The Lego Mindstorms NXT Software license agreement is displayed. Review its content
and then click on Continue.
7. When prompted, click on Agree to accept the terms of the license agreement.
8. You are then prompted to specify the location where you wish to install the application.
After specifying an appropriate location, click on Continue.
10. If prompted, supply a password and click on OK in order to continue the installation.
11. Over the next several minutes, the Lego Mindstorms NXT 2.0 application files are copied
onto your computer. Once this has completed, you’ll see a window prompting you to
restart your computer. Click on Restart.
Once installed, you can start Mindstorms NXT 2.0, as shown in Figure 1.6, by
opening the Applications folder followed by the Lego Mindstorms NXT
subfolder and then double clicking on the Mindstorms NXT icon.
10 Chapter 1 n Lego Mindstorms NXT 2.0 Overview
Figure 1.6
Mindstorms NXT 2.0 on Mac OS X looks, feels, and operates very much the same as it does on Microsoft
Windows.
Building Robots
The first step in building a new robot is to sit down and build it. One way of
going about this is to build the robot based on a set of existing building
instructions. The Mindstorms NXT 2.0 set comes with a Quick Start Guide that
walks you through the creation of a robotic vehicle. The Mindstorms NXT
software’s Robo Center includes instructions for creating four additional
robots. Instructions for building additional robots can be found online at
http://mindstorms.lego.com. You will also find detailed instructions for building
several robots in this book. Last but not least, you can use your own imagination
to build anything you can think of.
Figure 1.7
NXT-G programs consist of various combinations of program blocks that are configured to perform
specific tasks.
Sidebar
NXT-G may be the programming language that Lego supplies with the Lego Mindstorms NXT 2.0
kit, but it is only one of many programming languages that support NXT program development.
The following is a partial listing of NXT-compatible programming languages.
n RobotC—http://www.robotc.net
n Ruby-nxt—http://rubyforge.org/projects/ruby-nxt/
n pbLua—http://www.hempeldesigngroup.com/lego/pbLua/
n Lego:NXT—http://search.cpan.org/~collins/LEGO-NXT-2.00-1/
n Lego.NET—http://www.dcl.hpi.uni-potsdam.de/research/lego.NET/
n NXTþþ—http://nxtpp.clustur.com/
n RoboRealm—http://www.roborealm.com/
The Three Steps in Building Robots 13
n SqLego—http://wiki.squeak.org/squeak/2412
n NXT_Python—http://code.google.com/p/nxt-python/
Hint
The NXT Bricks can use Bluetooth to upload programs and files from your computer. NXT Bricks
have an approximate range of 33 feet (10m). In addition, you can use it to establish wireless
communication with other Bluetooth-enabled devices, including Bluetooth-enabled cell phones or
other NXT robots.
This book will explain how to use the Lego Mindstorms 2.0 kit’s USB cable to
establish communication between the NXT Brick and the computer. If your
computer has Bluetooth capability and you want to use that instead, refer to the
Lego Mindstorms User Guide supplied as part of the Lego Mindstorms NXT 2.0
kit for detailed instructions on how to work with it.
Figure 1.8
The Lego Mindstorms web site is the focal point of all activity within the user community.
Joining the Lego Mindstorms Community 15
Figure 1.9
The FIRST LEGO League provides a great platform for young people to get involved in the Lego
Mindstorms Community.
The Lego Mindstorms community is a very active group. In addition to the Lego
Mindstorms web site, there are many other web sites and organizations
dedicated to Lego development. One such group is the FIRST LEGO League
or FLL. The FLL, shown in Figure 1.9, is a worldwide robotic design competition
started in 1999 for the purpose of getting children interested in science and
16 Chapter 1 n Lego Mindstorms NXT 2.0 Overview
technology. The FLL has grown in popularity to include over 140,000 people in
56 countries around the world. To learn more about it visit http://www.
firstlegoleague.org.
Hint
See Appendix B for a listing of other web sites dedicated to Lego Mindstorms development.
Summary
As this chapter explained, thanks to the Lego Mindstorms NXT 2.0 kit, robotic
development is no longer the exclusive domain of large companies and advanced
machinists, engineers, and programmers. This chapter provided you with an
overview of Lego Mindstorms NXT 2.0, covering its history, major components,
and the three steps involved in building robots. You learned how to install Lego
Mindstorms NXT 2.0 on both Windows and Mac OS X computers. You also
learned about the Lego Community and the FIRST LEGO League.
Chapter 2
Getting Started
17
18 Chapter 2 n Getting Started
let you define constants and create variables, which you will learn all about in
Chapter 8, “Advanced Programming Techniques.”
The Tools menu provides access to a collection of specialized windows, each of
which is designed to perform a specific task. These windows include:
n Calibrate Sensors—Facilitates the re-calibration of sensors to tune their
operations.
n Update NXT Firmware—Provides the ability to update the NXT Bricks
firmware to the most current version, allowing you to take advantage of
any improvements made by Lego.
20 Chapter 2 n Getting Started
Figure 2.2
The Lego Mindstorms NXT File menu provides access to commands that manage program files.
Navigating the Mindstorms NXT 2.0 GUI 21
n Create Pack and Go—Creates a Pack and Go file facilitating the devel-
opment functions like My Blocks, Display Blocks, and Sound Blocks with
other NXT developers.
n Block Import and Export Wizard—Provides the ability to import new
programming blocks and to export custom developed programming
blocks.
n Image Editor—Provides the ability to create and edit graphic and text
files, which can then be downloaded and displayed on the NXT Brick’s
LCD.
n Remote Control—Provides you with the ability to remotely control your
robotic creations, controlling both speed and direction.
n Sound Editor—Provides the ability to create and edit sound files that
can be downloaded and played by the NXT Brick.
The Help menu contains commands that provide access to all the online help
resources listed below.
n Contents and Index
n Online Support
n Online Updates
n Register Product
n About LEGO MINDSTORMS NXT
Toolbar
The Mindstorms NXT 2.0 toolbar, shown in Figure 2.3, provides single click
access to commonly used commands and windows. Using the toolbar you can
create, open, and save program files. You can also cut, copy and paste, and work
with various tools. Icons located on the right side of the toolbar provide access to
the Robo Center and My Portal windows.
Programming Palette
The Programming palette contains a collection of programming blocks or icons
that are used in the creation of NXT-G programs. The programming blocks are
22 Chapter 2 n Getting Started
Pointer Tool
Pan Tool
Comment Tool My Portal
Create My Block Robo Center
organized into three tabs, located in the bottom left corner of the Mindstorms
NXT 2.0 GUI. These tabs are as follows:
n Common Palette—Contains the most commonly used programming
blocks.
n Complete Palette—Provides access to all programming blocks.
n Custom Palette—Provides access to blocks that you create or upload into
Mindstorms NXT 2.0.
Figure 2.4 shows the Complete palette. The Action block group is selected,
revealing all of the programming blocks that it comprises. To use a block, click
on it and then drag and drop it onto the Work Area.
Hint
You will learn all about Mindstorms NXT 2.0 programming blocks in Part III, “NXT-G Programming.”
Configuration Panel
The Configuration Panel is displayed in the bottom left corner of the Mind-
storms NXT 2.0 GUI whenever a programming block is added to or selected in
Navigating the Mindstorms NXT 2.0 GUI 23
Figure 2.4
The Complete palette provides access to all of Mindstorms NXT 2.0’s programming blocks.
Figure 2.5
The Configuration Panel is automatically displayed whenever you add or select a programming block
onto the Work Area.
the Work Area. The appearance and functionality of the Configuration Panel
varies, based on the currently selected programming block. Figure 2.5 shows
how the Configuration Panel looks when the Sound block is selected.
Using the Configuration Panel, you can customize the operation of the
programming block, modifying both input and output and other attributes
and actions.
NXT Controller
The NXT Controller provides you with the ability to communicate with the
NXT Brick. It is used to transfer program and data files to your NXT Brick. It
24 Chapter 2 n Getting Started
Download Stop
also enables you to start and stop programs and to change NXT Brick settings.
The NXT Controller consists of the following five button controls, shown in
Figure 2.6.
n NXT Window—Displays the NXT Window, which is used to set up a
connection to a NXT Brick, change its name, view battery, connection,
storage, and firmware version and to manage NXT Brick memory usage.
n Download and run selected—Downloads and executes the selected por-
tion of the current NXT-G program file to your NXT Brick.
n Download and run—Downloads the current NXT-G program file to
your NXT Brick and executes it.
n Download—Downloads the current NXT-G program file to your NXT
Brick.
n Stop—Halts the execution of the current NXT-G program.
My Portal
A big part of the philosophy behind Mindstorms NXT 2.0 is based on sharing
and participating in the Lego Mindstorms community. The My Portal window,
shown in Figure 2.7, provides a gateway to the http://www.mindstorms.com web
site. The My Portal window shares space with the Robo Center. You also need
Internet access to take advantage of this feature. To access it, you must click on
the My Portal icon located on the left side of the Mindstorms NXT 2.0 toolbar.
You can use the My Portal window to access information on new models,
program files, sound and image files, and wall papers. You can also use it to
learn new tips and tricks.
Navigating the Mindstorms NXT 2.0 GUI 25
Figure 2.7
The My Portal window provides direct access to all kinds of resources that you can use in your robotic
creations.
Robo Center
The Robot Center is located on the right side of the Mindstorms NXT 2.0 GUI.
It shares space with the My Portal window. As shown in Figure 2.8, the Robo
Center contains complete step-by-step instructions for creating four different
robots. These projects are outlined below.
n Vehicles—Complete instructions for building the Shooterbot robot
n Animals—Complete instructions for building the Robogator robot
n Machines—Complete instructions for building a Color Sorter robot
n Humanoids—Complete instructions for building the Alpha Rex robot
Figure 2.8
The Robo Center window provides you with access to building instructions for four different robots.
Figure 2.9
The Little Help window automatically displays help information based on the currently selected object.
displayed when the upper tab, identified by a question mark, is selected. To work
with this window, shown in Figure 2.9, all you have to do is move the cursor
over an object in the work area. In response, the Little Help window displays
information about the object as well as a link to additional information. To see
the additional information, click on the More Help >> link displayed at the
bottom of the Little Help window.
When you click on the More Help >> link, Mindstorms NXT 2.0 displays the
corresponding help file using your default web browser. For example, Figure 2.10
Navigating the Mindstorms NXT 2.0 GUI 27
Figure 2.10
The help file for the Sound Block provides extensive information for that block.
shows the help information that is displayed if you click on the More Help >> link
when the Sound block is selected.
Figure 2.11
The Little Help window helps you to move around large program files quickly.
the Magnifying Glass icon. As demonstrated in Figure 2.11, the Work Area Map
allows you to click and hold down the left mouse button on any area within the
window and then move the cursor to a new area. As you do, the Work Area
mirrors your actions, displaying whatever content is moved into focus within the
Work Area Map. (The in focus position of the Work Area is identified within
the Work Area Map by a white background.)
Work Area
The Work Area, shown in Figure 2.12, is the large gray gridded area located just
to the right of the Programming palette. This area is boundless, meaning that it
has no defined limits. It expands and contracts as necessary to allow for NXT-G
programs of any size. The Work Area is tabbed, allowing you to create or open
more than one NXT-G program at a time. To switch between programs all you
have to do is click on their respective tabs.
As you will learn in later chapters, NXT-G programs are created by dragging
and dropping different programming blocks onto the Work Area Map. As
shown in Figure 2.12, every new NXT-G program starts out with a Starting
Point sequence beam. To the right of the Starting Point is the word Start,
enclosed within four small bracket markers, which identify where the program’s
first programming block must be placed.
Trick
You can make additional room available when developing NXT-G programs by temporarily hiding
the Robo Center/My Portal windows. To do so, click on the orange Close button located on the far
right side of the toolbar. This will remove the window that is visible, enlarging the amount of space
allocated to the Work Area. To redisplay either of these two windows, all you have to do is click on
its toolbar icon.
Other NXT 2.0 GUI Windows 29
Figure 2.12
The Work Area is where you develop your NXT-G programs using Programming clocks.
Calibrate Sensors
The operation of your robotic creations can be affected by the conditions in
which they operate. Specifically, changes in environmental lighting and sound
may affect the way your robotic creations work. Because of this, your robotic
creations may work differently when you move them from one location to
another.
To manage the effects that different environmental conditions might have, you
can calibrate the NXT sensors to specific environments, allowing your robotic
creations to perform optionally in different environments. There are two ways of
calibrating sensors. One is through the Calibrate sensor window, which is
30 Chapter 2 n Getting Started
Figure 2.13
You can calibrate the sensors connected to your NXT Brick from this window.
accessed via the Tools menu. The other option is to use Calibration blocks
within your NXT-G programs.
In order to calibrate sensors, you must connect your NXT Brick to your
computer and turn it on. In addition, you must connect the appropriate sensor
to the brick. Next, click on Tools > Calibrate sensors. The Calibrate sensor
windows shown in Figure 2.13 will appear.
Hint
Neither the light nor the sound sensors are supplied with the Lego Mindstorms NXT 2.0 kit. These
sensors can be purchased separately from http://shop.lego.com.
Trap
If the window is grayed out, make sure that your NXT Brick is powered on. In addition, make sure
that it is connected to your computer.
Figure 2.14
This window allows you to check for and apply firmware updates to your NXT Brick.
made available. These firmware updates may add new features and capabilities
to your NXT Brick or may fix firmware bugs. To update the firmware on your
NXT Brick, click on Tools > Update NXT Firmware. The Update NXT
Firmware window appears as shown in Figure 2.14.
To determine if any firmware updates are available, click on the Check button.
This will open your default Web browser and display a page on the Lego
Mindstorms web site. Click on the Firmware link that is displayed on this page
to view the most recent firmware update as demonstrated in Figure 2.15.
Click on the Downloads link to initiate the download of the most current
firmware update. Next, click on the Download button that appears and save the
download to your computer. Save the download file to one of the following
locations.
Microsoft Windows:
C:\Program Files\LEGO Software\LEGO MINDSTORMS NXT\engine\Firmware
Other NXT 2.0 GUI Windows 33
Figure 2.15
The Firmware page lists the current and most recent firmware updates that are available.
Mac OS X:
Macintosh HD:Applications:LEGO MINDSTORMS NXT:engine:Firmware
Once the download is complete, return to the Update NXT Firmware window
and select the most recent firmware update from the Available Firmware Files:
list. Then click on the Download button. You can track the progress of the
download and installation of the firmware update by keeping an eye on the
progress bars located at the bottom of the window. Once all three progress bars
have turned green, the firmware upgrade process is complete. Click on the Close
button.
34 Chapter 2 n Getting Started
Figure 2.16
Creating a Pack and Go function that you can share with other Lego Mindstorms developers.
Figure 2.17
This window allows you to import third-party programming blocks.
Next, click on the Browse button and specify the location of the folder
containing the new blocks. A list of available blocks should be displayed. Select
the programming block(s) you want to import and click on the Import button.
Image Editor
Your NXT-G programs can display both text and graphics in the NXT Brick’s
LCD display. You can view and edit any of the graphic files that are supplied
with the NXT software, or you can create your own graphics from scratch using
the Image Editor, as demonstrated in Figure 2.18. The Image Editor works with
any of the following graphic file types:
n .jpg
n .bmp
n .png
n .ric
To access the Image Editor window, click on Tools > Image Editor. To open an
existing graphic, click on the Open button. To clear the drawing area, click on
the Clear button. To create or edit a graphic, you can use any of the drawing
36 Chapter 2 n Getting Started
Fill
Line
Pencil
Rectangle
Ellipse
Select for
Clipboard
Small, Medium,
Large
Undo
Redo
Text Erase
Figure 2.18
The Image Editor is used to develop graphics for display by your NXT-G applications.
tools displayed on the left side of the window. These tools work like their
counterparts in any typical paint or drawing program.
The NXT Brick’s LCD display is 100 pixels wide by 64 pixels tall. The grid
system display on the Image Editor window is also 100 64. The preview pane
on the right side of the window shows how the current graphic will appear when
displayed in the NXT Brick’s LCD window. Once you have finished creating or
modifying a graphic, you can save your work by clicking the Save button.
Trick
By default the NXT software saves graphics as .ric files. If you prefer, you can save graphics using a
different file format; just specify a .png, .bmp, or .jpg file extension as part of the filename.
Remote Control
The Mindstorms NXT 2.0 GUI includes a built-in remote control feature that
you can use to steer your robotic creation and perform one predetermined
action. You can also set and change speed. Remote Control is accomplished
from the Remote Control window, shown in Figure 2.19. To access this window,
click on Tools > Remote Control.
Other NXT 2.0 GUI Windows 37
Figure 2.19
This window enables you to control your robotic creations remotely from your computer.
The Remote Control window is well suited to controlling robotic creations that
use two server motors to control movement and the third server motor to
perform an action. You can execute remote control in one of two ways. From the
Remote Control window, you can:
n Click on the graphical arrow keys to set direction of the two servo
motors used for movement.
n Click on the Action button to perform an action with the third servo
motor.
n Click on the Plus and Minus button to increate or decrease the speed of
the servo motors.
n Click on one of the eight speed indicator meter settings to adjust speed
more than one level at a time.
Alternatively, when the Remote Control window is open, you can control your
robotic creations using the keyboard shortcuts outlined in Table 2.2. The
advantage of using the keyboard to remotely control your robotic creations is
that with the keyboard you can push two buttons at the same time. As a result,
you can execute gradual turns, giving you refined control over the movement of
your robotic creations.
In order to control your robotic creation, you must establish a connection to its
NXT Brick. This is done from the NXT window, which you can access by
38 Chapter 2 n Getting Started
þ Increase speed
- Decrease speed
A Increase speed
Z Decrease speed
1 Adjust speed to level 1
2 Adjust speed to level 2
3 Adjust speed to level 3
4 Adjust speed to level 4
5 Adjust speed to level 5
6 Adjust speed to level 6
7 Adjust speed to level 7
8 Adjust speed to level 8
Space Bar Execute the Action server motor
Figure 2.20
The Motor Configuration window allows you to configure and fine-tune the remote execution of servo
motors.
Configure the direction at which the third servo motors moves by clicking on
the appropriate radio button in the Direction: section located under the Action:
section. When you’re done, click on the checkmark button to save your
configuration changes, or click on the X button to cancel your changes.
Sound Editor
You can enhance your NXT-G programs through the addition of sound effects
that are played by the NXT Brick. Using the Sound Editor window, shown in
Figure 2.21, you can record and create your own sound files or edit any of the
sound files that come with the Mindstorms NXT 2.0 software.
Sound files played on the NXT Brick must be no more than five seconds long.
The following procedure outlines the steps involved in creating or editing a
sound file.
1. Click on Tools > Sound Editor. The Sound Editor window appears.
2. Click on the Open button to select an existing sound file, or click on the
Record button to record a new sound file.
3. Although the Sound Editor can open or record audio files that are as
long as 10 seconds, it can only save files that are no more than 5 seconds
long. The wave graphic used to represent the sound file is displayed in
40 Chapter 2 n Getting Started
red if the file is longer than 5 seconds. Wave files of less than 5 seconds
in length are shown in green.
4. Click on the Save button to save a sound file that is less than 5 seconds
long and specify a name for the file when prompted. If the sound file is
greater than 5 seconds, use the two slider controls located at each end of
the Sound Editor window to mark the beginning and end of a segment
of the sound file. If the segment that is cropped is less than 5 seconds
long, the wave graphic changes to green, signaling that it can be saved.
Click on the Save button to save the cropped portion of the sound file
and specify a name for the file when prompted.
If you want, you can also configure the value at which the NXT Brick will play
the sound file by moving the Adjust playback volume slider left or right as
desired. Any sound files that you create will automatically appear in the list of
sound files available to your NXT-G programs.
Hint
Sound file volume can also be configured using the Sound programming block, overriding any
settings you may specify in the Sound Editor.
Managing NXT Brick Communication and Memory 41
Figure 2.22
You can manage communication with NXT Bricks from the Communications tab.
42 Chapter 2 n Getting Started
Figure 2.23
You can manage the files stored on your NXT Brick from the Memory tab.
connection to an available NXT Brick, select it from the list and click on the
Connect button. To disconnect a connection, select it and click on the Remove
button. Removed connections can always be added back later.
Hint
Removing all system files from your NXT Brick won’t prevent it from operating. If necessary, you
can reinstall these files by restoring your NXT Brick to its factory configuration, as described later in
this chapter.
Creating Your First NXT Program 43
NXT Brick comes equipped with 130.8 KB of memory. However, much of this
memory is used to store the NXT Brick’s system software, which consists of
things like example programs, graphic files, and sound files. As a result, you are
left with somewhere around 55 KB of available memory to work with. As time
passes and you download more and more NXT-G programs, graphic files, and
sound files into your NXT Brick, memory can become scarce. From time to time
you will need to remove files to make room for new files.
Figure 2.24
Assigning a name to a new NXT program.
program is empty. To add the programming logic needed to make your program
work, you must add programming blocks to the program.
Begin by dragging and dropping an instance of the Display block from the
Programming palette onto the Programming Area. This block provides the
ability to display text or graphics within the NXT display window. When you
release the Display block, it is automatically connected to the Start position.
Your next step is to configure the Display block, instructing it to display a text
string. To do so, select the Text option from the drop-down list located in the
Configuration Panel and then type Hello World! in the Simple Text field, as
shown in Figure 2.25.
Note that as you type in the text, a depiction of it is displayed to the right,
showing how the text will appear once it is displayed in the NXT. Next, drag and
drop a Time block from the Programming palette onto the Programming area.
By default, it will automatically snap into place just to the right of the first block.
Configure the Time block to pause execution for five seconds, as shown in
Figure 2.26.
Next, let’s modify the program to display a graphical smiley face. NXT-G
provides you with access to different graphic files, all of which can be displayed
on the NXT’s LCD. To set this up, select the Image option from the drop-down
list located in the Configuration Panel and then select Smile 01 from the list of
graphic files shown at the bottom of the Configuration Panel. A depiction of the
smiley face graphic is displayed in the preview window to the right, showing
you how it will appear when displayed by the NXT Brick, as demonstrated in
Figure 2.27.
To finish off the program, you need to add a second Time block. Drag and drop
a Time block from the Programming palette onto the Programming area. It
Connecting Your NXT to Your Computer 45
Figure 2.25
A graphic T character is displayed in the lower right corner of the Display block indicating its content.
should automatically snap into place just to the right of the third block.
Configure the Time block to pause execution for five seconds, as shown in
Figure 2.28.
At this point your new NXT-G program is complete. In order to test it, you must
establish a connection to your NXT, download, and then run it.
Figure 2.26
The Time block has been configured to delay program execution for five seconds.
computer. To turn on the NXT Brick, press its (orange) Enter button. In
response, the NXT Brick powers on, displaying the Lego logo. This is followed
by the display of the Mindstorms icon and the playing of a sound that signals
that the NXT Brick has finished powering up. The main menu is then displayed.
In a few moments, your computer should recognize and install your NXT,
displaying a message telling you that new hardware is ready for use. At this point
you should be ready to download your new program into your NXT Brick.
Trick
If your computer supports Bluetooth, you can use it in place of the USB cable to connect to your
NXT Brick. Instructions for doing so are provided in the Lego Mindstorms User Guide that comes
with the Lego Mindstorms NXT 2.0 kit.
Downloading Your NXT-G Program 47
Figure 2.27
The program is currently configured to display a text message, pause five seconds, and then display a
smiley face.
Figure 2.28
The second Time block has also been configured to delay program execution for five seconds.
Hint
If you run into any problems with the download process, make sure your NXT Brick has not
automatically powered itself off. If it has, turn it back on and try again. If that does not fix things,
double-check its connection and make sure that the USB cable has not become disconnected from
your computer or NXT Brick.
Figure 2.29
Status information is updated as the download process occurs.
1. Pressing the orange button the first time opens the My Files folder,
which is the default item on the NXT Brick’s main menu.
2. Pressing the orange button a second time selects the Software Files
option.
3. Pressing the orange button a third time, by default, selects the most
recently downloaded program.
4. Pressing the orange button a fourth time executes the selected program.
Summary
This chapter provided you with a detailed review of the menus, toolbar, and
windows that make up the Mindstorms NXT 2.0 GUI. You learned how to
calibrate sensors, update your NXT Brick’s firmware, work with the Image
Editor and Sound Editor, remotely control your robotic creations, manage
connections to the NXT Brick, and manage the NXT Brick’s memory. Finally,
you learned how to create your first NXT program, connect your NXT Brick to
your computer, download your NXT-G program into your NXT Brick, and
execute the NXT-G program.
This page intentionally left blank
Chapter 3
The Lego Mindstorms NXT 2.0 kit comes with 619 different pieces and parts. Of
these, the NXT Brick and the servo motors and sensors represent the critical
components in any robotic creation. Therefore, it is important that you have a
good understanding of each of these components and how to work with them.
In this chapter you will learn about each of the electronic components supplied
in the Lego Mindstorms NXT 2.0 kit, and you will learn about their uses and
capabilities.
The major topics covered in this chapter include:
n A detailed examination of the features of the NXT Brick
n A review of the NXT Brick’s menu system
n A detailed examination of each of the sensors
n A detailed examination of the features and capabilities of the servo motors
n An examination of the cables used to connect the NXT Brick to sensors
and motors
51
52 Chapter 3 n Working with Electronic Components
USB Port
Motor Ports (A, B, C)
LCD Display
Speaker
Left Button Right Button
On/Enter Button
Clear/Go Back Button
the Lego Mindstorms User Guide, the NXT Brick is an “intelligent computer-
controlled Lego brick.” As shown in Figure 3.1, the front of the brick is off-white
in color and its top, bottom, and back sides are gray.
As shown in Figure 3.1, the NXT Brick has numerous connection ports, buttons,
an LCD, and a speaker.
Hint
If you prefer a different color scheme, you may want to purchase the black version of NXT Brick. As
of the writing of this book, Lego had released a special 10th anniversary edition of the NXT Brick
dubbed the Black NXT Limited Edition (see Figure 3.2). This item was not yet available in the United
States. When it is, you will be able to purchase it at http://shop.lego.com. There is no word on how
long the black version will remain available. However, thanks to eBay and other similar venues, you
should be able to find one if you are really interested.
The NXT Brick is powered by six AA batteries that go in the back of the NXT
Brick and provide it with power, which is, in turn, also used to power the motors
and sensors. If you prefer, you can purchase the rechargeable lithium battery
pack shown in Figure 3.3 by visiting http://www.shop.LEGO.com and doing a
The NXT Brick 53
Figure 3.2
The black version of the NXT Brick.
search on Rechargeable Battery. As of the writing of this book, this part sold for
$54.99.
Hint
The NXT Brick is a remarkably reliable computer. However, like all computers, the NXT Brick may
experience occasional problems. If it becomes unresponsive, check its battery level indicator,
located in the upper right corner of the NXT Brick’s display, to ensure that it does not need new
batteries. If your NXT Brick simply stops executing and does not respond to your commands, it may
be frozen. If this occurs, look at the upper right corner of the NXT Brick’s display, just to the left of
the battery icon, to see if the NXT icon is spinning. If it is not, you will have to reset the NXT Brick,
as explained in Chapter 2, “Getting Started.”
54 Chapter 3 n Working with Electronic Components
Figure 3.3
Over time the lithium rechargeable battery pack can save you money.
Technical Specifications
A breakdown of the technical specifications of the NXT Brick is provided in the
following list:
n 32-bit ARM7 Microcomputer
n 256 KB Flash, 64 KB RAM
n 8-bit AVR Microcontroller
n 4 KB Flash, 512 bytes of RAM
n Bluetooth Wireless (Class II V2.0)
n USB port (12 MB)
n 4 Input Ports
The NXT Brick 55
n 3 Output Ports
n 100 64-pixel LCD
n Speaker—8 kHz Sound Quality
n Power: 6 AA Batteries
As you can see, the NXT Brick lacks the processing power of a typical desktop
computer. However, it is perfectly suited to what it has been designed to do:
provide intelligence and control over robotic creations.
Primary Features
The NXT Brick is packed with an assortment of features and controls, including:
n Three output ports and a USB port. Provide connections to servo
motors and to your computer.
n Four input ports. Provide connections to the sensors.
n A speaker. Plays sound files.
n Four NXT button controls. Used to interact with the NXT Brick menu
system.
n An LCD display window. Displays text and graphics.
These features and controls are explained more fully in the sections that follow.
Output/USB Ports
As shown in Figure 3.4, there are three motor ports located at the top of the
NXT Brick. Using the cables that are supplied in the Lego Mindstorms NXT 2.0
kit, you connect the kit’s servo motors to these ports, which are labeled A, B,
and C.
Next to the three motor ports is a USB port, which is used to establish a USB
connection between the NXT Brick and your computer using the USB cable
included with the Lego Mindstorms NXT 2.0 kit. Alternatively, you can use the
NXT Brick’s built-in Bluetooth capability to wirelessly connect to your com-
puter. To use Bluetooth, your computer must have either built-in Bluetooth or a
Bluetooth adapter. For a list of compatible Bluetooth adapters, go to http://www.
mindstorms.com/Bluetooth. You can also purchase the Bluetooth adapter shown
56 Chapter 3 n Working with Electronic Components
USB Port
Motor Port C
Motor Port A
Motor Port B
Figure 3.4
There are three connectors for servo motors and a USB connection at the top of the NXT Brick.
in Figure 3.5 at the Lego online store, currently for $37.99. To do so, go to http://
www.lego.com and search on Bluetooth Part # 9847.
Input Ports
In addition to the motor ports and USB ports located at the top of the NXT
Brick, there are also four sensor ports located at the bottom of the NXT Brick.
As shown in Figure 3.6, these ports, labeled 1, 2, 3, and 4, are used to connect
sensors to the NXT Brick.
Although you can connect any sensor to any sensor port when building your
robotic creations, the Try Me programs that you can use to experiment with and
text sensors require that the sensors be connected to their default ports. Table 3.1
lists the NXT Brick’s default sensor port assignments.
Speaker
The NXT Brick has a built-in speaker through which it can communicate by
playing different sounds through four small slits located on the right side of the
NXT Brick.
Figure 3.5
Bluetooth capability enables wireless communication between your computer and the NXT Brick.
Sensor Port 1
Sensor Port 2
Sensor Port 4
Sensor Port 3
Figure 3.6
These ports are used to connect sensors to the NXT Brick.
58 Chapter 3 n Working with Electronic Components
1 Touch
2 Touch
3 Color
4 Ultrasonic
NXT Brick
Name
USB Icon NXT Icon
Figure 3.7
The NXT Brick’s LCD display is able to display three menu options at a time.
Figure 3.8
The NXT Brick indicates its Bluetooth status by way of a Bluetooth icon.
Figure 3.9
The NXT Brick indicates its USB status by way of a USB icon.
status. There are three possible states, each of which is represented by a different
icon. Figure 3.8 identifies these states and their associated icons.
The second icon at the top of the LCD shows the NXT Brick’s current USB
status. There are two possible states, each of which is represented by a different
icon. Figure 3.9 shows these states and their associated icons.
The name assigned to the NXT Brick is displayed as a text string at the top
center of the LCD display. The default name is NXT. As covered in Chapter 2,
you can change your NXT Bricks from the NXT window.
60 Chapter 3 n Working with Electronic Components
The fourth icon displayed near the upper right corner of the LCD is the
Mindstorms icon. When operating properly, this icon should continuously spin.
If your NXT Brick becomes unresponsive, check on the status of this icon. If it
has stopped spinning, your NXT Brick has frozen up. Try turning it off and on.
If this does not get the NXT Brick working again, you may have to reset it as
covered in Chapter 2.
The last of the icons displayed at the top of the NXT Brick’s LCD is the Battery
Level indicator icon, which graphically depicts the NXT Brick’s current battery
level.
Figure 3.10
A depiction of the NXT Brick’s file system.
The NXT Brick’s File System 61
My Files
The My Files folder provides access to NXT-G programs that you create and
download from your computer to programs that you create directly on the NXT
Brick. The My Files menu provides access to four submenus, listed here.
n Software files. Stores all of the NXT-G programs that you download
into the NXT Brick from your computer.
n NXT files. Stores any programs you create on the NXT Brick.
n Sound files. Stores all of the sound files that are pre-installed on the
NXT Brick as well as any sound files that you add to your NXT-G
programs.
n Datalog files. Stores log files containing variable and sensor values
generated during program execution.
NXT Program
This menu contains options that guide you through the creation of small NXT-G
programs. These programs are created in five steps, each of which consists of a
different command. Programs created from this menu require that sensors and
motors be connected to their default ports.
62 Chapter 3 n Working with Electronic Components
NXT Datalog
Data logging is the process of recording data over a period of time using sensors.
Using the options provided on this menu, you can configure the NXT Brick
along with its sensors to collect the following data.
n Temperature C
n Temperature F
n Sound dB
n Sound dBA
n Reflected light
n Ambient light
n Motor rotations
n Motor degrees
n Touch
n Ultrasonic inch
n Ultrasonic cm
n Color
View
Options on this menu allow you to view real-time data collection from sensors
connected to you next. With the appropriate sensors and motors in place, you
can view data for any of the following:
n Sound dB
n Sound dbA
n Reflected light
n Ambient light
n Temperature C
n Temperature F
n Motor rotations
The NXT Brick’s File System 63
n Motor degrees
n Touch
n Ultrasonic inch
n Ultrasonic cm
n Color
Bluetooth
This submenu is used to change Bluetooth options on the NXT Brick. It contains
the following four submenus:
n My Contacts. Stores a list of trusted Bluetooth contacts.
n Connections. Displays a list of current Bluetooth connections.
n Visibility. Controls whether other Bluetooth devices can see your NXT
Brick.
n On/Off. Enables and disables Bluetooth functionality.
This menu also contains a Search command that can be used to look for
Bluetooth-enabled devices within range of the NXT Brick.
Settings
The Settings menu lets you modify any of the following NXT Brick config-
uration settings.
n Volume. Allows you to modify the audio level at which sounds are
played.
n Sleep. Lets you specify how many minutes of inactivity must pass before
the NXT Brick puts itself to sleep. Valid options are 2, 5, 10, 30, and
60 minutes, or Never.
n NXT Version. Display NXT version information.
n Delete files. Lets you delete all of the downloaded programs stored in
any of the following subfolders: software files, NXT files, sound files, and
datalog files.
64 Chapter 3 n Working with Electronic Components
Try Me
Provides access to six sample programs designed to test the operation of the
NXT Brick’s sensors and motors. These sample programs are listed here:
n Try-Color
n Try-Touch
n Try-Light
n Try-Ultrasonic
n Try-Motor
n Try-Sound
Executing these tests is easy. For example, to execute the Try-Touch program,
use one of the NXT cables to connect the color sensor to port 1 on the NXT
Brick and then turn on the NXT Brick and select Try Me > Try-Touch > Try-
Touch Run. Next, press the orange-tipped cone at the touch sensor. In response,
a graphical image of a face appears and says “whoops.” Repeat this test as many
times as you want and then terminate the Try-Color program by pressing the
Clear/Go Back button.
Trap
In order for each of the six tests to work, you must connect the sensors to their default ports on the
NXT Brick using the RJ12 cables provided as part of the Lego Mindstorms NXT 2.0 kit.
The Sensors
The Lego Mindstorms NXT 2.0 kit comes equipped with four sensors, listed
here.
n Color Sensor
n Two Touch Sensors
n Ultrasonic Sensor
These sensors facilitate the building of robotic creations that can touch, detect
color and light, and even detect other objects. Two commonly used sensors not
found in the Lego Mindstorms NXT 2.0 kit are the light and sound sensors.
The Sensors 65
These two sensors were part of the Lego Mindstorms NXT 1.0 kit. Some of
programs found on the NXT Brick’s Try Me menu are designed to work with the
light and sound sensors. You can purchase both of these sensors online at http://
shop.lego.com.
Hint
Several other sensors are available. Some are made by Lego and some are made by third-party
companies. All can be purchased online at the Lego store. These sensors include:
n Compass sensor
n Accelerometer sensor
n Infrared sensor
n Gyroscopic sensor
n Temperature sensor
In addition to these sensors, Lego Mindstorms NXT 2.0 is also capable of supporting sensors from
the Lego RCX kit. These sensors include the rotation, light, touch, and temperature sensors.
However, a converter cable, which you can purchase from http://shop.lego.com, is required.
Color Sensor
The color sensor, shown in Figure 3.11, enables your robotic sensor to detect
different colors and light, providing your robotic creations with a form of vision.
In addition, this sensor provides a third function in that it can also be used as a
lamp to generate light. The color sensor can detect six different colors: red,
green, blue, yellow, black, and white. The color sensor supports a light sensor
Figure 3.11
The Color sensor provides your robotic creations with a form of vision, allowing them to detect different
colors and variations in light.
66 Chapter 3 n Working with Electronic Components
Figure 3.12
The Touch sensor can be used to create robots that are able to detect when they come into contact with
other objects.
mode, enabling it to detect light intensity within a room. It can also measure the
light intensity of colored surfaces. The sensor returns a value from 0 to 100,
where 0 represents complete darkness and 100 represents the brightest level. In
lamp mode the sensors can display red, green, or blue light.
Touch Sensors
The Lego Mindstorms NXT 2.0 kit comes with two touch sensors. A touch
sensor, shown in Figure 3.12, provides your creations with a sense of touch,
allowing them to feel and react to things around them. The touch sensors attach
to your robotic creation via connectors located underneath the sensor. In
addition, the sensor’s orange tip has a crosshair hole that can be used to further
customize the sensor.
The orange tip of the touch sensor registers contact with other objects. It
functions as a push button, registering two different states, pressed or released. A
value of 1 is generated when the orange tip is pressed and a value of 0 is
generated when it is not being pressed. Any of the following events can trigger a
touch sensor:
n The orange tip is pressed
n The orange tip is released
n The orange tip is pressed and then released
The Sensors 67
Figure 3.13
The Ultrasonic sensor can be used to detect objects and determine their distance.
Ultrasonic Sensor
While the color sensor provides your robotic creations with the ability to detect
color and light, the ultrasonic sensor (shown in Figure 3.13) provides your
creations with the ability to see objects and to determine their distance. Using
this sensor, you can create robots that can avoid other objects as they move
about or track and target objects that come into range.
The ultrasonic sensor is capable of measuring distances between itself and other
objects. It can also detect movement. It detects objects and measures the distance
between the sensor and the objects. In this way, it provides your creations with
the ability to determine their location. The sensor can cover a maximum
distance of 255 centimeters (100 inches) and with a precision level that is
within þ/ 3 centimeters (1.19 inches).
The Ultrasonic sensor operates on the same basic principle as sonar systems, as
depicted in Figure 3.14. It sends out high frequency sound waves, which reflect
off of objects in front of the sensor. The sensor keeps track of how long it takes
for the sound waves to bounce off of an object and return, and then calculates
distances based on the elapsed time.
Figure 3.14
A depiction of the ultrasonic sensor sonar-like operation.
68 Chapter 3 n Working with Electronic Components
Like all sonar base systems, the ultrasonic sensor is able to see solid objects easier
than software objects, Similarly, larger objects are easier to detect than smaller
objects.
Servo Motors
Mindstorms NXT 2.0 includes a set of three servo motors, shown in Figure 3.15,
that provide the ability to make your creations move and grasp things. The servo
motors have a number of connection points where they can be attached to your
creations. Servo motors have built-in reduction gear assemblies that include
optical rotary encoders that sense and track rotation.
Using its servo motors, your robotic creations can move, rotate, and grasp. Each
servo motor includes an integrated tachometer. A tachometer is a rotation
sensor that precisely controls the rotation of the motors. Servo motors transfer
motion through a pair of circular orange wheels that can be connected to your
robotic creations.
Figure 3.15
Servo motors provide your robotic creations with the ability to move.
Cables 69
Servo motor rotations are measured either by rotations or degrees. 360 degrees
represent a complete rotation. Servo motors operate with a precision or þ/ one
degree. When working with code blocks, you can specify either the number of
times a servo motor should rotate or the number of degrees by which it should
rotate. In addition, you can also configure a motor to run continuously for a
specified period of time.
Cables
As shown in Figure 3.16, the Lego Mindstorms NXT 2.0 kit includes seven
different cables of varying lengths. These cables are used to connect the sensors
and servo motors to the NXT Brick. These cables contain six wires and have
RJ12 connectors.
Figure 3.16
Additional sets of replacement cables can be purchased online from the Lego store.
70 Chapter 3 n Working with Electronic Components
1 20 cm/8 inches
2 50 cm/20 inches
4 35 cm/14 inches
The only difference between the seven cables is their length. The Lego Mind-
storms NXT 2.0 kit includes the list of cables outlined in Table 3.2.
Even though the cables that are supplied with the Lego Mindstorms NXT 2.0 kit
look like telephone cables, they are not. Telephone cables are four-wire cables,
whereas the cables supplied with the kit are six-wire and therefore not
compatible.
Note
The cables are among the most delicate pieces in the kit. Their RJ12 connectors are easily broken if
not handled carefully. Should one break, you can purchase replacement cables at the Lego store,
located at http://shop.lego.com, as shown in Figure 3.16. At the time this book was written, a set
of replacement connector cables cost $9.99.
Summary
The electronic components supplied with the Lego Mindstorms NXT 2.0 kit are
the key components in any robotic creations. Therefore, a good understanding
of the components and how they work is essential to any Mindstorms developer.
In this chapter you learned all about the NXT Brick, sensors, and the servo
motors. You learned that the buttons on the NXT Brick can also be used as
touch sensors. You learned about the NXT Brick’s menu system. Lastly, this
chapter provided you with an overview of the cables that are used to connect the
NXT Brick to the sensors and motors.
Chapter 4
In Chapter 3, “Working with Electronic Components,” you learned all about the
NXT Brick and the electronic motors, sensors, and cables that are included as
part of the Lego Mindstorms NXT 2.0 kit. These are important pieces, but they
only represent 15 of the 619 pieces in the kit. A thorough understanding of the
other pieces is essential to robotic development. In this chapter you will learn
about all of these pieces, including how to identify them, their function, and
their color and quantity.
The major topics covered in this chapter include:
n Beams
n Connectors
n Gears
n Other pieces
71
72 Chapter 4 n Adding Blocks and Gears
identification techniques will be applied to help simplify things. This will include
using attributes like part length and color as well as categorizing parts into
differently named categories and leveraging a few Lego terms.
Straight Beams
Straight beams are one of the most commonly used pieces in any robotic
creation. They have a smooth exterior, and their ends are rounded. Straight
beams are perforated with circular holes that run down the middle of the beam.
The circular holes are used to connect the straight beams to other pieces using
other connector pieces. Figure 4.1 identifies the eight types of straight beams in
the Lego Mindstorms NXT 2.0 kit.
Straight beams are identified by their length, which is measured using a unit of
measure referred to as a module. A module is 8mm long, representing the
distance between the outer edge of the beam and the center of the space located
between its next two closest holes, which is the same as the distance between the
center positions on either side of a hole, as depicted in Figure 4.2.
As demonstrated in Figure 4.1, this book identifies straight beams by specifying
their length followed by their name. Straight beams come in three colors. The
Building a Foundation with Beams 73
Figure 4.1
Straight beams are used to provide robotic creations with a strong framework.
Figure 4.2
An example of a straight beam that is three modules in length.
74 Chapter 4 n Adding Blocks and Gears
Figure 4.3
Except for the 2M beam, all straight beams have only circular holes.
Figure 4.4
Circular holes allow axles free movement.
2M straight beam is black, the 3M straight beam is dark gray, and the 5M
through 15M straight beams are all light-gray or white. However, because color
is not an attribute that is needed to distinguish one straight beam from another,
it is not included in the names of the beams referenced in this book.
With the exception of the 2M straight beam, all straight beams have circular
holes at both ends. As shown in Figure 4.3, the 2M beam differs from other
straight beams in that it has a circular hole at one end and a cross-hole at the
other.
The key difference between circular holes and cross-holes is that when you place
an axle in an open hole, the beam is able to spin freely as depicted in Figure 4.4.
Building a Foundation with Beams 75
Figure 4.5
Using axles inserted into angle beam cross-holes in order to build rigid structures.
Angular Beams
Another type of beam included in the Lego Mindstorms NXT 2.0 kit is the
angular beam. With angular beams, often referred to simply as angled beams,
one or more sections of the beam are angled. Figure 4.6 shows all eight of the
angular beams that are provided in the kit.
Angular beams can be used to create complex structures. They can also be used
in all sorts of creative ways, such as to create claws for robotic cranes. Like
straight beams, angular beams are measured and identified using modules.
Angular beams range in size from 5M to 11.5M. Angular beams vary in their
degree of angle, with the maximum angle of 90 degrees.
As demonstrated by the 9M angular beam shown in Figure 4.7, most angular
beams begin and end with cross-holes, allowing for the creation of rigid
connections using axles.
76 Chapter 4 n Adding Blocks and Gears
Figure 4.6
Angular beams come in different sizes, shapes, and colors.
Figure 4.7
Most angular beams have cross-holes at both ends.
Connectors 77
The T-Beam
The final type of beam included in the Lego Mindstorms NXT 2.0 kit is the T-
beam. As shown in Figure 4.8, this beam is T-shaped and is 3M wide and 3M
tall. This beam consists entirely of round holes. It can be used in various creative
ways to connect pieces in ways that other beams cannot.
Connectors
The largest category of pieces included in the Lego Mindstorms NXT 2.0 kit is
composed of the connectors. It includes the subcategories listed here.
n Axles
n Bushings
n Pegs
n Angle connectors
n Steering links
n Cross blocks
n Peg blocks
n Other connectors
These different types of connectors facilitate connections, allowing you to
connect and tie together different Mindstorms pieces into a cohesive whole.
Axles
Axles are cross-shaped shafts of varying lengths that can be used to connect to
any Mindstorms piece that has a cross-hole connection. Axles are often used in
conjunction with gears and servo motors to transmit motion. The Lego
Figure 4.8
Both sides of the T-beam are the same length.
78 Chapter 4 n Adding Blocks and Gears
Figure 4.9
Axles are used to establish connections and to transmit motion.
Bushings
One of the issues of working with axles is that they tend to slip out of their
connections to other pieces. To address this issue, the Lego Mindstorms NXT
2.0 kit includes three types of bushing connectors. Bushings hold their axle’s
position firmly, locking other pieces that may be connected to an axle in place.
Figure 4.10 shows the three types of bushing connectors supplied in the kit.
Figure 4.11 demonstrates the use of the bushing and half-bushing connectors.
The half-bushing connector firmly holds its position on the axle where it is
Connectors 79
Figure 4.10
Bushings are used to establish rigid axle connections.
Figure 4.11
Bushings are used to cap the ends of axles, to hold pieces in place on an axle, or as separators.
placed. In Figure 4.11 the bushing has been placed at the end of the axle. Any
number of other pieces can be placed between these two bushings, and their
positions will be firmly maintained.
Bushings can also be used as separators in order to create space between
different pieces attached to an axle. The Bushing 4 Peg piece has characteristics
of both bushings and pegs, which are discussed in the next section.
Pegs
Though small in stature, pegs are big in terms of utility, providing you with the
ability to connect beams and other types of pieces that have holes. As shown in
80 Chapter 4 n Adding Blocks and Gears
Figure 4.12
There are 10 different types of pegs.
Figure 4.12, the Lego Mindstorms NXT 2.0 kit contains 10 different types of
pegs. In total, 204 of these pegs are supplied in the kit.
Some pegs are designed to fit circular holes, some are designed to fit cross-holes,
and some are designed to fit both types of holes. In addition, pegs are either
smooth or friction. Smooth pegs move freely within round holes, whereas friction
pegs are designed to firmly hold their position. Regardless of the types of
connections that pegs have, one feature common to all pegs is the stop ridge. As
shown in Figure 4.13, the stop ridge determines how far the peg can get into a hole.
As demonstrated in Figure 4.14, pegs are used to connect different pieces
together. In the case of Figure 4.14, two straight beams are ready to be connected
using two connector pegs with friction.
Figure 4.15 shows the result of the previous example, once the two straight
beams are connected by pegs. The result is a new sturdy connection.
Connectors 81
Figure 4.13
The stop ridge is a feature common to all pegs.
Figure 4.14
Creating a rigid connection using a pair of connector pegs with friction.
Figure 4.15
Two straight beams connected by a pair of pegs.
Angle Connectors
Another important category of connector is the angle connector, which is used
to establish connections between axles. As shown in Figure 4.16, three different
types of angle connectors are supplied with the Lego Mindstorms NXT 2.0 kit.
Angle connectors can be used to connect two axles and to create 180-degree
(straight) connections, to connect two perpendicular axles, and to establish 90-
degree connections.
Figure 4.17 demonstrates the use of all three angle connectors. Here, two axles
are connected using an Angle Connector 90-Degree connector. Each of the axles
82 Chapter 4 n Adding Blocks and Gears
Figure 4.16
Angle connectors establish connections between axles.
Figure 4.17
An example of the use of angle connectors.
Steering Link
The steering link, shown in Figure 4.18, is designed to establish connections with
either the Friction Ball Peg or the Smooth Axle Ball Peg. These connections are
very strong. They can be used to add a decorative look or to establish flexible
connections in robotic creations that bend, stretch, and move.
Connectors 83
Figure 4.18
The steering link creates sturdy connections.
Figure 4.19
The steering link connects to ball pegs.
Figure 4.19 shows an example of the steering link used to establish a connection
between two Friction Ball Pegs.
Cross Blocks
Cross blocks, shown in Figure 4.20, are a combination of beams and bushings,
providing a great deal of flexibility of use. Cross blocks combine circular and
cross-hole connections. Cross blocks allow for the establishment of different
types of connections. These blocks provide enough variety to tackle most every
need and scenario.
Peg Blocks
Peg blocks are a highly specialized type of peg, combining features of beams and
pegs. Peg blocks have circular holes, which run in multiple directions. Peg blocks
facilitate parallel and perpendicular connections. The Lego Mindstorms NXT 2.0
kit contains three types of peg blocks, as shown in Figure 4.21.
Other Connectors
In addition to all of the different types of connections discussed so far, the Lego
Mindstorms NXT 2.0 kit contains three additional connectors that do not fit
into any of the previously discussed categories. These three connectors are
shown in Figure 4.22.
84 Chapter 4 n Adding Blocks and Gears
Figure 4.20
Cross blocks combine qualities of beams and bushings.
The Cross Axle Extension is used to connect two axles, establishing a longer axle
as a result, overcoming any axle length limitations in the kit. The Catch with
Cross-hole connector connects axles that intersect perpendicularly. The Flexible
Axle Damper 2M connector is used to connect axles that run in parallel and
supports robotic creations that require flexibility.
Gears
Gears are used to transmit motion. The Lego Mindstorms NXT 2.0 kit provides five
different types of gears. As shown in Figure 4.23, a total of 11 gears is provided.
The first four gears shown in Figure 4.23 transmit motion through their teeth
while rotating. Gears connect to one another, or mesh, by interlocking their
Gears 85
Figure 4.21
Peg blocks have features found in beams and pegs.
Figure 4.22
All three of these blocks establish connections to axles.
86 Chapter 4 n Adding Blocks and Gears
Figure 4.23
Gears are used as a means of transferring motion.
teeth. The rotation of one gear results in the rotation of the gear with which it is
meshed. Gears have cross-holes in their centers that are used to connect them to
axles. The axles are, in turn, threaded through circular holes in beams and other
pieces, allowing them to spin freely.
As shown in Figure 4.23, gears are identified based on the number of teeth they have.
The first four types of gears shown in Figure 4.23 are conical gears. Their teeth, as
shown in Figure 4.24, are shaped to allow them to mesh with one another when
mounted on either a parallel axis or when mounted perpendicular to one another.
Figure 4.24
Conical gear teeth come to a single point on both ends.
Figure 4.25
A two-gear “gear train” mounted on an angular beam.
When used in pairs, each gear in a gear train moves in an opposite direction as
depicted in Figure 4.26.
Gear trains can consist of any number of gears. In a gear train the gear that is
responsible for transferring motion to the other gears is referred to as the driver
88 Chapter 4 n Adding Blocks and Gears
Figure 4.26
A depiction of the direction that gears move in a gear train made up of two gears.
Figure 4.27
A hand crank made using a gear train with three gears.
gear. It is connected to a server motor via an axle. The last gear in the gear train
is referred to as the driven gear. If there are any gears between the driver gear
and the driven gear, these are referred to as idler gears. For example, Figure 4.27
shows a gear train made up of three gears.
Trap
As you add more gears to a gear train, more friction is introduced, reducing the overall
effectiveness of the gear train. In principle, it is best to keep the number of gears in gear trains
as small as possible.
Gears 89
Figure 4.28
An inventory of the pieces needed to build your own hand crank.
Figure 4.29
A visual depiction of the steps involved in assembling your own hand crank.
The parts needed to re-create the hand crank shown in Figure 4.27 are shown in
Figure 4.28.
Assembly directions are provided in Figure 4.29.
90 Chapter 4 n Adding Blocks and Gears
Figure 4.30
You can use any number of idler gears in a gear train.
Figure 4.31
The knob gear only works with other knob gears.
Figure 4.30 depicts the direction that gears move in a gear train made up of three
gears. As you can see, each gear moves in the opposite direction of the gear that
drives it.
Idler gears are used to bridge gaps between driver and driven gears when they
are too far apart to mesh. Idler gears can also be used to control the direction in
which the driven gear rotates. Using an odd number of idler gears, you can
create a gear train in which the driven gears move in the same direction as the
driver gear. Using an even number of idler gears, on the other hand, causes the
driven gear to rotate in the opposite direction from the driver gear.
The knob gear does not look like a typical gear. It has four circular knobs that
rotate around its axis. The knobs mesh with knobs on other knob gears. Like
other gears, it transfers motion from one axle to another, provided the other axle
also has a knob gear. Figure 4.31 demonstrates how knob gears work together.
Gears 91
Note that knob gears rotate in opposite directions as they mesh together. Like
other gears, you can create gear trains using knob gears.
Trick
An alternative to gearing down or gearing up is to programmatically control gear train speed using
programming blocks, which can be used to execute greater control over gear ratio.
92 Chapter 4 n Adding Blocks and Gears
Figure 4.32
These parts are essential elements of mobile robotic creations.
Other Parts
The remaining pieces and parts that make up the Lego Mindstorms NXT 2.0 kit
are shown in Figure 4.33. The pieces shown on the left side of the figure are
primarily decorative. The pieces shown on the right side of the figure have
various purposes. The magazine is used to hold the kit’s balls for using robots
that shoot. The shooter provides a means of shooting balls. The kit includes a
total of 12 balls, colored yellow, red, blue, and green. The comb wheel connects
up to four axles. It can be used in various ways, as a stabilizer for axles or as an
Summary 93
Figure 4.33
The Lego Mindstorms NXT 2.0 kit’s remaining pieces and parts.
attachment that extends the effects of gears. The V Belt is a small rubber band
that can be used to loosely bind pieces together.
Summary
This chapter rounded out the book’s review of the pieces and parts that make up
the Lego Mindstorms NXT 2.0 kit. It focused on all the nonelectric and
nonmechanical pieces and parts. This included learning about beams, connec-
tors, pegs, and various other parts groupings. In addition, you learned how to
work with gears and reviewed different configurations in which they might be
used to transmit motion. This chapter also demonstrated the use of many of the
pieces that were covered in order to further enhance your understanding of their
usefulness.
This page intentionally left blank
Chapter 5
Now that you know the basics of how to navigate around the Lego Mindstorms
NXT 2.0 GUI and have a basic understanding of all the different pieces and parts
supplied with the Lego Mindstorms NXT 2.0 kit, it is time to begin learning
about NXT-G programming. Using NXT-G, you will write programs that
control the operation of your robotic creations. NXT-G programs send instruc-
tions to the NXT Brick telling it how and when to operate attached servo motors
and sensors. In this chapter you will learn a number of basic programming
concepts and see how they apply to Lego Mindstorms NXT 2.0.
The major topics covered in this chapter include:
n Setting up personal profiles
n An overview of the program execution process
n An introduction to pseudocode and flowcharts
n Learning how to create new NXT-G programs
n Learning how to access programming blocks
95
96 Chapter 5 n Getting to Know the NXT-G Development Environment
Figure 5.1
A depiction of the key components involved in program execution.
Figure 5.2
An example of how programming blocks are used in the creation of NXT-G programs.
98 Chapter 5 n Getting to Know the NXT-G Development Environment
Figure 5.3
A depiction of the key logic needed to perform a particular task.
Hint
A flowchart is a graphical depiction of some or all of a program’s logic. Programmers create
flowcharts in order to outline the overall logic involved in developing a computer program. Once
developed, the graphical nature of flowcharts helps make their review easy. By reviewing a
flowchart, you can verify that your approach to solving a particular programming challenge makes
sense. This gives you a chance to identify any design flaws in the logic of your program before you
invest the time and effort required to develop it.
In this flowchart example, two separate courses of action are outlined. Which
one is executed depends on whether a NXT Brick sensor button has been
pressed. If the sensor button has been pressed, the actions outlined at the top of
the flowchart are executed. Otherwise, the actions listed at the bottom of the
flowchart are executed.
Once developed, you can translate the program logic outlined in a flowchart into
NXT-G programming logic as demonstrated in Figure 5.4.
Here, a switch programming block is used to execute either of two sets of
embedded programming blocks, depending on whether the NXT Brick’s sensor
100 Chapter 5 n Getting to Know the NXT-G Development Environment
Figure 5.4
The implementation of the flowchart’s logic in NXT-G.
button has been pressed. If you look closely at this example, you will see how
clearly it reflects the programming logic that was outlined in the previous
pseudocode and flowchart examples.
Figure 5.5
A depiction of the steps involved in creating and preparing a NXT-G program for execution.
Beginning a Program Development Session 101
Figure 5.6
Opening an existing NXT-G program.
102 Chapter 5 n Getting to Know the NXT-G Development Environment
Figure 5.7
Naming a new NXT-G program.
Hint
You can also access program files for the currently selected profile by pressing CtrlþO on Windows
or Command-O on Mac OS X. In addition, you can select program files by clicking on File > Open.
To create a new NXT-G program, type a name for the program in the Create
new program field as shown in Figure 5.7 and click on the associated Go>>
button.
Hint
You can also create a new NXT-G program by pressing CtrlþN on Windows or Command-N on Mac
OS X. Alternatively, you can create a new program file by clicking on File > New.
Beginning a Program Development Session 103
The Lego Mindstorms NXT 2.0 GUI automatically assigns the default name
Untitled-1 to your first new program. The number at the end of the new
program name is automatically incremented with each new program you create
after that. However, you can and should provide your NXT-G programs with
unique names of your own choosing, preferably assigning names that help
identify programs and describe their purpose.
Hint
In addition to keeping NXT-G programs separate, developer profiles also keep other program files
separated, including things like sound and graphic files. Note, however, that developer profiles are
not a security feature. There is nothing to prevent you from selecting someone else’s profile.
Figure 5.8
Selecting a developer profile.
104 Chapter 5 n Getting to Know the NXT-G Development Environment
Figure 5.9
Managing developer profiles.
Step-by-Step
1. Click on the Create button. This adds a new profile with a default name of Profile_1 to
the list of developer profiles and enables the Name: field.
2. Overtype the default profile name with a unique name of your own choosing, as demon-
strated in Figure 5.10.
4. Repeat the above steps to create as many profiles as needed and then click on the Close
button when done.
Beginning a Program Development Session 105
Figure 5.10
Creating a new developer profile named Jerry Ford.
Hint
You can rename developer profiles at any time from the Manage Profiles dialog box by selecting
one and overtyping its name. You can delete them by selecting one or more from the list of profile
names and then clicking the Delete button.
Figure 5.11
The work area is where you will develop the programming logic for your NXT-G programs.
Figure 5.12
By closing the Robo Center you can increase your view of the work area.
You can restore the work area to its original size and redisplay the Robo Center
at any time by clicking on the Robo Center Icon (an orange 3M beam) located
on the GUI toolbar.
Figure 5.13
Programming blocks are accessed through one of three tabs on the Programming palette.
Figure 5.13, programming blocks are organized and presented on the three tabs
that make up the Programming palette.
The Programming palette is located on the left side of the GUI and only one of
its tabs is visible at a time. Programming blocks are added to the NXT-G
program by dragging and dropping them onto the work area. After program-
ming blocks are added to a NXT-G program, you can configure the manner in
which each programming block executes by modifying parameters that are
displayed in the Configuration Panel whenever a programming block is selected.
Each programming block is unique and therefore has different parameters,
specific to the tasks that the programming block performs.
Accessing Programming Blocks 109
Figure 5.14
The most commonly accessed programming blocks are accessible from the Common tab.
Figure 5.15
The Common group provides access to the same programming blocks found on the Common palette.
Display Displays text, shapes, and images on the NXT Brick’s LCD.
Common Contains a list of all of the blocks found on the Common tab.
Action Contains a list of blocks that send commands to the NXT Brick in order to
execute specific actions.
Sensor Contains blocks that read sensors.
Flow Contains a list of blocks that can be used to alter the logical execution flow
of a program.
Data Contains a list of blocks that define, generate, and process program data.
Advanced Performs miscellaneous functions not associated with other block groups.
112 Chapter 5 n Getting to Know the NXT-G Development Environment
Record/Play Records an action manually performed with a robotic creation and then allows
that action to be replayed.
Sound Instructs the NXT Brick to play sound files.
Display Displays text, shapes, and images on the NXT Brick’s LCD.
Switch Chooses between two alternate courses of action based on the analysis of
sensor or input value.
Hint
Data wires are data pathways that you can draw between programming blocks in order to pass
data from one programming block to another.
Accessing Programming Blocks 113
Figure 5.16
The Action group displays five programming blocks.
Display Displays an image, text, or shape on the NXT Brick’s LCD screen or clears
the LCD screen.
Send Message This block is used to send a wireless message to your NXT Brick via a
Bluetooth connection.
Color Lamp Controls the Color Sensor’s lamp function, emitting red, green, or blue
light.
114 Chapter 5 n Getting to Know the NXT-G Development Environment
Figure 5.17
The Sensor group displays nine programming blocks.
There are a total of nine Sensor programming blocks, as outlined in Table 5.5.
Touch Sensor Sends a true/false signal through a data wire based on the current
condition of a touch sensor.
Sound Sensor Detects sounds and reports on sound level.
NXT Buttons Sends true signal through a data wire whenever a NXT button is
activated.
Rotation Sensor Counts the number of degrees or the number of rotations that a motor
turns.
Timer This block reads the timer’s current value or restarts the timer.
Color Sensor Enables the detection of different colors and the measurement of light
intensity.
Data programming blocks also pass data to other programming blocks via data
wires. There are a total of seven Data programming blocks, as outlined in Table 5.7.
Figure 5.18
The Flow group contains four programming blocks.
Switch Chooses between two alternate courses of action based on the analysis of
sensor or input value.
Stop Halts program execution and any running motors.
Accessing Programming Blocks 117
Figure 5.19
The Data group contains seven programming blocks.
Figure 5.20
The Advanced group contains seven programming blocks.
Number to Text Takes a number and turns it into a displayable text string.
Text Adds text strings together in order to create a longer text string.
click on the Programming pallete’s Custom tab and then move the mouse
pointer over their icons. Any blocks of these types are then displayed. By default,
you don’t have any My Block programming blocks. Table 5.9 provides an
overview of these blocks and their purpose.
Summary
This chapter provided you with foundational information needed to begin
programming using NXT-G. You learned about the execution of computer
programs and how NXT-G programs are developed. You were introduced to
pseudocode and flowcharts and given examples of these important program
planning and design tools. This chapter showed you how to create and switch
between different developer profiles and how to open and create NXT-G
programs. This chapter also showed where to find programming blocks and
reviewed their location on the Programming palette.
This page intentionally left blank
Chapter 6
In the last chapter you learned how to create and open NXT-G programs, and
you learned a lot about the various NXT-G programming blocks. Now it is time
to dig a little deeper and learn how to use sequence beams to develop NXT-G
programs that can run sequentially or that can multitask, executing multiple
tasks in parallel. You will learn how to work better in the work area, and you will
learn how to add comments to your NXT-G programs in order to document
your programming logic.
The major topics covered in this chapter include:
n A breakdown of the different parts of the programming block icons
n Getting to know a little more about the Starting Point
n Learning how to work with sequence beams
n Learning how to multitask using parallel sequence beams
n Adding additional value to your NXT-G programs using comments
121
122 Chapter 6 n Basic NXT-G Programming Concepts
Figure 6.1
The configuration panel facilitates the customization of programming block actions.
Figure 6.2
A breakdown of the different attribute information displayed on programming blocks.
programming block attributes, you can customize the actions the programming
block performs. The configuration panel is only visible when a single program-
ming block is selected. If no programming block is selected or if more than one
programming block is selected the configuration panel turns blank.
Changes made to programming block attributes are reflected in the appearance
of the blocks. Figure 6.2 demonstrates how much attribute information you can
glean from a programming block just by looking at it.
Figure 6.2 provides a detailed example of the attribute information displayed on
a typical programming block. In the case of Figure 6.2, a Move block is shown
twice, with and without its data hub extended. A data hub is a collection of input
and output ports that programming blocks can use to receive and transmit data.
Beginning at the Starting Point 123
You will learn all about the data hub in Chapter 8, “Advanced Programming
Techniques.”
Hint
You can use data wires, which are introduced in Chapter 8, to pass data between programming
blocks, even those located on different sequence beams.
As shown in Figure 6.2, seven distinct pieces of attribute information are visible.
Attributes vary from programming block to programming block and not all
programming blocks display the same number or type of attribute data. In the
case of the Move block shown in Figure 6.2, the block is identified by its image
icon. Its direction is set to forward, its power level is set to 75 percent, and its
unit measure is set to rotations. In addition, the block is set to control two
motors, attached to ports B and C on the NXT Brick. The circular shape and
color stripe identifies this block as being a Common programming block. If
more information is needed or if you are not sure what a given attribute symbol
means, you can either select the programming block and review its attribute data
in the configuration panel or you can look the block up in the Lego Mindstorms
NXT 2.0 help file.
Figure 6.3
Every NXT-G program begins at the Starting Point.
124 Chapter 6 n Basic NXT-G Programming Concepts
Excluded from
the NXT-G Program
Figure 6.4
Programming blocks not connected to a NXT-G program’s sequence beams are not saved as part of the
program file.
to the Starting Point via a sequence beam. Programming blocks are downloaded
to the NXT Brick when you download your program into it.
Trap
Any blocks that are not connected to a NXT-G program’s sequence beams, as demonstrated in
Figure 6.4, are not considered to be a part of your NXT-G program. As such, they will not be
downloaded to your NXT Brick.
Hint
You can remove a programming block from a sequence beam by selecting it and then pressing the
Delete key. If you remove a programming block from a sequence beam, the beam will automati-
cally resize itself to reclaim space formerly allocated to the programming block.
Understanding the Sequence Beam 125
Figure 6.5
The work area displays three vertical white grid squares at the location where the programming block
will be positioned.
Figure 6.6
Programming blocks automatically snap into place when dropped on a sequence beam.
Figure 6.7
When the mouse pointer turns into a pair of arrow-tipped brackets, you can manually adjust a sequence
beam’s length.
Figure 6.8
An example of a sequence beam that has been manually extended.
If you prefer, you can manually increase the length of sequence beams. To do so,
move the mouse pointer over the end of a sequence beam and left click on it. In
response, the mouse pointer will turn into a pair of arrow-tipped brackets, as
demonstrated in Figure 6.7. Next, drag the mouse pointer outward as you
continue to hold down the mouse button. Once you have the sequence beam’s
length the way you want it, double click on it to lock it in place.
Figure 6.8 shows an example in which a sequence beam has been manually
extended several inches.
If you single click as you draw, you can pin the sequence beam to that location
on the work area and then continue to draw it in a different direction. You can
continue to draw the sequence beam in various directions using this approach.
Understanding the Sequence Beam 127
Figure 6.9
An example of a sequence beam that has been manually extended in different directions.
Figure 6.10
An example of an extended beam populated with programming blocks.
When finally done, double click on the new end of the sequence beam to lock it
into position, as demonstrated in Figure 6.9.
Once you are done extending a sequence beam, you can go back and add
programming blocks to it as needed to develop your NXT-G programs, as
shown in Figure 6.10.
128 Chapter 6 n Basic NXT-G Programming Concepts
Reel
Figure 6.11
The mouse pointer has turned into a reel indicating that you can now grab the sequence beam and
resize it.
Multitasking Using Parallel Sequence Beams 129
Figure 6.12
An example of a NXT-G program under development that has two sequence beams.
Figure 6.13
This NXT-G program performs two separate sets of actions in parallel within one another.
130 Chapter 6 n Basic NXT-G Programming Concepts
Figure 6.14
This NXT-G program performs three sets of parallel actions.
By expanding and adding programming blocks to the upper sequence beam, you
can add a third set of actions that will operate in parallel with the first two sets of
actions, as demonstrated in Figure 6.14.
In addition to the parallel sequence beams already available at the Starting Point,
you can add new parallel sequence beams at other points along existing sequence
beams within your program. This is accomplished by placing the mouse pointer
over an open portion of the sequence beam where you want to add a parallel
sequence beam and then holding down the Shift key while you move the mouse
pointer to draw and expand a new beam originating from the existing beam, as
demonstrated in Figure 6.15.
Moving Programming Blocks Around Your NXT-G Programs 131
Figure 6.15
In this example, two new sequence beams have been added to the NXT-G program.
Figure 6.16 shows how the NXT-G program shown in Figure 6.15 might look
once the program blocks have been added to the new sequence beams.
Figure 6.16
An example of a NXT-G program whose programming logic is outlined using five sequence beams.
Once it is selected, left click on the programming block and, while continuing to
hold down the left mouse button, drag the programming block to its new
location.
Trap
In order to drag and drop a programming block from one location to another, the mouse pointer
must be set to the Pointer Tool. As shown in Figure 6.18, the Pointer Tool makes the cursor look
like a pointer arrow, which is the default setting for the mouse pointer.
If you need to move more than one contiguous programming block, you can
drag and drop them all at once by clicking on the work area and then drawing a
blue square around the entire block as demonstrated in Figure 6.19.
Moving Programming Blocks Around Your NXT-G Programs 133
Figure 6.17
The highlighted programming block is the currently selected block.
Figure 6.18
The Pointer Tool is selected by default each time you start a new programming project.
Once selected, you can drag and drop the selected programming blocks to their
new location by left clicking on one of the blocks, and then holding down the left
mouse button while moving them to their new location.
Trick
You can also select multiple programming blocks by selecting the first block and then, while
holding down the Shift key, selecting additional programming blocks.
134 Chapter 6 n Basic NXT-G Programming Concepts
Figure 6.19
An example of a NXT-G program in which two program blocks have been selected.
Trap
If you make any mistakes when moving programming blocks, you can use the toolbar’s Undo and
Redo commands to put things back to a prior state. You can also use the Cut, Copy, and Paste
commands to make copies of programming blocks.
Figure 6.20
Using the Pan Tool to drag a new portion of the work area into view.
Once you have selected the Pan Tool, you can use the cursor to click on an open
area of the work area and then drag that location to a new position on the
screen.
Figure 6.21
You must click on the Comment Tool in order to add a comment to your NXT-G program.
Once you have selected the Comment Tool button, all you have to do to add a
comment to a NXT-G program is click on an open portion of the work area. A
white box will appear into which you can begin typing. This space will
automatically resize itself to accommodate as much text as you enter. If you
press Enter when typing in text, a carriage return is executed, allowing you to
continue typing in a new line.
Trick
Once written, you can modify a comment by selecting it, positioning the cursor at the desired
location within the comment, and entering your new text.
You can move comments around the work area if you need to by clicking on the Pointer icon in the
toolbar and then selecting a comment. When you do this, the comment is enclosed within a blue
square. You can drag and drop the comment to a new location on the work area by placing the
mouse pointer over a portion of the outlying blue square and holding down the left mouse button
to grab onto it.
Comments can be deleted by selecting them and pressing the Delete key.
Figure 6.22
An example of a NXT-G program that contains comments describing each programming block’s
configuration and purpose.
Summary 137
Figure 6.23
Adding a high-level comment to a NXT-G program.
Trick
You can also add a comment by double clicking on a free spot in the work area and then typing in
comment text.
You can provide more high-level comprehensive documentation for your NXT-
G program by clicking on the Mindstorms icon located on the Starting Point.
This displays a scrolling text box labeled Info: at the bottom of the Lego
Mindstorms NXT 2.0 GUI (in the location where the configuration panel is
displayed) as demonstrated in Figure 6.23.
A good way to make use of the Info: text box is to use it to document your
program’s name, purpose, creation date, and last update. This way anyone,
including yourself, who comes back later and looks at this documentation, can
immediately assess the program’s purpose, author, and other useful information.
Summary
This chapter showed you how NXT-G reflects programming block configuration
through the display of graphic images on the programming block’s icon. You
learned more about the Starting Point as well as how to work with sequence
beams. This included how to develop multitasking programs using parallel
sequence beams. You also learned the importance of documenting your NXT-G
programs through the addition of comments.
This page intentionally left blank
Chapter 7
The seven programming blocks located on the Common palette are the most
commonly used programming blocks. A good understanding of how to work
with these blocks is essential to your success as a Lego Robotics developer. In
this chapter you will review all of the major features and capabilities of these
blocks. This will include learning how to work with and configure programming
block attributes.
The major topics covered in this chapter include learning how to:
n Move your robots
n Record and play back motor movement
n Play sounds and display graphics on the NXT Brick’s LCD
n Pause execution and retrieve sensor data
n Control logic flow using loops and conditional logic
139
140 Chapter 7 n Working with the Common Programming Blocks
are by far the most commonly used and therefore merit additional attention.
Common programming blocks include all of the following:
n Move
n Record/Play
n Sound
n Display
n Wait
n Loop
n Switch
Together these programming blocks provide the foundational programming
logic needed to develop basic NXT-G programs for all types of robotic creations.
Ports
Direction Duration
Power
Figure 7.1
The Move block displays four graphic symbols indicating its configuration.
The Move Block 141
Figure 7.2
The Move block’s configuration panel.
Hint
Note that on the far left side of the Move block’s configuration panel are feedback boxes. These
boxes display data reflecting the movement of servo motors connected to each of NXT Brick’s ports.
Data is displayed in degrees. To work, you must have at least one servo motor connected to your
NXT Brick, and the NXT Brick must be powered on and connected to your computer.
The values shown in each of the three display fields increment or decrement based on whether the
servo motors are moved forward or backward. Data is displayed in feedback boxes even if you
manually manipulate servo motor movement. By manually moving your robot a predefined
distance, for example, and observing the resulting data, you can collect the data needed to
precisely configure the duration attribute.
You can reset the value shown in the feedback boxes to zero at any time by clicking on the R
button located above the port letter labels.
Trap
If you configure two ports but only attach one servo motor to your robotic creation, the Move block
won’t work. This is because the block is unable to synchronize the two motors.
If you select all three ports, ports B and C are automatically synchronized, and
the servo motor attached to port A must be controlled via another programming
block. With this configuration, you must ensure that the servo motors respon-
sible for movement are attached to ports B and C, because the default port
assignments cannot be changed.
The Direction Attribute
Direction specifies the direction the robot will move and is configured as
forward (default), backward, or stop. Specifying stop resets all configured
motors. You may have to reverse the direction setting depending on how you
have designed your robot and attached its servo motors. Figure 7.3 depicts the
direction that servo motors spin when moving forward and backward.
The Steering Attribute
The Steering attribute is enabled only when two ports are selected. Using the two
drop-down selection boxes, you can specify to which side of your robot the two
turbo motors are attached. By default, the two motors are configured to move at
the same speed, causing the robot to move in a straight line. By adjusting the
slider bar, you can make your robot veer to the left or right at various angles.
Figure 7.3
A depiction of forward and backward servo motor movement.
The Record/Play Block 143
Action
Figure 7.4
The Record/Play block displays a single graphic symbol indicating whether it is currently recording or
playing back a recorded action.
144 Chapter 7 n Working with the Common Programming Blocks
Figure 7.5
The Record/Play block’s configuration panel.
Figure 7.6
The Sound block displays several graphic symbols indicating how the programming block has been
configured.
Figure 7.7
The Sound block’s configuration panel as it appears when configured to play a sound file.
146 Chapter 7 n Working with the Common Programming Blocks
Figure 7.8
The Sound block’s configuration panel as it appears when configured to play a note.
Figure 7.9
An example of a short piece of music consisting of seven notes.
displayed. When you select a file, the file is immediately played on your
computer, so you can hear what it sounds like.
The Wait Attribute
The Wait attribute is used to instruct NXT-G to wait until the Sound block has
finishing playing before allowing the next block on the sequence beam to
execute. This attribute is enabled by default.
The Note Attribute
The Note attribute is accessible only when the Sound block is used to play a
tone. As shown in Figure 7.8, a graphical keyboard is displayed in the
configuration panel. You can specify the note to be played by using the
mouse pointer to click on a keyboard key. A letter representing the selected
key is then displayed above the keyboard. To the right of the letter is a field in
which you can type a number specifying the number of seconds that the tone
should be played.
If you string a number of Sound blocks together on a sequence beam, you can
play music. Figure 7.9 shows an example of how this might look.
Figure 7.10
The Display block displays a single graphic symbol indicating the type of resource that it will display
when executed.
Figure 7.11
A depiction of the system of coordinates used when drawing on the NXT Brick LCD.
Images, text, and shapes are drawn on the NXT Brick LCD using a coordinate
system. This system originates from coordinates 0, 0, located in the lower left
corner of the display. The Y (vertical) coordinate stretches upward and the X
(horizontal) coordinate stretches to the right. Because the LCD is 64 pixels high
by 100 pixels wide, the maximum length along the Y coordinate is 64, and the
maximum length of the X coordinate is 100. Coordinates 32, 50 specify the
center position of the LCD. Figure 7.11 depicts this coordinates system.
Figure 7.12 shows how the configuration panel looks when the Display block is
configured to display an image.
Sound files are relatively large files. If your applications play a lot of sounds, you
can quickly fill up the NXT Brick’s memory. Musical tones, on the other hand,
are relatively small and consume a lot less memory. If all your application
requires is to make a simple sound, playing a tone is the more efficient option.
The Display Block 149
Figure 7.12
The Display block’s configuration panel as it appears when configured to display an image.
Hint
If you do not clear the LCD prior to a drawing operation, you can layer one drawing on top of
another, resulting in more complex output, displaying shapes, images, and text at the same time.
Figure 7.13
The Wait block comes in many different forms.
Figure 7.14
The Wait block’s configuration panel as it appears when configured to pause execution a predetermined
period of time.
feel for how the other variations work. For additional detail on how to work with
all of the various Wait block options, consult the Lego Mindstorms NXT 2.0
Help file.
Figure 7.15
The Wait block’s configuration panel as it appears when configured to pause execution depending on
sensor input data.
The Loop Block 153
Figure 7.16
In this NXT-G program, execution is delayed until the Touch Sensor is pressed on the robot.
Figure 7.17
The Loop block is configured by default to repeat its execution forever.
Figure 7.17 shows how the Loop block looks when initially added to a NXT-G
program. By default, the Loop block is configured to repeat forever in an endless
loop, as indicated by the display of the infinity symbol on the lower right side of
the block.
Trap
An endless loop is a loop that does not have a predefined means of terminating its own execution.
The Loop block can be configured to set up five different types of loops,
including:
n Forever—Executes forever.
n Sensor—Executes repeatedly until a given sensor value is received.
n Time—Executes repeatedly for a predetermined number of seconds.
n Count—Executes a specific number of times.
n Logic—Executes repeatedly until a logical value of True or False is
received from another programming block via a data wire.
Figure 7.18 demonstrates how the Loop block looks when configured to respond
to touch sensor input. Specifically, the Loop block repeats its execution until the
touch sensor’s button is pressed.
Figure 7.19 shows how the configuration panel looks when the Loop block is
configured to look for touch sensor input.
The Loop block has five different attributes. These attributes vary depending on
the type of loop that has been specified. The configuration panel also displays a
The Loop Block 155
Figure 7.18
This Loop block has been configured to execute until a touch sensor button is pressed.
Figure 7.19
The Loop block’s configuration panel as it appears when configured to look for touch sensor input.
feedback box for the Loop block, which displays a number representing the
number of times the loop has repeated its execution.
The Control Attribute
The Control attribute is used to specify the type of loop that is to be set up
(Forever, Sensor, Time, Count, or Logic).
The Sensor Attribute
The Sensor attribute is available only when the Control attribute is set to Sensor.
It contains a drop-down list of 11 options, each of which specifies a different
type of sensor input. In the case of Figure 7.19, the sensor block has been
specified.
The Show Attribute
The Show attribute determines whether or not an output data plug is included as
part of the Loop block, allowing the block to pass a number representing the
number of times the loop has repeated to another programming block as input.
156 Chapter 7 n Working with the Common Programming Blocks
Figure 7.20
An example of a NXT-G program made of eight programming blocks.
Figure 7.21
An example of a NXT-G program consisting of two programming blocks embedded within a Loop block.
Top Condition
Port Number
Sensor Type
Tested Condition
Bottom Condition
Figure 7.22
The Switch block is configured by default to analyze touch sensor input.
provide NXT-G programs with the ability to select from two sets of paths based
on the evaluation of a specified condition.
The Switch block is highly configurable. Figure 7.22 shows how the Switch block
looks when initially added to a NXT-G program. By default, the Switch block is
configured to evaluate data collected from a touch sensor block.
Figure 7.23 shows the attributes supported by the Switch block when configured
to work with a touch sensor. Switch block attributes may vary for other sensors.
Hint
For a detailed review of all the Switch block’s attributes, refer to the Lego Mindstorms NXT 2.0 Help
file.
158 Chapter 7 n Working with the Common Programming Blocks
Figure 7.23
An example of the attributes belonging to the Switch block.
Hint
If configured to work with a value instead of a sensor, the Switch block can test for Logic, Number,
and Test data passed to the block via data wires. If the data to be tested is a number or text, the
Switch block can have more than two sequence beams/tabs.
Figure 7.24
A comparison of the regular and flat views of the Switch block.
Summary
This chapter provided you with an in-depth review of all of the programming
blocks located on the Common palette (Move, Record/Play, Sound, Display,
Wait, Loop, and Switch). This included learning how to configure these
programming blocks and reviewing their major attributes. An understanding
of these programming blocks will enable you to develop NXT-G programs that
make robots move, play sounds, display text and graphics on the NXT Brick
LCD, collect sensor data, and use that data to control robot actions and
behavior.
This page intentionally left blank
Chapter 8
Advanced Programming
Techniques
In this chapter you will learn advanced NXT-G programming principles. This
includes learning more about Action, Sensor, Flow, and Data blocks. You will
also learn how to create your own custom blocks. You will learn how to transmit
data between programming blocks using data wires. By the time you have
completed this chapter, you will have the foundational programming knowledge
required to begin developing the NXT-G programs for the robots that you will
learn how to build in Chapters 9 through 11.
The major topics covered in this chapter include:
n Learning how to work with data hubs and data wires
n Learning more about programming blocks that pass data through data
wires
n Learning how to create custom functions by building My Blocks
n Tips for tracking program execution and locating and fixing errors
161
162 Chapter 8 n Advanced Programming Techniques
robots that can perform specific tasks, interact with their environment, and
adjust their operation, you need to learn how to work with data hubs and data
wires, using them to pass data between programming blocks. This data can then
be analyzed, incorporated into your NXT-G programs, and used to create vastly
more complex NXT-G programs and thus more intelligent and capable robots.
With one exception, the Wait programming block, all NXT-G programming
blocks are able to communicate and pass data among one another using data
wires. Many programming blocks only work properly when you pass data to
them through data wires.
Input Data
Output Data
Plugs
Plugs
Data Hub
Figure 8.1
An example of a Math programming block with its data hub hidden and with it displayed.
Passing Data Between Blocks Using Data Wires 163
side of the data hub and are used to accept data passed to the programming
blocks. Output data plugs are located on the right side of the data hub and are
used to pass data to other programming blocks.
Hint
To remove a data wire, all you have to do is click on it and press the Delete key.
Figure 8.2
An example of two programming blocks connected via a data wire.
164 Chapter 8 n Advanced Programming Techniques
Here, logic (True/False) data from a Touch sensor block is passed to a Display
block. If a value of True is passed to the Display block, the Display block will
clear the NXT Brick’s LCD screen. If a value of False is passed to the Display
block, the LCD is not cleared.
Different blocks have different numbers of input and output plugs. If you look
closely at Figure 8.2, you will notice that there are different graphic symbol labels
displayed for each data plug. These symbols provide a visual description of the
type of data that the plug is able to work with. In order to create valid
connections between different programming blocks, the data wires that connect
them must be connected to compatible data plugs. In the case of the example
shown in Figure 8.2, the data wire is connected to data plugs that work with
logic data on both blocks.
Trick
If you click on a data hubs tab after setting data connections, the data hub is automatically resized
so that only the data plugs in use remain displayed as demonstrated in Figure 8.3.
The first two programming blocks show a data wire connected between two data plugs. The
second two programming blocks show the same two data plugs after the data hub in the second
block has been resized.
Figure 8.3
An example programming block with its data hub in expanded and collapsed states.
Passing Data Between Blocks Using Data Wires 165
Trap
As demonstrated in Figure 8.4, some data plugs accept ranges of data. If data outside of these
ranges is passed, an error will occur. The data passed may be ignored, or it may be changed to
make it fit the range.
166 Chapter 8 n Advanced Programming Techniques
Figure 8.4
An example taken from the Lego Mindstorms NXT 2.0 Help file for the Display block.
Figure 8.5
An example of a broken data wire with missing input.
you which of the three types of errors has occurred. Figure 8.5 shows an example
of a broken data wire that has missing input.
The data wire connection shown in Figure 8.5 begins at an output plug whose
corresponding input plug does not have a data source. Figure 8.6 demonstrates
that one way of fixing this error is to delete the data wire and redraw it, this time
from the same type of data type plug, in this case, the one located at the bottom
of the Move block’s data hub.
The broken data wire shown in Figure 8.7 has two input sources, which is not
permissible.
Figure 8.8 shows an example of a broken data wire with a data type mismatch.
Here a data logic output plug on a Touch sensor block has been connected to a
Port input plug on a Color Lamp block. The logic plug passes a value of True/
False, but the Port plug can only accept numeric data within a range of 1–4.
168 Chapter 8 n Advanced Programming Techniques
Figure 8.6
The data wire’s missing input error has been corrected.
Figure 8.7
An example of a broken data wire with too many inputs.
Passing Data Between Blocks Using Data Wires 169
Figure 8.8
An example of a broken data wire with a data type mismatch.
Figure 8.9
An example of a data wire whose path extends through multiple programming blocks.
170 Chapter 8 n Advanced Programming Techniques
In Figure 8.9 the power level input and output plug is used to facilitate the
creation of the wire path. Here, light intensity data is passed from a Light sensor
to a Move block and then on to a Sound block where it is used to configure
volume level. The data that traverses the wire path is not changed.
Figure 8.10
The Motor block provides detailed control over servo motors.
Programming Blocks That Use Data Wires 171
Figure 8.11
The Motor block’s configuration panel.
Trap
Avoid using both the Move block and Motor block in the same NXT-G program. Doing so increases
the program’s size. Specifying one or the other allows for code sharing and means one less block
has to be downloaded into the NXT Brick.
Figure 8.11 shows the Motor block’s attributes as seen in the configuration
panel.
As shown in Figure 8.11, the Motor block supports a total of eight attributes and
a Feedback box displays the number of degrees a servo motor has moved. These
attributes include:
Port
Specifies the ports the servo motor is connected to (A, B, or C).
Direction
Specifies the direction that the servo motor will rotate (forward, backward, or
stop).
Action
Specifies how the servo motor will accelerate: Constant—Immediately accel-
erates to the specified power level; Ramp Up—Slowly accelerates to the specified
power level; Ramp Down—Slowly decelerates to the specified power level.
Power
Specifies the servo motor’s power level.
Control
Specifies whether Motor Power is set. If set, Motor Power causes the NXT Brick
to attempt to supply whatever level of power is needed to ensure that the servo
172 Chapter 8 n Advanced Programming Techniques
motor spins at its specified speed (for instance, when operating under adverse
conditions).
Duration
Specifies whether servo motor movement should be measured in seconds,
degrees, rotations, or unlimited.
Wait
Specifies whether the block should finish its execution before allowing other
blocks on the sequence beam to execute.
Next Action
Specifies whether servo motors should brake or coast when the block is done.
Figure 8.12
The Send Message block sends wireless messages via Bluetooth to other robots.
Figure 8.13
The Color Lamp block is used to make the Color sensor display a colored light.
Programming Blocks That Use Data Wires 173
display of red, green, or blue light. This block is usually used in pairs, one block
turning a light on and the other block turning it off.
The Color Lamp block has the following attributes.
Port
Specifies the port to which the color sensor has been attached.
Action
Specifies whether the lamp features should be enabled or disabled.
Color
Specifies the color of light that should be displayed (red, green, or blue).
Figure 8.14
There are a total of nine sensor blocks.
174 Chapter 8 n Advanced Programming Techniques
Figure 8.15
The Touch sensor block’s configuration panel.
manner, having only 2 or 3 attribute settings. This section will use the Touch
sensor to demonstrate the operation of all the sensor blocks. To learn more
about the other sensor blocks, consult the Lego Mindstorms NXT 2.0 Help file.
The Touch sensor block sends a logic value (True/False) through a data wire that
indicates the current status of the Touch sensor. As shown in Figure 8.15, this
block has just two attributes, both of which are displayed on its icon.
Port
Specifies the port on the NXT Brick to which the Touch sensor is connected.
Action
Specifies the type of action being tested: Pressed, Released, or Bumped.
To help demonstrate how this sensor block works, look at the example shown in
Figure 8.16. Here, the Touch sensor block is used to control the direction in
which a robot moves. The Touch sensor control block sends a logical value of
True through its data wire when its sensor is pressed, and a False if it is not
pressed. The value passed by the Touch sensor block is passed via data wire to
Figure 8.16
A small NXT-G program that uses the Touch sensor to control the robot’s direction.
Programming Blocks That Use Data Wires 175
the Motor block’s Direction input plug, which accepts logic data and sets the
Motor block’s Direction attribute. If a True value is received, the Motor block
sets the robot’s direction to forward. If a False value is received, the robot’s
direction is set to backward.
Figure 8.17
The Stop block is used to halt a NXT-G program’s execution.
176 Chapter 8 n Advanced Programming Techniques
Figure 8.18
The Logic block performs a logical comparison on its input data.
Figure 8.19
The Logic block’s configuration panel.
or supplied via data wire. The Logic block outputs a logical (True/False) value
through its output plug.
As shown in Figure 8.19, the Logic block has a single attribute named Operation.
In most cases, input values will be passed to the block via data wire. However,
you can assign input values of True or False by selecting the check (True) or
cross (False) for both of the block’s input values on the configuration panel.
In addition, using the drop-down list located in the upper right corner of the
configuration panel, you can select one of the four following logical operations
that will be used to process the data values.
n And. Outputs a value of True if both input values are true.
n Or. Outputs a value of True if either or both input values are true
(default).
n Xor. Outputs a value of True if one input value is True and the other
value is False. Outputs a value of False if both input values are True or
both input values are False.
Programming Blocks That Use Data Wires 177
Figure 8.20
The Logic block is used to process data from two sensor blocks, and its output is sent to the Move block,
where it is used to set the robot’s direction.
n Not. Inverts a logical value (used when only one value is received as
input).
To help demonstrate how this data programming block works, look at the
example shown in Figure 8.20. Here, logic data from the Sound and Light sensor
blocks is passed to a Logic block where it is analyzed. If either of these sensor
block’s logic data is True, a value of True is output by the Logic block. When
received by the Move block, a value of True sets the robot’s direction to forward
and a value of False sets the robot’s direction to backward.
Figure 8.21
The Math block performs basic mathematical operations on input data.
Figure 8.22
The Math block’s configuration panel.
Using the drop-down list located in the upper right corner of the configuration
panel, you can select one of the six following mathematical operations that will
be used to process the data values.
n Addition. Adds the two numeric values together (default).
n Subtraction. Subtracts the second value from the first value.
n Multiplication. Multiplies the two numeric values.
n Division. Divides the first numeric value by the second numeric value.
n Absolute Value. Calculates the absolute value of a single value (used
only when the block is given a single value to process).
n Square Root. Calculates the square root of a single value (used only
when the block is given a single value to process).
Figure 8.23
The Compare block determines if one input is greater than, less than, or equal to another input.
Figure 8.24
The Compare block’s configuration panel.
configuration panel or supplied via data wire. The Compare block outputs the
two values as well as a logical value representing the result of its comparison.
As shown in Figure 8.24, the Compare block has a single attribute named
Operation. In most cases, input values will be passed to the block via data wire.
However, you can assign input values in the form of two numbers by typing
values into the A and B entry fields.
Using the drop-down list located in the upper right corner of the configuration
panel, you can select one of the three following comparison operations that will
be used to process the data values.
n Less Than. Determines whether the first value is less than the second
value (default).
n Greater Than. Determines whether the first value is greater than the
second value.
n Equals. Determines whether the two values are equal.
To help demonstrate how this data programming block works, let’s look at the
example shown in Figure 8.25. Here, logic data from the Light sensor block is
180 Chapter 8 n Advanced Programming Techniques
Figure 8.25
The Compare block is used to process data from the Light sensor block, and its output is sent to the
Move block, where it is used to set the robot’s direction.
Figure 8.26
The Range block determines if a number is inside or outside of a range of numbers.
Figure 8.27
The Range block’s configuration panel.
Figure 8.28
The Random block generates a random number within a specified range.
However, you can assign an input value by typing it in the Test value entry field.
The range against which the input value is compared can be supplied via data
wires (not shown by default on the Range block), or they can be supplied by the
Operation attribute. When configuring this attribute, you can choose between
determining whether the input value is inside (default) or outside the range. To
specify the range, you can either type its upper and lower values in the two entry
fields or use the slider control to specify them.
Figure 8.29
The Random block’s configuration panel.
Figure 8.30
The Variable block retrieves or modifies a value stored in computer memory.
Figure 8.31
New variables are defined from within the Edit Variables window.
Figure 8.32
The Variable block’s configuration panel.
It should be noted that variables exist only in the NXT Brick’s memory. When
the NXT Brick is turned off, the variable is deleted from memory and you will
have to define it again to use it.
Once defined, you can use the Variable block to assign a value to the variable
and to retrieve that value. Figure 8.32 shows the configuration panel for the
Variable block. Three attributes, described below, are available.
List
Lists all available variables and allows you to select one.
184 Chapter 8 n Advanced Programming Techniques
Action
Specifies whether the block will be used to read the variable’s value or to write a
new value in memory for the variable.
Value
The Value attribute is accessible only when the Action attribute is set to Write. It
allows you to assign a value of True or False to a logic variable, a number to a
numeric variable, or to enter a text string to be assigned to a text variable.
Figure 8.33
The Constant block retrieves a constant value stored in computer memory.
Programming Blocks That Use Data Wires 185
Figure 8.34
New constants are defined from within the Edit Constants window.
Figure 8.35
The Constant block’s configuration panel.
You can use the Constant block to retrieve the value assigned to the constant
whenever you need it. Figure 8.35 shows the configuration panel for the
Constant block. Four attributes, described below, are available.
Action
The Action attributes allow you to either choose the constant you want to work
with from a list of existing constants or to use the block to define a new constant.
186 Chapter 8 n Advanced Programming Techniques
Data Type
The Data Type attribute, available only when using the block to define a new
constant, is used to specify either Logic, Number, or Text as the constant’s data
type.
Value
The Value attribute, available only when using the block to define a new
constant, allows you to specify a value for the constant.
Name
The Name attribute, available only when using the block to define a new
constant, allows you to assign a name to the new constant.
Figure 8.36
The Number to Text block converts a number to a text string allowing it to be displayed on the NXT
Brick’s LCD.
Programming Blocks That Use Data Wires 187
As shown in Figure 8.37, the Number to Text block has a single attribute named
Number. Normally, the number that the block converts into text is passed to it
via data wire. Alternatively, you can specify the number to be converted by
typing it into the Number entry field.
Figure 8.37
The Number to Text block’s configuration panel.
Figure 8.38
The Text block concatenates up to three strings together to create a larger string.
Figure 8.39
The Text block’s configuration panel.
188 Chapter 8 n Advanced Programming Techniques
it via data wire. However, you can specify the text strings by typing them into
the three text fields labeled A, B, and C.
Figure 8.40
The Keep Alive block prevents the NXT Brick from entering Sleep Mode.
Figure 8.41
The File Access block writes and reads data from files stored on the NXT Brick.
Programming Blocks That Use Data Wires 189
Figure 8.42
The File Access block’s configuration panel.
Figure 8.42 shows the configuration panel for the File Access block. Its attributes
are described below.
Action
The Action attribute specifies the actions performed by the block. Available
choices include:
n Read—Reads the data stored in the specified file.
n Write—Writes data to the specified file. If the target file already contains
data, the new data is written to the end of the existing file.
n Close—Closes a file after it has been written to or read from.
n Delete—Deletes the file.
Hint
You must use the Close action after each time you perform an action on a file. For example, if you
write something to a file, you have to close it before you can read from it. You will need to add a
different File Access block to your NXT-G program for each action you perform.
Name
The Name attribute is used to specify the name of the data file that will be
created, processed, deleted, or closed.
File
The File attribute is available only when your NXT Brick is connected to your
computer. It allows you to select an existing file stored on your NXT Brick.
Type
The Type attribute is used to specify the type of data in use (text or numeric).
190 Chapter 8 n Advanced Programming Techniques
Text
Although when writing to a file the data to be written is usually supplied via data
wire, you can specify the data to be written by keying it into the entry field for
the Text attribute. The Text attribute is available only when the Action is set to
Write.
Hint
In addition to calibrating sensors using the Calibrate block, you can also calibrate sensors from the
Lego Mindstorms NXT 2.0 GUI, as described in Chapter 2, “Getting Started.”
Figure 8.44 shows the configuration panel for the Calibrate block. Its attributes
are described below.
Figure 8.43
The Calibrate block is used to calibrate sound and light sensors.
Figure 8.44
The Calibrate block’s configuration panel.
Programming Blocks That Use Data Wires 191
Port
The Port attribute specifies the port on the NXT Brick to which the sensor being
configured has been attached (1, 2, 3, or 4).
Sensor
The Sensor attribute specifies whether the programming block is to be used to
calibrate a light or sound sensor.
Action
The Action attribute is used to specify whether the block will calibrate or delete
the specified sensor. Deleting a sensor simply restores its default settings.
Value
The Value attribute is used to specify either the minimum (0) or maximum (100) value.
Figure 8.45
The Reset Motor block disables automatic error correction for servo motors.
Figure 8.46
The Reset Motor block’s configuration panel.
192 Chapter 8 n Advanced Programming Techniques
Figure 8.47
The Bluetooth Connection block is used to establish a Bluetooth connection with another Bluetooth
device.
My Block that someone else has developed and which you have downloaded
from the Internet, allowing you to leverage someone’s programming knowledge
and experience. Lego Mindstorms NXT 2.0 does not come with any My Blocks
or Web Blocks.
Creating a My Block
To create a My Block, add and configure the programming blocks you want to
use to the work area. For example, you might create a My Block using the
programming blocks shown in Figure 8.48.
Once you have finished configuring the programming blocks, select them and
either click on the Create My Block toolbar button or click on Edit > Make A
New My Block. In response, the My Block Builder window appears. Assign a
descriptive name to the block by filling in the Block Name field and then
document the block’s function and purpose in the Block Description text field as
demonstrated in Figure 8.49.
When done, click on Next and the My Block Builder window will help you
create a custom icon for the My Block. The bottom part of the window displays
a list of icons that you can drag to the Icon Builder entry field at the top of the
window. You can resize the icon by clicking one of the four squares located at
Figure 8.48
A collection of programming blocks to be used as the basis for creating a My Block.
194 Chapter 8 n Advanced Programming Techniques
Figure 8.49
The My Block Builder window displays the selected programming blocks and allows you to name and
describe the My Block.
the four corners of the icon. A preview of how the My Block’s icon will look,
located in the top right corner of the window, is immediately updated. You may
drag and drop more than one icon onto the Icon Builder entry field, as
demonstrated in Figure 8.50.
Once you are done configuring the appearance of your My Block, click on
Finish.
The programming blocks that make up the My Block are then removed from
the work area and replaced with the new My Block, as demonstrated in
Figure 8.51.
The My Block is automatically added to the Custom palette. To view it, simply
move the mouse pointer over the My Block icon, as demonstrated in Figure 8.52.
Creating Custom Functions by Creating My Blocks 195
Figure 8.50
You can add and configure one or more icons to the Icon Builder entry area in order to configure the
appearance of your My Block.
Figure 8.51
Once completed, the My Block is displayed in place of the programming blocks that comprise it.
196 Chapter 8 n Advanced Programming Techniques
Figure 8.52
The My Block is made visible on the My Blocks group on the Custom palette.
Modifying a My Block
If you decide you need to make a change to a My Block, you may do so by
adding it to the work area and either double clicking it or selecting it and then
clicking on Edit > Edit Selected My Block. In response, the contents of the My
Block are displayed in their own tabbed window in the work area. Make any
required changes to the programming blocks that make up the My Block and
then close its tabbed window. Click on Yes when prompted to save your
changes.
You can also modify a My Blocks icon by selecting it and then clicking on Edit >
Edit My Block Icon. My Blocks are stored in their own folder as files with an .rbt
file extension. You can delete a My Block by selecting it and then clicking on
Creating Custom Functions by Creating My Blocks 197
Edit > Manage Custom Palette. This will display a window containing two
folders, one for your My Blocks and one for your Web Blocks. Open the My
Blocks folder, select your My Block file and then press the Delete key to delete it.
Deleting a My Block
To delete a My Block, add it to the work area and then click on Edit > Manage
Custom Palette. Locate and open the My Blocks folder, select the My Block, and
press the Delete key. If you delete a My Block, you must then go back and
modify any NXT-G programs that made use of it. Otherwise, those programs
won’t work any more. Figure 8.53 shows an example of what a broken My Block
looks like.
Figure 8.53
An example of a broken My Block.
198 Chapter 8 n Advanced Programming Techniques
that is displayed. Drag and drop your new Web Block(s) into this folder and
close it.
The next time you place the mouse pointer over the Web Blocks group on the
Custom palette, your new Web Blocks will be displayed, and you can begin
working with them.
part of your NXT-G program where an error is hidden, just by watching your
robot run and listening for the tones that tell you when different parts of your
NXT-G programs are executing.
In a similar fashion, you can embed extra Display blocks within your NXT-G
programs when you are developing them and use those blocks to display
relevant information when your NXT-G programs run. Information that is
displayed might include data that is passed between blocks via data wires or
messages that indicate variable values. You can also use Display blocks in place
of Sound blocks to identify when different parts of your NXT-G programs
execute.
Another way to determine what is going on as your robots run is to leave your
robot’s NXT Bricks attached to your computer when it runs. This allows you to
monitor feedback boxes belonging to programming blocks.
Summary
This chapter rounded out your NXT-G programming foundation by completing
your review of NXT-G programming blocks, including those in the Action,
Sensor, Flow, and Data groups. You learned how to transmit data through these
programming blocks using data hubs and data wires. You also learned how to
create custom functions that facilitate code reuse through the development of
My Blocks. Lastly, you learned a few tricks for tracking programming execution
and locating and fixing program errors. You are now ready to begin developing
the NXT-G programs needed to automate the operation of the robots whose
development is covered in Chapters 9 through 11.
This page intentionally left blank
Chapter 9
Go Bot
Now it’s time to put everything you have learned so far in this book to good use.
In this chapter you will learn how to create your first robotic project, the Go Bot,
a four-wheeled robot. It uses two servo motors to drive it. It will be programmed
to move forward and backward, talk, and display friendly text messages. Go Bot
is a great first robotic development project, providing an effective example of the
fundamental steps involved in robotic development. It will also provide you with
the opportunity to put your programming skills to good use.
The major topics covered in this chapter include:
n Developing a basic understanding of how to create modular robots.
n A review of Go Bot’s features and capabilities.
n Step-by-step instructions for building Go Bot.
n Learning how to remote control Go Bot.
n A complete review of the NXT-G program that makes Go Bot come to
life.
201
202 Chapter 9 n Go Bot
Hint
When you’re done with a robot that you are proud of, it’s a good idea to document what you have
accomplished. This can be done as easily as snapping a picture of your robot, or it can be as
complex as developing a complete set of build instructions, which you can create using various
CAD programs like the Lego Digital Designer or LDRAW. You might even consider creating a video
of your robot’s assembly and operation.
Introducing Go Bot
Moving vehicles are a popular type of robot and make for an excellent starting
point. Go Bot is such a robot. As shown in Figure 9.1, Go Bot is a four-wheeled
robotic vehicle. The two front wheels are connected to and operated by servo
motors and are responsible for the vehicle’s propulsion. The back two wheels
provide additional support.
Go Bot will be programmed to begin by saying and displaying the word Hello on
the NXT Brick’s LCD. Next it will play a tone, move forward, pause, and play
another tone. Go Bot will then move backward to its starting point, play a tone,
and spin in place before saying Goodbye and halting.
Hint
The Go Bot can be used as the basis for creating all sorts of other vehicles. It will serve as the
starting point for the robotic projects in Chapters 10 and 11.
Building Go Bot 203
Figure 9.1
The Go Bot is a four-wheeled robotic vehicle.
Building Go Bot
Go Bot is a relatively simple and straightforward robotic project. You will create
it in six high level steps as outlined below.
n Preparing the NXT Brick
n Assembling the left motor drive
n Assembling the right motor drive
n Assembling the rear wheel chassis
n Assembling Go Bot
n Programming and testing Go Bot
Figure 9.2 provides a complete listing of all the different electronic components
and parts needed to make Go Bot. Begin your work on this project by retrieving
these pieces from your Lego Mindstorms NXT 2.0 kit and setting them aside to
work with.
204 Chapter 9 n Go Bot
Figure 9.2
The parts inventory list for Go Bot.
Step 1—Take two 5M straight beams, four connector peg with friction 3M pegs,
and two 3 5M perpendicular beams and connect them as shown in Figure 9.3.
Figure 9.3
Step 2—Take the components created in Step 1 and connect them to the NXT
Brick as shown in Figure 9.4.
Figure 9.4
Step 1—Create the robot’s left wheel by connecting a rim to a tire as shown in
Figure 9.5.
Figure 9.5
Step 2—Connect one connector peg with friction and cross axles and six
connector pegs with friction to one of the servo motors as shown in Figure 9.6.
Figure 9.6
Step 3—Connect the completed wheel to the servo motor using one 7M cross
axle and two bushings as shown in Figure 9.7.
Building Go Bot 207
Figure 9.7
Figure 9.8
Hint
The completed left motor drive should look like the example shown in Figure 9.9.
Figure 9.9
208 Chapter 9 n Go Bot
Figure 9.10
Step 2—Connect one connector peg with friction and cross axles and six
connector pegs with friction to one of the robot’s servo motors as shown in
Figure 9.11.
Figure 9.11
Building Go Bot 209
Step 3—Connect the completed wheel to the servo motor using one 7M cross
axle and two bushings as shown in Figure 9.12.
Figure 9.12
Figure 9.13
Hint
The completed right motor drive should look like the example shown in Figure 9.14.
210 Chapter 9 n Go Bot
Figure 9.14
Figure 9.15
Building Go Bot 211
Step 2—Connect two 3 5M perpendicular beams to the upper left and right
ends of the chassis using a connector peg with friction and connector peg with
friction and cross axle, as shown in Figure 9.16.
Figure 9.16
Step 3—Connect a 3M straight beam to the left side of the rear chassis using a
connector peg with friction and connector peg with friction and cross axle, as
shown in Figure 9.17. Add a second 3M straight beam to the right side of the
rear chassis using a second connector peg with friction and connector peg with
friction and cross axle (not shown in Figure 9.17).
Figure 9.17
Step 4—Assemble a new wheel using a rim and tire and add the wheel to the
rear chassis using a 5M cross axle, two bushings, and two half-bushings as
shown in Figure 9.18.
212 Chapter 9 n Go Bot
Figure 9.18
Hint
The completed rear wheel chassis should look like the example shown in Figure 9.19.
Figure 9.19
Assembling Go Bot
Now that all of the major subassemblies that make up Go Bot have been put
together, it is time to assemble these subassemblies into a completed robot.
Step 1—Attach the left and right servo motor assemblies to the NXT Brick, as
shown in Figure 9.20.
Building Go Bot 213
Figure 9.20
Step 2—As shown in Figure 9.21, connect the robot’s rear wheel chassis to the
robot by connecting its 3 5M perpendicular beam to the left and right servo
motors using the two sets of connector pegs with friction that are already
connected to the servo motors.
Figure 9.21
214 Chapter 9 n Go Bot
Hint
A fully assembled copy of Go Bot is shown in Figure 9.22.
Figure 9.22
Step 3—To operate, the robot’s two servo motors must be connected to the
NXT Brick. Set this up by connecting the left servo motor to the NXT Brick’s C
port and the right servo motor to the NXT Brick’s B port using a pair of 35cm/
14-inch cables, as shown in Figure 9.23.
Figure 9.23
Building Go Bot 215
Programming Go Bot
Now that Go Bot has been assembled, it is time to begin work on the NXT-G
program that will bring it to life. The NXT-G program itself will be relatively
simple. It will provide instructions to the Go Bot to perform the following
actions:
n Drive forward
n Drive backward
n Spin in place
n Talk
n Display different text messages
The NXT-G program will be created in three distinct stages. In the first stage,
the programming blocks needed to control the robot’s movement will be added
to the program and configured. The robot’s operation will then be tested to
ensure that things work as they should. Next, Sound and Display blocks will be
added to the NXT-G program to give the robot some personality. This will
complete the development of the robot’s programming. The robot’s operations
will again be tested. Lastly, you will add comments to the NXT-G program that
document its operation, and then you will perform one last test of the robot’s
operations.
Figure 9.24
A simple starting version of the Go Bot’s NXT-G program.
review each of the programming blocks that make up the NXT-G program and
ensure that their configuration matches up with the configuration settings
specified for each block. Once you have found and corrected any block
configuration errors, download and retest Go Bot’s operation before proceeding
to the second stage of the program’s development.
Insert another Sound block just after the third of the three Move blocks. Select
the Sound block and configure it as follows:
n Action—Select Sound File.
n Control—Select Play.
n Volume—Set value level to 75 percent.
n Function—Ensure that Repeat is not selected.
n File—Select Goodbye from the list of files in the scrolling list box.
n Wait—Select Wait for Completion.
Next, add a Display block to the end of the sequence beam and configure it as
follows:
n Action—Select Text from the drop-down list.
n Display—Select Clear.
n Text—Type Goodbye in the text entry field.
n Position—Specify a value of 30 for X and 29 for Y.
Lastly, add a Sensor Wait block at the end of the sequence beam. Select the
Sensor block and configure it as follows:
n Control—Select Time from the drop-down list.
n Until—Type a value of 1 in the Seconds entry field.
Figure 9.25 shows how the NXT-G program should now look. Save the program
and then turn on and connect the robot (e.g. NXT Brick) to your computer and
download the program by clicking on the NXT Download button located at the
bottom right corner of the work area. Once the NXT-G program has been
downloaded, disconnect Go Bot and place it on the floor in an area free of
Figure 9.25
This version of the Go_Bot NXT-G program contains all of the robot’s programming logic.
220 Chapter 9 n Go Bot
obstacles. Press on the NXT Brick’s orange button four times to select and run
the NXT-G program.
Before continuing to the final stage of the NXT-G program’s development, test
the robot and make sure it operates as previously described.
Figure 9.26
The final version of the Go_Bot NXT-G program.
Remote Controlling Go Bot 221
Hint
In order to be able to make effective use of the Remote Control feature, you either need a relatively
long USB cable or a wireless Bluetooth connection.
For the remote control to work, the NXT Brick must be powered on and a
connection must exist between the robot (e.g. NXT Brick) and your computer.
Otherwise, the Remote Control window will appear grayed out and you will not
be able to access any of the controls shown on it. Often, Lego Mindstorms NXT
2.0 automatically detects and establishes a connection with the NXT Brick.
However, sometimes you may have to do it yourself. To do so, click on the
Connections button and then select your NXT Brick and click on the Con-
nections button followed by the Close button.
With a connection established, use your mouse to click on the Forward, Backward,
Left, and Right button to remotely control the movement of Go Bot. Alternatively,
Figure 9.27
Using the Remote Control window to operate Go Bot.
222 Chapter 9 n Go Bot
you can use the keyboard’s Up, Down, Right, and Left keys to control Go Bot’s
movement. You can adjust the speed at which Go Bot moves by clicking on either
the Increase Speed or Decrease Speed buttons or by clicking directly on the graphic
speed meter to indicate the speed at which you want Go Bot to move.
Summary
This chapter showed you how to create your first robotic creation, Go Bot. In
developing Go Bot, you learned the fundamental steps involved in robotic
development: design, build, program, and test. This chapter explained the
importance of breaking down robot development into subassemblies. You also
learned how to develop Go Bot’s NXT-G program in several stages, testing the
robot’s operation at the end of each stage. Finally, you learned how to use the
Remote Control window to manually take control of Go Bot.
Chapter 10
Tracker Bot
In this chapter, you will learn how to build and program your second robot. This
robot, called Tracker Bot, is a modified version of the robot you created in
Chapter 9, “Go Bot.” Tracker Bot differs from Go Bot in two distinct ways. First,
Tracker Bot includes a color sensor that it uses to locate and follow a dark line
wherever it may go. Second, Tracker Bot is operated by a totally new NXT-G
program, which provides Tracker Bot with the intelligence needed to perform its
mission: find and follow the line.
The major topics covered in this chapter include:
n A review of the features and capabilities of Tracker Bot
n Detailed building instructions for Tracker Bot
n Instructions for developing the NXT-G program that operates Tracker
Bot
223
224 Chapter 10 n Tracker Bot
Figure 10.1
Tracker Bot uses the color sensor in light sensor mode to detect its target path.
Figure 10.2
Go Bot serves as the structural foundation upon which Tracker Bot is built.
commonly used design and building technique that Lego Robotic developers
take advantage of.
Tracker Bot is created in five steps, outlined here:
n Preparing the color sensor for connection
n Assembling support beams
n Completing the sensor assembly
n Connecting the sensor assembly to the robot
n Programming Tracker Bot
Figure 10.3 provides a complete listing of all the different parts needed to
upgrade Go Bot to Tracker Bot. Begin your work on this project by retrieving Go
Bot and then getting the pieces pictured in Figure 10.3 from your Lego
Mindstorms NXT 2.0 kit.
Figure 10.3
The parts inventory list for Tracker Bot.
226 Chapter 10 n Tracker Bot
Figure 10.4
Figure 10.5
Building Tracker Bot 227
Figure 10.6
Hint
Figure 10.7 shows how the color sensor should look now that it has been prepared for connection
to the sensor assembly.
Figure 10.7
Step 1: Take a 13M straight beam and connect a connector peg with friction and
cross axle to it and then take a 2M straight beam and connect a connector peg
with friction and cross axle and a connector peg with friction 3M to it, as shown
in Figure 10.8.
Figure 10.8
Step 2: Connect the two beams used in Step 1 together as shown in Figure 10.9.
Figure 10.9
Step 3: Take another 13M straight beam and connect a connector peg with
friction and cross axle to it and then take another 2M straight beam and connect
a connector peg with friction and cross axle and a connector peg with friction
3M to it, as shown in Figure 10.10.
Building Tracker Bot 229
Figure 10.10
Step 4: Connect the two beams used in Step 3 together as shown in Figure 10.11.
Figure 10.11
Figure 10.12
Step 2: The final step in the development of the sensor assembly is to connect a
12M cross axle to the bottom of the assembly, as shown in Figure 10.13.
Figure 10.13
Hint
Figure 10.14 shows how the sensor assembly should look once all subcomponents have been
connected.
Figure 10.14
Building Tracker Bot 231
Figure 10.15
Hint
Figure 10.16 shows an example of how Tracker Bot should look once it is fully assembled.
Figure 10.16
232 Chapter 10 n Tracker Bot
Step 2: In order to take advantage of its new sensor assembly attachment, the
color sensor must be connected to the NXT Brick. Set this up by connecting the
color sensor to the NXT Brick’s number 3 port using a 35cm/14-inch cable, as
shown in Figure 10.17.
Figure 10.17
you should put Tracker Bot through its first test run, which you can do using the
Mindstorms NXT 2.0 Test Pad that came with your Lego Mindstorms NXT 2.0
kit. Just place the robot on the pad and point it in the direction of the circular
black track line that is drawn across the entire pad.
Next, you will add Sound blocks, a Wait block, and a second Loop block to the
NXT-G program in order to provide its sound effects. This will complete the
development of the robot’s programming, allowing you to put it through a
second round of testing. The third and final stage of program development
consists of documenting the NXT-G program through the addition of comments
that explain the program’s operation. When done adding the comments, you
should, of course, put the robot through one last test of its operation.
The Move blocks located in the lower half of the Switch block are executed when
“dark” value (< 40) is detected. The first of these two Move blocks powers the
servo motor attached to the NXT Brick’s B port, moving the robot to the left.
Select it and configure it as follows:
n Port—Select port B.
n Direction—Select Forward.
n Steering—This option will be disabled.
n Power—Set power to 75 percent.
n Duration—Select Unlimited.
n Next Action—This option will be disabled.
The second Move block in the lower half of the Switch block powers the servo
motor attached to the NXT Brick C port, moving the robot a little to the right.
Select it and configure it as follows:
n Port—Select port C.
n Direction—Select Forward.
n Steering—This option will be disabled.
n Power—Set power to 15 percent.
n Duration—Select Unlimited.
n Next Action—This option will be disabled.
Figure 10.18 shows how the NXT-G program should look at this point. Save the
program. Connect Tracker Bot (e.g. the NXT Brick) to your computer and
download the program by clicking on the NXT Download button located at the
bottom right corner of the work area. Once the NXT-G program has been
downloaded, disconnect Tracker Bot and place it on the Mindstorms NXT 2.0
Test Pad, pointing it in the general direction of the large dark oval drawn on the
pad. Press on the NXT Brick’s orange button four times to select and run the
NXT-G program.
Ensure that Tracker Bot is able to locate and begin following the dark oval line.
If Tracker Bot detects and then loses track of the line, restart the program,
236 Chapter 10 n Tracker Bot
Figure 10.18
This version of the Tracker_Bot program enables the robot’s basic execution.
adjusting its starting position, putting it a little more in line with the robot’s
starting direction. If Tracker Bot does not perform correctly, go back and review
each of the programming blocks that make up the NXT-G program and ensure
that their configuration matches up with the configuration settings specified for
each block and then update, download, and retest the robot again.
Begin by inserting a Loop block on the sequence beam immediately before the
Loop block. Configure it to repeat its execution three times by selecting it and
configuring it as follows:
n Control—Select Count from the drop-down list.
n Until—Set Count to 3.
n Show—Leave the Counter option disabled.
Drag and drop a Sound block inside the Loop blocks that you just configured.
Select the Sound block and configure it as follows:
n Action—Select Sound File.
n Control—Select Play.
n Volume—Set value level to 75 percent.
n Function—Ensure that Repeat is not selected.
n File—Select !Sonar from the list of files in the scrolling list box.
n Wait—Select Wait for Completion.
Hint
Sound files with the ! character as the first character in their file name are played sound effects. All
other sound files represent spoken words.
Tracker Bot will now begin its execution after playing a sonar sound three times.
Next, add a Sensor Wait block to the main sequence beam, placing it between
the NXT-G program’s two loops. Select the Wait block and configure it as
follows.
n Control—Select Time from the drop-down list.
n Until—Type a value of 2 in the Seconds entry field.
Next, add a final Sound block to the NXT-G program, placing it immediately
before the second Loop block. Select the Sound block and configure it as follows.
n Action—Select Sound File.
n Control—Select Play.
238 Chapter 10 n Tracker Bot
Figure 10.19
A more robust version of the Tracker Bot program.
Figure 10.20
The final version of the Tracker_Bot NXT-G program.
Summary
This chapter took you step-by-step through the steps required to modify and
enhance Go Bot and turn it into Tracker Bot. In doing so, you gained practical
experience working with the color sensor. You also got the chance to stretch
your NXT-G programming skills, employing conditional logic and a loop, as you
wrote the program file that tied everything together.
This page intentionally left blank
Chapter 11
Detector Bot
This chapter’s robotic project is Detector Bot. Detector Bot is a modified version
of Tracker Bot. Its job is to seek out an object located at the end of its path. It
features a new assembly that includes a touch sensor. This allows the robot to
detect when it comes into contact with its target object. With this new sensor
assembly in place, the robot will be able to finally reach the end of its quest,
following its path until it finds its objective.
The major topics covered in this chapter include:
n A review of the features and capabilities of Detector Bot
n Detailed building instructions for Detector Bot
n Instructions for developing the NXT-G program that operates Detector
Bot
241
242 Chapter 11 n Detector Bot
Figure 11.1
Detector Bot uses a touch sensor to determine when it has found its target.
Figure 11.1 shows how Detector Bot will look when it is complete. Note the
addition of the touch sensor located in the front of the robot as well as the use of
a cross axle and two teeth that extend the ability of the touch sensor to detect
objects along a wider path.
Figure 11.2
Tracker Bot will serve as the basis for building Detector Bot.
Building Detector Bot 243
Figure 11.3
The parts inventory list for Detector Bot.
244 Chapter 11 n Detector Bot
Figure 11.4
Step 2: Attach two connector pegs with friction 3M to two 3M straight beams, as
shown in Figure 11.5.
Figure 11.5
Step 3: Take the two 3M straight beams and connect them to the touch
sensor using the already attached connector pegs with friction 3M, as shown in
Figure 11.6.
Building Detector Bot 245
Figure 11.6
Hint
Figure 11.7 shows how the touch sensor looks now that it has been prepared for connection to the
sensor assembly.
Figure 11.7
Step 1: Place the left and right screens upside down on a flat surface and connect
a connector peg with friction and a connector peg with friction 3M to each
screen, as shown in Figure 11.8.
Figure 11.8
Step 2: Place the touch sensor in an upright position between the two screens
and connect the screens to the sensor, as shown in Figure 11.9.
Figure 11.9
Hint
Figure 11.10 shows how the touch sensor looks now that it has been connected to the left screen
and the right screen.
Building Detector Bot 247
Figure 11.10
Figure 11.11
Step 2: Connect the support beams to the touch sensor assembly, as shown in
Figure 11.12.
248 Chapter 11 n Detector Bot
Figure 11.12
Hint
Figure 11.13 shows how the touch sensor assembly looks now that its support beams have
been added.
Figure 11.13
Step 1: Insert a 9M cross axle into a catch with cross hole and then center
the catch on the axle. Add a pair of teeth to the end of the 9M cross axle and
connect a 3M cross axle to the open end of the catch with cross hole, as shown in
Figure 11.14.
Figure 11.14
Step 2: Connect the component you assembled in the previous step to the touch
sensor assembly, as shown in Figure 11.15.
Figure 11.15
Hint
Figure 11.16 shows how the touch sensor assembly looks now that it has been completely
assembled.
250 Chapter 11 n Detector Bot
Figure 11.16
Figure 11.17
Building Detector Bot 251
Hint
When properly attached, the touch sensor can be raised or lowered. When lowered, it should float
approximately a quarter inch above the surface of the ground.
Step 2: Using a 20cm/8-inch cable, connect the touch sensor to Port 1 on the
NXT Brick, as shown in Figure 11.18.
Figure 11.18
n Play a Game Over message and display corresponding text if the robot
does not make contact with another object within 60 seconds.
As with previous projects, Detector Bot’s NXT-G program will be developed in a
series of stages. In the first stage, Tracker Bot’s program will be copied and
renamed. In the second stage, the program’s programming logic will be updated
to include the collection and analysis of touch sensor input and to halt program
execution if appropriate. Once this stage is complete, you will put Detector Bot
through its first test run to ensure that the program enhancements work
correctly.
In the third stage of the NXT-G program’s development, you will add
programming logic that announces the end of the program’s execution (when
60 seconds has expired without the robot detecting its target object). You will
once again test the robot’s operation, this time to validate that the program shuts
down the robot’s execution, as just described, when time expires. The fourth and
final stage of the NXT-G program’s development enhances the program through
the addition of comments.
Figure 11.19
Tracker Bot’s NXT_G program serves as the starting point in the development of the Detector Bot’s
program.
half of the Switch block, placing it after the Sound block. Select the Display block
and configure it as follows.
n Action—Select Text from the drop-down list.
n Display—Select Clear.
n Text—Type Object found in the text field entry box.
n Position—Specify a value of 8 for the X coordinate and 32 for the Y
coordinate.
Drag and drop a Sensor Wait block at the end of the sequence beam. Select the
Sensor block and configure it as follows:
n Control—Select Time from the drop-down list.
n Until—Type a value of 1 in the Seconds entry field.
Next, drag and drop a Stop block into the Switch block, placing it after the
Display block. The Stop block has no configuration panel. Its purpose is to halt
the execution of the NXT-G program.
Figure 11.20 shows how the programming logic outlined in the program’s loop
should look at this point in the NXT-G program’s development.
Figure 11.20
The programming logic in the NXT-G program’s loop has been enhanced to capture touch sensor data
and to control early program termination.
Building Detector Bot 255
Before continuing to the final stage of the NXT-G program’s development, set
aside some time to test the robot and make sure it operates as previously
described. Start by saving the program. Next, connect the robot (e.g., the NXT
Brick) to your computer and download the program by clicking on the NXT
Download button located at the bottom right corner of the work area.
Disconnect Tracker Bot, place it on the Mindstorms NXT 2.0 Test Pad and
press on the NXT Brick’s orange button four times to select and run the NXT-G
program. Once you have verified that the robot has successfully located and
begun tracking the path outlined by the solid block line, place an object like a
book or soda can in Detector Bot’s path and make sure the robot detects the
object and then halts.
If Detector Bot does not perform as expected, go back and review each of the
programming blocks that you added to the new Switch block that you inserted
inside the program Loop block and ensure that their configuration matches up
with the configuration settings specified for each block. If you find any errors in
block configuration, correct the errors and then download, and retest the robot
again.
Figure 11.21
If 60 seconds pass with no collision, the NXT-G stops its execution and announces that it has failed to
locate its target.
Wrap things up by dragging and dropping a Display block onto the end of the
sequence beam, placing it just after the Sound block that you just configured.
Select the Display block and configure it as follows.
n Action—Select Text from the drop-down list.
n Display—Select Clear.
n Text—Type Object not found in the text field entry box.
n Position—Specify a value of 3 for the X coordinate and 28 for the
Y coordinate.
At this point, you have completed the development of the NXT-G program’s
programming logic. Figure 11.21 shows how the two programming blocks that
you just added should look.
Figure 11.22
The final version of the Detect_Bot NXT-G program.
Summary
In this chapter you learned how to enhance Tracker Bot, turning it into Detector
Bot through the addition of a new touch sensor assembly and the extension of its
NXT-G program. Detector Bot sill locates and follows a dark line wherever it
may go. However, the robot now has the ability to determine when it comes into
contact with another object, allowing it to ascertain when it has found its target
object and completed its mission.
This page intentionally left blank
Chapter 12
Alarm Bot
Figure 12.1
Alarm Bot’s electronic components consist of two sensors and a servo motor.
Figure 12.2
Alarm Bot uses the ultrasonic sensor to detect when an object comes within 48 inches of the robot.
Figure 12.3
The parts inventory list for Alarm Bot.
Step 1: Attach two 11M straight beams to the right side of the NXT Brick using
four connector pegs with friction.
Step 2: Connect the lower end of the two 11M straight beams to a 13M straight
beam using two more connector pegs with friction.
Building Alarm Bot 263
Step 3: Connect one additional connector peg with friction directly to the NXT
Brick, as shown in Figure 12.4.
Figure 12.4
Step 4: Attach two more 11M straight beams to the left side of the NXT Brick
using four connector pegs with friction.
Step 5: Connect the lower end of the two 11M straight beams to a 13M straight
beam using two more connector pegs with friction, as shown in Figure 12.5.
Figure 12.5
264 Chapter 12 n Alarm Bot
Figure 12.6
Figure 12.7
Step 3: Connect the pair of 3M pegged blocks to the three 5M straight beams, as
shown in Figure 12.8.
Building Alarm Bot 265
Figure 12.8
Hint
Figure 12.9 shows how the support platform looks once it is assembled.
Figure 12.9
Figure 12.10
Figure 12.11
Figure 12.12
Hint
Figure 12.13 shows how the support struts look after they are added to the framework base.
Figure 12.13
268 Chapter 12 n Alarm Bot
Figure 12.14
Step 2: As you attach the servo motor to the 3M pegged block, make sure you
also connect the servo motor to the two support struts, as shown in Figure 12.15.
Hint
Figure 12.16 shows how the robot should look now that the servo motor has been attached.
Building Alarm Bot 269
Figure 12.15
Figure 12.16
Step 3: Finish securing the servo motor to the robot by capping the two 4M
cross axles at the top of the support struts with two half-bushings, as shown in
Figure 12.17.
270 Chapter 12 n Alarm Bot
Figure 12.17
Figure 12.18
Building Alarm Bot 271
Step 2: Place the ultrasonic sensor on its side, push an 8M cross axle with end
stop all the way through the 3M cross block, and then lock the 8M cross axle
with end stop into place using a half-bushing, as shown in Figure 12.19.
Figure 12.19
Step 3: Attach a connector peg with friction to each of the two design shells, as
shown in Figure 12.20, and then use the connector pegs with friction to connect
the two design shells to the ultrasonic sensor.
Figure 12.20
272 Chapter 12 n Alarm Bot
Step 4: Connect the ultrasonic sensor subassembly to the robot by inserting the
open end of the 8M cross axle with end stop into the crosshair connection
located in the center of the servo motor’s upper side, as shown in Figure 12.21.
Figure 12.21
Figure 12.22
Building Alarm Bot 273
Figure 12.23
Figure 12.24
274 Chapter 12 n Alarm Bot
Hint
Figure 12.25 shows how Alarm Bot looks after it is fully assembled.
Figure 12.25
Figure 12.26
servo motor is developed. The third stage of the NXT-G program’s development
involves the addition of comments that document the NXT-G program and its
operation.
Add a second loop to the main sequence beam, placing it within the upper half
of the Switch block. Programming blocks in the upper half of the Switch block
execute whenever the robot detects an intruder. Select it and configure it as
follows:
n Control—Select Count from the drop-down list.
n Until—Configure the loop to repeat two times every time it is executed
by typing a value of 2 in the Count entry field.
n Show—Leave the Counter option unselected.
Drag and drop another Color Lamp block onto the main sequence beam, placing
it with the loop located in the upper half of the Switch block. Select and
configure the Color Lamp block as follows:
n Port—Select the number 3 port.
n Action—Turn on the sensor lamp by selecting the On option.
n Color—Select the green lamp color.
Drag and drop a second Color Lamp block into the upper half of the Switch
block. Select and configure the Color Lamp block as follows:
n Port—Select the number 3 port.
n Action—Turn on the sensor lamp by selecting the On option.
n Color—Select the blue lamp color.
Drag and drop a third Color Lamp block into the upper half of the Switch block.
Select and configure the Color Lamp block as follows:
n Port—Select the number 3 port.
n Action—Turn on the sensor lamp by selecting the On option.
n Color—Select the red lamp color.
Drag a Sound block into the upper half of the Switch block. Select and configure
the Sound block as follows:
n Action—Select Sound File.
278 Chapter 12 n Alarm Bot
n Control—Select Play.
n Volume—Set value level to 100 percent.
n Function—Ensure that Repeat is not selected.
n File—Select Alarm from the list of files in the scrolling list box.
n Wait—Select Wait for Completion.
Drag another Sound block into the upper half of the Switch block. Select and
configure the Sound block as follows:
n Action—Select Sound File.
n Control—Select Play.
n Volume—Set value level to 100 percent.
n Function—Ensure that Repeat is not selected.
n File—Select Object Detected from the list of files in the scrolling list box.
n Wait—Select Wait for Completion.
Finally, drag and drop one more Color Lamp block onto the bottom half of the
Switch block. This block executes when the robot does not detect an intruder.
Select the Color Lamp block and configure it as follows:
n Port—Select the number 3 port.
n Action—Turn on the sensor lamp by selecting the Off option.
n Color—Select the red lamp color.
Figure 12.27 shows how the programming logic outlined in the program’s loop
should look at this point in the NXT-G program’s development.
Before moving on to the next stage of the NXT-G program’s development, take a
little time to test the robot and make sure that when the NXT-G program runs
its ultrasonic sensor operates properly. Start by saving the program. Next,
connect the robot (e.g., the NXT Brick) to your computer and download the
program by clicking on the NXT Download button located at the bottom right
corner of the work area. Disconnect Alarm Bot, place it in the center of the room
with no other obstacles within 48 inches of the direction that the ultrasonic
sensor is pointing. Press the NXT Brick’s orange button four times to select and
Building Alarm Bot 279
Figure 12.27
These programming blocks manage the operation of the robot’s ultrasonic sensor.
run the NXT-G program. Nothing should happen. Next, move within 48 inches
of the front of the ultrasonic sensor. It should detect you and signal the alarm.
Move out of range of the ultrasonic sensor and the alarm should stop sounding.
If Alarm Bot does not perform as expected, go back and review each of the
programming blocks that you added to the program and ensure that their
configuration matches up with the configuration settings specified for each
block. If you find any errors in block configuration, correct the errors and then
download and retest the robot again.
Figure 12.28
The Alarm_Bot program now uses the sensor motor to rotate the ultrasonic sensor.
Next, disconnect Alarm Bot and place it in the center of the room with no other
obstacles within 48 inches of the direction that the ultrasonic sensor is pointing.
Press the NXT Brick’s orange button four times to select and run the NXT-G
program. Nothing should happen. However, when you move within 48 inches of
the front of the ultrasonic sensor from any direction, it should detect you and
signal the alarm, and when you move out of range of the ultrasonic sensor, the
alarm should stop sounding.
Figure 12.29
The final version of the Alarm_Bot NXT-G program.
Summary
In this chapter, you learned how to create the book’s final robot development
project, Alarm Bot. This robot sounds an alarm whenever it detects an object
within four feet of it. In doing so, you gained practical experience working with
the ultrasonic sensor, which is mounted on top of an 8M cross axle and
repeatedly rotated a full 360 degrees in order to allow the robot to detect
intruders approaching from any direction. You also learned how to work with
the color sensor as a lamp, displaying a series of different colored lights
whenever an intruder is detected and the alarm is sounded.
Appendix A
Congratulations on making it all the way to the end of this book. At this point
you should have a solid understanding of how to use Lego Mindstorms NXT 2.0
to create and program all kinds of exciting robotic creations. With this
foundation now in place, you are well positioned to continue the development
of your robotic design and program development skills.
There is plenty left to learn, so don’t look at completing this book as the end of
your education, but rather consider it to be the beginning. To join the ranks of
advanced robotic developers, you need to keep learning everything you can
about Lego Mindstorms NXT 2.0, and most importantly, you need to keep
experimenting and building all kinds of new robotic creations. The only limit to
what you can do is your own imagination.
As you continue to experiment and try your hand with different robotic
creations, you will amass quite an inventory of creation and NXT-G source
code. You can use the designs and source code that you create as the basis for
even more complex and challenging creations. This way, you can borrow design
ideas and source code and use them to model new creations based on existing
ones. This will save you a lot of time, sweat, and tears because you won’t have to
reinvent the wheel every time you begin a new development project.
Assuming that you have re-created all of the robotic creations that have been
presented in this book, you should already have a lot of good experience
designing and programming your creations. You should faithfully document
283
284 Appendix A n What’s on the Companion Web Site?
and add to this collection every time you create a new project. You’ll come to
appreciate the advantages and time savings from practicing this habit and will
come to view these resources as indispensible.
Chapter 9 Go_Bot.rbt
Chapter 10 Tracker_Bot.rbt
Chapter 11 Detect_Bot.rbt
Chapter 12 Alarm_Bot.rbt
Appendix B
What’s Next?
As you no doubt have learned, robotic development and programming are not
only challenging but a whole lot of fun. While this book has taught you a lot
about Lego Mindstorms NXT 2.0 and how to use it to develop all sorts of robotic
creations, there is a lot more to this development framework than can possibly
be covered in any one book. The purpose of this appendix is to provide you with
a collection of online resources to which you can turn for more information
about Lego Mindstorms NXT 2.0. In addition, you will find resources listed for
Lego in general and LDraw, the CAD (Computer Aided Design) software used
in this book to generate 3D images and design instructions. With it, you can
create documentation for your own robotic creations.
285
286 Appendix B n What’s Next?
Figure B.1
The Lego Mindstorms web site provides information and hosts activities and communication for a global
community of Mindstorms enthusiasts.
Mindstorms NXT 2.0. You can sign up for a free Lego Mindstorms newsletter
and participate in different building challenges. You can watch short movies,
view fan-built projects, post and read questions and answers on forums, and
even download free sounds for use in your projects.
Figure B.2
The Mindstorms page at the open directory project.
.org/Kids_and_Teens/Sports_and_Hobbies/Toys/Lego/Mindstorms//, as shown
in Figure B.2. Here you will find links to all kinds of topics.
Figure B.3
This blog facilitates an ongoing global conversation for Lego Mindstorms NXT enthusiasts.
The site also provides access to the NXT STEP Forum, where you can participate
in a host of different types of discussions.
Figure B.4
Visit this site to get free instructions for building all kinds of different robots.
complete with color building instructions. You can even download the programs
needed to bring these robots to life.
Figure B.5
The Wikipedia Lego Mindstorms page is developed and maintained by a worldwide community of robotic
developers dedicated to sharing information.
Figure B.6
Be sure to check out the Forums section at nxtasy.org.
Figure B.7
If you need more parts than came with your kit, you can buy them from the Lego Shop.
Lego Resources
While you can create untold number of robots using just the pieces and parts
that come with the Lego Mindstorms NXT 2.0 kit, there is no reason for you to
limit yourself to just these resources. If you want, you can incorporate other
types of Lego blocks and pieces into your creations, further expanding the types
of things you can build.
Figure B.8
There is no reason that you could not incorporate regular Lego blocks into your robotic creations.
which is amazingly extensive. To learn more about Lego, you should visit http://
www.lego.com, as shown in Figure B.8. Here you will find information about
Lego bricks, play sets, Mindstorms, and much more.
Lego Club
There is no better way to stay in touch with the greater Lego community than by
joining the Lego Club, located at http://club.lego.com, as shown in Figure B.9.
294 Appendix B n What’s Next?
Figure B.9
The Lego club is the place where Lego lovers around the world congregate and share information.
Here you can play games, post and read questions and answers on the site’s
message boards, and sign up for the free club magazine.
Figure B.10
Wikipedia’s Lego page provides an excellent historical overview of Lego’s origins.
Figure B.11
Wikipedia’s LDraw page provides a good overview of LDraw.
Developing Instructions for Your Robotic Creations 297
LDraw.org
LDraw’s main web site is www.ldraw.org, as shown in Figure B.12. This site
provides a history of LDraw and provides information about books, online
resources, and all things LDraw. You can download and install LDraw from this
site. Instructions are provided for installing LDraw. There are also tutorials that
you can read to learn all about LDraw and its operation.
Figure B.12
LDraw.org is central headquarters for keeping up with all things related to LDraw.
298 Appendix B n What’s Next?
Figure B.13
LEGO Digital Designer is easy to install and operate.
Figure B.14
Visit www.tech-publishing.com to learn more about game development and to provide your feedback
on this book.
Angle Connector. Used to connect two axles and to create 90- or 180-degree
connections.
Angular Beam. A beam in which one or more sections are angled.
Attributes. Programming block properties or modifiers that can be configured
to customize the programming block’s execution.
Axle. A cross-shaped shaft that can be used to connect to any Mindstorms piece
that has a cross-hole connection.
Block Import and Export Wizard. A utility program that provides the ability
to import new programming blocks and to export custom-developed
programming blocks.
Bluetooth. A wireless networking technology that can be used to wirelessly
download your NXT-G programs into the NXT Brick.
Bluetooth Connection Block. A programming block that establishes a con-
nection to another Bluetooth device or disables/enables Bluetooth function-
ality.
Bushing. Used to hold an axle firmly in position, locking other pieces that may
be connected to the axle in place.
Calibrate Block. A programming block that calibrates the minimum and
maximum values detected by sound/light sensors.
301
302 Glossary
Create Pack and Go. A utility program that creates a Pack and Go file
facilitating development functions like My Blocks, Display Blocks, and
Sound Blocks with other NXT developers.
Cross Axle Extension. Connects two axles in order to establish a longer axle.
Cross Block. A Lego Mindstorms NXT 2.0 piece that combines circular and
cross-hole connections.
Custom Palette. A tab on the Programming palette that provides access to
blocks that you create or upload into Mindstorms NXT 2.0.
Data. A piece of information collected, stored, modified, and processed during
application execution.
Data Hub. A projection that slides down from the bottom left side of a
programming block which contains one or more data plugs that allow the
programming block to send and receive data.
Data Wire. A connection between one block’s output data plug and another
block’s input data plug.
Developer Profile. An organizational mechanism whereby NXT-G pro-
grammers are only presented with programs that they develop.
Display Block. A programming block that displays text, shapes, and images on
the NXT Brick’s LCD.
Driven Gear. The last gear in the gear train.
Driver Gear. The gear that is responsible for transferring motion to the other
gears in a gear train.
Endless Loop. A loop that does not have a predefined means of terminating its
own execution.
File Access Block. A programming block that saves data to files on your NXT
Brick.
FIRST LEGO League (FLL). A worldwide robotic design competition started in
1999 for the purpose of getting children interested in science and technology.
Flexible Axle Damper 2M Connector. Connects axles that run in parallel;
supports robotic creations that require flexibility.
304 Glossary
Flow. The order in which program blocks are executed within a NXT-G
program.
Flowchart. A graphical depiction of some or all of a program’s logic.
Friction Peg. A peg that firmly holds its position within a round hole.
Gear. Used to transmit motion (five different types of gears).
Gear Ratio. Describes the rotation of the driver gear relative to the driven gear.
Gear Train. The use of two or more gears together.
Gearing Down. Occurs when you either decrease the size of the driver gear or
increase the size of the driven gear.
Gearing Up. Occurs when you either increase the size of the driver gear or
reduce the size of the driven gear.
IDE (Integrated Development Environment). A graphical application devel-
opment environment designed to facilitate program development.
Idler Gear. Gears located between the driver gear and the driven gear in a great
train.
Image Editor. A utility program that lets you convert and download graphic
files onto your NXT Brick, where they can then be loaded and displayed on
the Brick’s LCD screen.
Input Data Plug. Used to accept data passed to the programming blocks.
Integrated Development Environment (IDE). A collection of computer pro-
grams that execute from a single user interface.
Keep Alive Block. A programming block that prevents the NXT Brick from
going to sleep.
Knob Gear. A gear with four circular knobs that rotate around its axis.
Lego Mindstorms NXT. A robotics kit developed by Lego in 2006, which was
the predecessor to the Lego Mindstorms 2.0 kit.
Lego Mindstorms NXT 2.0. A robotics kit developed by Lego in August 2009.
Light Sensor Block. A programming block that detects ambient light.
Liquid Crystal Display (LCD). A monochrome display on the NXT Brick that
is 100 pixels wide by 64 pixels high.
Glossary 305
Little Help Window. Displays information about the currently selected object
as well as a link to additional information about the object in the Lego
Mindstorms NXT 2.0 help file.
Logic Block. A programming block that reviews inputs and returns a true/false
value.
Logic Data. A term used to represent data that has either of two values: true or
false.
Loop. A collection of one or more programming blocks that are repeatedly
executed.
Loop Block. A programming block that repeats the execution of specified
programming blocks.
Magazine. Holds balls for using with robots that shoot.
Math Block. A programming block that performs arithmetic, subtraction,
multiplication, and division operations.
Mesh. The interlocking of gear teeth that facilitates the transference of motion
between gears.
Microcomputer. A small digital computer designed to be used by one person at
a time.
Module. A unit of measurement (8mm) that is used to identify the length of
beams and other parts.
Motor Block. A programming block that provides precise control of a motor’s
speed.
Move Block. A programming block that moves a robotic creation forward or
backward.
My Block. A collection of one or more customized programming blocks that
perform a specific task.
My Portal. A window that provides a gateway to the http://www.mindstorms.
com web site, providing access to information on new models, program files,
and sound and image files.
Number to Text Block. A programming block that takes a number and turns it
into a displayable text string.
306 Glossary
Steering Link. Establishes a connection with either the friction ball peg or the
smooth axle ball peg.
Stop Block. A programming block that halts program execution and any
running motors.
Stop Ridge. A notch on an axle or peg that determines how far an axle or peg
can go into a hole.
Straight Beam. A Lego Mindstorms NXT 2.0 piece that is perforated with
circular holes which run down the middle of the beam and which has a
smooth exterior and rounded ends.
String. A set of characters that can be passed between programming blocks and
displayed on the NXT Brick’s LCD.
Switch Block. A programming block that chooses between two alternate
courses of action based on the analysis of sensor or input value.
T-Beam. A beam that is T-shaped and is 3M wide and 3M tall.
Tachometer. A component within a sensor motor that precisely controls the
rotation of the motors.
Text Block. A programming block that adds text strings together in order to
create a longer text string.
Timer Block. A programming block that reads the timer’s current value or
restarts the timer.
Tire. Used to create cars and any other type of wheeled vehicle or machine
(tires are made to fit snuggly around the rims).
Touch Sensor. Provides robots with a sense of touch, allowing them to feel and
react to things around them.
Touch Sensor Block. A programming block that sends a true/false signal
through a data wire based on the current condition of a touch sensor.
Track. Ribbed rubber banks used to make things like tanks, construction
vehicles, and other machines.
Ultrasonic Sensor. Provides robots with the ability to see objects and to
determine their distance.
Glossary 309
311
312 Index