Variables form a very important aspect of every programming language. Variables are memory locations in
which we can store a value. We can use that value at different places and we can change it. Let us describe the
use of variables using an example.
Moving in a spiral
Assume we want to adapt the above program in such a way that the robot drives in a spiral. This can be achieved
by making the time we sleep larger for each next straight movement. That is, we want to increase the value of
each time. But how can we do this?
is a constant and therefore cannot be changed. We
need a variable instead. Variables can easily be defined in NXC. Here is the spiral program.
#define TURN_TIME 360
int move_time; // define a variable
move_time = 200; // set the initial value
Wait(move_time); // use the variable for sleeping
move_time += 200; // increase the variable
The interesting lines are indicated with the comments. First we define a variable by typing the keyword int
followed by a name we choose. (Normally we use lower-case letters for variable names and uppercase letters for
constants, but this is not necessary.) The name must start with a letter but can contain digits and the underscore
sign. No other symbols are allowed. (The same applied to constants, task names, etc.) The word int stands for
integer. Only integer numbers can be stored in it. In the second line we assign the value 200 to the variable.
From this moment on, whenever you use the variable, its value will be 200. Now follows the repeat loop in
which we use the variable to indicate the time to sleep and, at the end of the loop we increase the value of the
variable by 200. So the first time the robot sleeps 200 ms, the second time 400 ms, the third time 600 ms, and so
Besides adding values to a variable we can also multiply a variable with a number using
, subtract using
and divide using
. (Note that for division the result is rounded to the nearest integer.) You can also add one
variable to the other, and write down more complicated expressions. The next example does not have any effect
on your robot hardware, since we don't know how to use the NXT display yet!
C# Imaging - C# Code 93 Generator Tutorial
NET web application and WinForms program using Visual C# code in png, jpeg, gif, bmp, TIFF, PDF, Word, Excel 1D bar codes on images & documents in specific area. break pdf password; cannot select text in pdf
aaa = 10;
bbb = 20 * 5;
ccc = bbb;
ccc /= aaa;
ccc -= 5;
aaa = 10 * (ccc + 3); // aaa is now equal to 80
ArrayInit(values, 0, 10); // allocate 10 elements = 0
values = aaa;
values = bbb;
values = aaa*bbb;
values = ccc;
Note on the first two lines that we can define multiple variables in one line. We could also have combined all
three of them in one line. The variable named values is an array, that is, a variable that contains more than a
number: an array can be indexed with a number inside square brackets. In NXC integer arrays are declared so:
Then, this line allocates 10 elements initializing them to 0.
ArrayInit(values, 0, 10);
In all the above programs we defined exactly what the robot was supposed to do. But things get a lot more
interesting when the robot is going to do things that we don't know. We want some randomness in the motions.
In NXC you can create random numbers. The following program uses this to let the robot drive around in a
random way. It constantly drives forwards for a random amount of time and then makes a random turn.
int move_time, turn_time;
move_time = Random(600);
turn_time = Random(400);
The program defines two variables, and then assigns random numbers to them.
means a random
number between 0 and 600 (the maximum value is not included in the range of numbers returned). Each time
you call Random the numbers will be different.
Note that we could avoid the use of the variables by writing directly e.g.
You also see a new type of loop here. Rather that using the repeat statement we wrote
. The while
statement repeats the statements below it as long as the condition between the parentheses is true. The special
Generate and draw QR Code for Java
and can be printed with any printer even the is installed and valid for implementation to print QR Code Build a Java barcode object for a specific barcode type can't select text in pdf file; break a pdf into parts
is always true, so the statements between the brackets are repeated forever (or at least until you press
the dark grey button on NXT). You will learn more about the while statement in Chapter IV.
In this chapter you learned about the use of variables and arrays. You can declare other data types than int:
short, long, byte, bool and string.
You also learned how to create random numbers, such that you can give the robot unpredictable behavior.
Finally we saw the use of the while statement to make an infinite loop that goes on forever.
In the previous chapters we saw the repeat and while statements. These statements control the way the other
statements in the program are executed. They are called “control structures”. In this chapter we will see some
other control structures.
The if statement
Sometimes you want that a particular part of your program is only executed in certain situations. In this case the
if statement is used. Let me give an example. We will again change the program we have been working with so
far, but with a new twist. We want the robot to drive along a straight line and then either make a left or a right
turn. To do this we need random numbers again. We pick a random number that is either positive or negative. If
the number is less than 0 we make a right turn; otherwise we make a left turn. Here is the program:
#define MOVE_TIME 500
#define TURN_TIME 360
if (Random() >= 0)
The if statement looks a bit like the while statement. If the condition between the parentheses is true the part
between the brackets is executed. Otherwise, the part between the brackets after the word
is executed. Let
us look a bit better at the condition we use. It reads
Random() >= 0
. This means that
greater-than or equal to 0 to make the condition true. You can compare values in different ways. Here are the
most important ones:
smaller than or equal to
larger than or equal to
not equal to
You can combine conditions use
, which means “and”, or
, which means “or”. Here are some examples of
ttt != 3
true when ttt is not equal to 3
(ttt >= 5) && (ttt <= 10)
true when ttt lies between 5 and 10
(aaa == 10) || (bbb == 10)
true if either aaa or bbb (or both) are equal to 10
Note that the if statement has two parts. The part immediately after the condition, which is executed when the
condition is true, and the part after the else, which is executed when the condition is false. The keyword else and
the part after it are optional. So you can omit them if there is nothing to do when the condition is false.
The do statement
There is another control structure, the do statement. It has the following form:
The statements between the brackets after the do part are executed as long as the condition is true. The condition
has the same form as in the if statement described above. Here is an example of a program. The robot runs
around randomly for 20 seconds and then stops.
int move_time, turn_time, total_time;
total_time = 0;
move_time = Random(1000);
turn_time = Random(1000);
total_time += move_time;
total_time += turn_time;
while (total_time < 20000);
Note also that the do statement behaves almost the same as the while statement. But in the while statement the
condition is tested before executing the statements, while in the do statement the condition is tested at the end.
For the while statement, the statements might never be executed, but for the do statement they are executed at
In this chapter we have seen two new control structures: the if statement and the do statement. Together with the
repeat statement and the while statement they are the statements that control the way in which the program is
executed. It is very important that you understand what they do. So better try some more examples yourself
Off course you can connect sensors to NXT to make the robot react to external events. Before I can show how to
do this, we must change the robot a bit by adding a touch sensor. As before, follow the Tribot instructions to
build the front bumper.
Connect the touch sensor to input 1 on the NXT.
Waiting for a sensor
Let us start with a very simple program in which the robot drives forwards until it hits something. Here it is:
until (SENSOR_1 == 1);
There are two important lines here. The first line of the program tells the robot what type of sensor we use.
is the number of the input to which we connected the sensor. The other sensor inputs are called
indicates that this is a touch sensor. For the light sensor we would use
After we specified the type of the sensor, the program switches on both motors and the robot starts moving
forwards. The next statement is a very useful construction. It waits until the condition between the brackets is
true. This condition says that the value of the sensor
must be 1, which means that the sensor is
pressed. As long as the sensor is not pressed, the value is 0. So this statement waits until the sensor is pressed.
Then we switch off the motors and the task is finished.
Acting on a touch sensor
Let us now try to make the robot avoid obstacles. Whenever the robot hits an object, we let it move back a bit,
make a turn, and then continue. Here is the program:
if (SENSOR_1 == 1)
OnRev(OUT_AC, 75); Wait(300);
OnFwd(OUT_A, 75); Wait(300);
As in the previous example, we first indicate the type of the sensor. Next the robot starts moving forwards. In the
infinite while loop we constantly test whether the sensor is touched and, if so, move back for 300ms, turn right
for 300ms, and then continue forwards again.
Besides the touch sensor, you also get a light sensor, a sound sensor and a digital ultrasonic sensor with
Mindstorms NXT system. The light sensor can be triggered to emit light or not, so you can measure the amount
of reflected light or ambient light in a particular direction. Measuring reflected light is particularly useful when
making a robot follow a line on the floor. This is what we are going to do in the next example. To go on with
experiments, finish building Tribot. Connect light sensor to input 3, sound sensor to input 2 and ultrasonic sensor
to input 4, as indicated by instructions.
We also need the test pad with the black track that comes with the NXT set. The basic principle of line following
is that the robot keeps trying to stay right on the border of the black line, turning away from line if the light level
is too low (and sensor is in the middle of the line) and turning towards the line if the sensor is out of the track
and detects a high light level. Here is a very simple program doing line following with a single light threshold
#define THRESHOLD 40
if (Sensor(IN_3) > THRESHOLD)
until(Sensor(IN_3) <= THRESHOLD);
The program first configures port 3 as a light sensor. Next it sets the robot to move forwards and goes into an
infinite loop. Whenever the light value is bigger than 40 (we use a constant here such that this can be adapted
easily, because it depends a lot on the surrounding light) we reverse one motor and wait till we are on the track
As you will see when you execute the program, the motion is not very smooth. Try adding a
command before the until command to make the robot move better. Note that the program does not work for
moving counter-clockwise. To enable motion along arbitrary path a much more complicated program is required.
To read ambient light intensity with led off, configure sensor as follows:
Using the sound sensor you can transform your expensive NXT set into a clapper! We are going to write a
program that waits for a loud sound, and drives the robot until another sound is detected. Attach the sound sensor
to port 2, as described in Tribot instructions guide.
Documents you may be interested
Documents you may be interested