Introduction: Lego Robotics Parts and Programming
The NXT-G Programming Environment
Write a simple program to make the brick play a sound
Connect the USB cable between the brick and the computer.
Select the Sound brick from the Action Blocks, and select a Sound file to play.

The 'Play Sound' Program
The Sound Block Settings
Select "Save" from the file menu, and save your program with the name "Play Sound".
Press the "Download and Run" button. Did it play?
Unplug your brick from the computer. See if you can find the program named "Play Sound" on the brick and play it directly from the brick.
Congratulations! You just wrote and ran your first NXT-G program!
Change the "Play Sound" program to repeat itself 3 times using a loop
Select the "Loop" brick from the Flow Blocks and put it after the "Sound" block
Put the "Sound" block from before inside the loop, and set the loop to repeat 3 times.

The Sound block inside a Loop
The Loop Block settings
- Download and play the"Play Sound" program. Did it do what you expected?
Building a Door Alarm
The parts you'll need:
Step 1: Attach connectors to the arms.
Step 2: Attach the arms together, and connect them to the ultrasonic sensor and the brick.
Step 3: Connect a wire from the ultrasonic sensor to port 4 on the brick.
The Door Alarm Program
Start with a Loop (from the Common Blocks List). Set the Loop to run "Forever"

The Loop Block
The Loop Block settingsPut a Wait Block (from the Common Blocks List) inside the loop. Set the control to "Sensor" and select "Ultrasonic Sensor" from the Sensor list. Make sure the Port is set to "4" (where the Ultrasonic Sensor is plugged in), and the Distance is set to "< 16 Inches".

A Wait Block inside the Loop Block
The Wait Block settingsAdd a loop after the Wait block, and set the control to "Count" and the "Count" to "3".

A loop inside the main loop
The inner Loop settingsPut a sound block inside the inner loop and choose a sound to play when the alarm triggers,

The finished Door Alarm programSave the program with the name "Door Alarm". Download and test your program. Did it work? Check with your instructors if you have any problems.
If you finish the door alarm try this!
(1) Pretty Tricky: Can you change the program so that the alarm makes a different sound if the object it detects is closer than 6 inches? Hint: You will have to use a "Switch" block to perform different actions if the object is closer than 6 inches, or further than 6 inches. Think carefully about where to put the Switch block, and if some of the existing program needs to go inside the Switch block.
(2) Even Trickier: Can you change the program so that the screen on the brick displays the distance detected? This will require a block that will convert the distance (a number) to text (letters) to be displayed on the screen and a block to display the text. It also requires the use of data wires to carry the output data from the ultrasonic sensor to the converter block, and from the converter block to the display block. Don't worry if you can't do this one yet, we'll be covering data wires later in the class.
(see your instructors for help with the answers)