The Display
The NXT \brick" has a display that is 100 pixels wide and 64 pixels high. Unlike the latestand greatest game consoles, the display is monochrome, meaning that a particular pixel is
either on or o. While simple, the display provides an invaluable tool for communicating
information from within a running program.
Hello World!
An old tradition in computer science is the \Hello World!" program (HWP). The HWP is
a simple program whose primary purpose is to introduce the programmer to the details of
writing, saving, compiling, and running a program. It helps the programmer learn the ins
and outs of the system they will be using. Our HWP will print the words \Hello World!" to
the NXT display.
// Displays the words "Hello World !" on the NXT
// display for 5 seconds and exits.
task main () {
nxtDisplayString (4," Hello World !");
wait1Msec (5000);
}
To execute these instructions on the NXT, run the RobotC program. Type the text
exactly as it appears in Listing 3.1 into the editor window. Save your program under the
name \HelloWorld". Turn on the NXT brick and connect it to the USB port of the com-
puter. Under the Robot menu, choose Download Program. Behind the scenes, the HWP is
compiled and transferred to the NXT. Now, on the NXT, go to My Files ! Software Files
! HelloWorld ! HelloWorld Run. If successful, the words \Hello World!" will appear on
the display.
Program Dissection
Nearly every character in the HWP has meaning. The arrangement of the characters is
important so that the compiler can translate the program into machine language. The rules
of arrangement are called the syntax. If the syntax rules are violated, the compilation and
download step will fail and the compiler will try to suggest ways to correct the mistake.
To start, we have task main(), signifying that this is the rst section of instructions to
be executed. A program may have up to 10 tasks, but the main task always starts rst. The
open and close curly braces ({, }) enclose a block of instructions. Blocks will be discussed
later in the context of program variables.
The rst instruction is a call to the function nxtDisplayString(). Enclosed in the
parentheses are the arguments to the function. The rst argument, 4, species the line on
which to place the words (there are 8 lines labeled 0 through 7 from top to bottom). The
second argument, "Hello World!", enclosed in double quotes, is the collection of characters,
also known as a string, to be displayed. The instruction is delimited by a semi-colon, ;.
The delimiter makes it easy for the compiler to determine where one instruction ends and
the next one begins. All instructions must end with a semi-colon.
The second instruction is a call to the wait1Msec() function. This causes the program to
pause by the number of milliseconds (1 millisecond = 1/1000th of a second) specied in its
argument before proceeding to the next instruction. In this case, the program pauses 5,000
milliseconds (or 5 seconds) before proceeding. If this pause is not included, the program will
exit as soon as the string is displayed and it will seem as if the program does nothing at all.
The two lines at the top of Listing 3.1 are comments and are ignored by the compiler.
Comments are useful in large programs to remind us what is going on in a program or in a
particular section of the program. The characters // cause any characters that follow to the
end of the line to be ignored by the compiler. Additional information about comments in
RobotC is available here1.
No comments:
Post a Comment