Computing / Computer Science

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.

Booleans

 

Our third major data type is called a Boolean. This data type deals with logic and is based in the binary language at the heart of every computer. Binary is defined as something "composed of two pieces or parts". Booleans are the same as in it only has two states: True or False. A Boolean can not have any other value.

Sensor Value [ sLabel]

 

Motors and Sensors are very similar in robotC. They are both special variables that are used to interface with the attached devices. Where motors accept data inputs, sensors only output data. This sensor integer value represents a real world condition that the NXT is sensing. In order to act on that information, we have to interpret the data for the NXT. We will do that below in conditional statements. For now, let's look at the elements of the variable.

Sensor Configuration

 

If motors are the output of our programs, then sensors are the input. Sensors give the NXT the ability to become aware of the world around it. We can use this awareness to grant the NXT the intelligence to navigate a dynamically changing world.

As with motors, before we can use a sensor, we must first configure it. This is done using the same tool we used to setup the motors.

Waddle Bot Unit 1 Lesson 2

Pages