Any

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

If Statements

 

The IF statement is the most basic logic command that all other logical structure is based on. This gives our NXT the ability to make choices based off of data. In a basic IF statement, a conditional statement determines if a block of code is executed or not. If the statement is TRUE, the block is executed, if the statement is FALSE, the block is skipped. Let's look at an example:

View Sensor Values on NXT

It's important to note that the value for the "threshold" variable was not picked at random. That number was picked because we tested the different regions on the field for what value the sensor will output. The NXT provides menu options that allow you to see the current sensor value. Use the arrow buttons to change your selection and the orange button to pick the selected menu option.

Conditional Statements

 

Computer logic is based around the ability of a computer to compare integers. Even the most complex choice can be boiled down to being able to determining if one number is greater than, less than, or equal to another number. This behavior forces us to form our logical instructions in a way that can be answered through evaluating two numbers. The result of this evaluation is a Boolean.

Pages