Any

Advanced: NXT Tutorial by Dale Yocum

Note: this content, created by Dale Yocum has been copied (with permission) from http://www.ortop.org/NXT_Tutorial/html/advanced.html on July 20, 2011.

 

Essentials: NXT Tutorial by Dale Yocum

Note: this content, created by Dale Yocum, has been copied (with permission) from http://www.ortop.org/NXT_Tutorial/html/essentials.html on July 20, 2011.

 

These lessons assume you have a basic robot built.  See the RoboCenter or Robot Educator in the NXT Editor software for plans or use the paper copy that came with your set.

 

 

NXT Tutorial by Dale Yocum

Note: this content, created by Dale Yocum, has been copied (with permission) from http://www.ortop.org/NXT_Tutorial on July 20, 2011.

 

Welcome to this multimedia tutorial for the NXT-G graphical language of the LEGO® Mindstorms® NXT system.

Waddle Bot Unit 1 Lesson 4

Incrementing Integers/Counters

 

Sometimes, you will want to be able to count how many times you have gone through a loop. Using a while loop and an integer variable, you can add this functionality. Without the ability to control the number of times a loop occurs, you would be forced to write out all of the commands each time you want to repeat. This leads to a needlessly long program and makes you a less effective programmer. The while loop is constructed like this:

Infinite Loops

 

The while command can only accept a single Boolean value. Whatever is inside the conditional statement will always be evaluated to a Boolean. If we want to force a while block to loop continuously, we can simply put a boolean into the while statement. Doing this with a TRUE value will cause something called in infinite loop. Since there is not a conditional statement to change the evaluation to FALSE, the code block will loop continuously. The while command would look something like this:

Logical Expressions

 

As long as the NXT is on a white board, it will run continuously until it runs into the first black line. While this is going on, the NXT can't do anything but what is included in the while code block. Let's say that we setup a wall around our board. We don't want the NXT to crash into the wall so we need to add a second condition to the while loop. The NXT should leave the while loop if it gets too close to a wall. This would result in the NXT stopping.

Wait for Condition

One of the most common ways to use a while loop is as a wait command. This is better than a simple wait1sec command because you can wait for almost any condition to be met. In our sample program, we use a while loop to wait for the NXT to cross a line. We start by moving the NXT forward. We then trap the NXT in a while loop until a line is detected. We should leave this section blank, but it is generally a good idea to give yourself some feedback when you wait for a condition. Once the light sensor detects a line, the NXT will leave the while loop and run into our stop commands.

while (condition) {}

 

A while loops acts in a very similar manner to an IF statement. Where an IF statement would run the attached code block only once, an IF loop will run the same code multiple times. As long as the conditional statement evaluates to TRUE as the start of the loop, the NXT remains in the loop. If isn't until the conditional statement evaluates to FALSE will it skip the code block and move onto the next part of the program.

Waddle Bot Unit 1 Lesson 3

Pages