Concept-based Lesson: Waddle Bot Unit 1 Lesson 1

Printer-friendly version

  This introduction to the RobotC programming language begins to explore the basic tools and syntax. At the end of this lesson, students should be able to do the following:

  • Run and compile a simple program
  • Understand what a function is and how they work.
  • Identify the difference between strings and integers.

In all of our lessons, we will start with a sample program. It is generally a good idea to copy this program to your NXT and test it out. You can then experiment with the code and see how small changes affects your robot. Learning programming is not about doing things right the first time. It is about exploring what is possible through guided experimentation.

Let's get started with "Hello World". This program simply displays the words "Hello World" on the LCD screen.

  1. //Chapter 1 - Unit 1 - Hello World
  2. task main
  3. {
  4.   eraseDisplay(); //Clears display so we can write.
  5.   nxtDisplayString(1,"Hello World"); //Writes text to line 1
  6.   wait1Msec(2000); //Wait two seconds
  7. }

Task [string]label { } Introduction

Printer-friendly version

When you open a new RobotC program, you start with a blank slate, with no instructions. The NXT/RobotC was designed with a number of rules that force us to write or programs in a certain way. Our first rule of RobotC is:

1. NXT actions shall reside within tasks.

A task in the most simple of terms is a labeled container of instructions. By separating our instructions into tasks, we gain the ability to do multiple things at once. Tasks can be labeled anything so when you launch a program, the NXT looks for something called the "root task". This task is the only task that the NXT will automatically start. When this task ends, the NXT will close the program. The designers of this system determined that the task labeled "main" which brings us to our second rule:.

2. "task main {}" will always be the first task to be launched.

To create a task, we type the following:

The first part of any construct in RobotC is a "Command Declaration". This notifies the NXT that a particular construct is being created. In this case, we are creating a task and the NXT is now expecting certain elements to come next. The next element is the text label of the task followed by the encapsulated instructions that make up the task.

Encapsulation is when you put a symbol at the start and end of something. It acts as the boundaries of a container to relate the contents of the encapsulation with the preceding command. For tasks, we use curly braces to mark the start and end of the task. This brings us to our third rule.

3. Encapsulation will always consist of two symbols: one at the start to open, and one at the end to close.

Programming is a lot like cooking. If you have trouble conceptualizing what a task is, think of it as a recipe card. They are both a labeled set of instructions to follow. In cooking, you perform the task. In RobotC, the NXT does the work.

Comments and White Space

Printer-friendly version


RobotC is a free-form programming language. This means that when you write a program, multiple spaces, tabs, and line returns are ignored by the NXT. This allows us to use these ignored structures to format our program and make it easier to read. If wanted, we could put all of our commands on one line. Here is what the above example program would look like if we did:

  1. task main{eraseDisplay();nxtDisplayString(1,"Hello World");wait1Msec(2000);}

So in the interest of making our code easier to read, we put each command on new line.

Another thing that is ignored by the compiler is comments. Comments are essentially notes that we leave inside the program.  Most complex programs are created over the course of weeks or months. While you can write commands in a way that is easy to read, it can really help to have a quick explanation of what is going on. It is recommended that you add comments as you write your program.

There are two types of comments:

  • Single line – uses two forward slashes at the start of a line. “ // This is a comment ”
  • Multi line – uses a “slash star” to encapsulate your comment. “ /* This is a comment */ “

Primitive Data Types

Printer-friendly version


At the most general level, everything in a program can be separated into two general groups: Commands and Data. The main difference between these groups is the ability to store information. Commands can only perform actions while data is information that can be acted upon.

The main data types that we will work with are:

  • Integer – Whole numbers. Examples: 1, 5, 47, 100
  • String – Text characters. Examples: “Hello” , “NXT” , “Fifty-Five”

It is important to note that the NXT can only understand a data of a certain size. Limitations within the design limit the size of strings to 19 characters and integers to approximately 37,000. If your data is larger than the limit, you will need to break it into multiple chunks.

There are more data types out there, and we will explore them as we use them. Right now, Strings and integers will give us the ability to work with words and numbers.

Elements of a Function

Printer-friendly version

RobotC is considered a high-level language. This grants us access to a large library of functions to perform actions with. These functions contain a large number of instructions that translate our commands to something the NXT can understand. It is important to realize that the NXT doesn't understand words and numbers like we do. Programming for an NXT is like talking in another language. The rules of this language is called syntax. Each element has specific rules that allow the NXT to understand what you want it to do.

To understand the syntax of functions, let's take a look at the functions we used in the example.

-- wait1Msec --
  • Function Syntax: wait1Msec( [int] time );

  • Usage: Put task in idle state for a number (parameter1) of milliseconds.

We look at this function first because it is the most simple function that has all four elements of a function. Let's look at each element in turn.

  1. Function Name - This name allows the RobotC compiler look in its' library of functions and add the correct commands to the compiled program.
  2. Parameter - This Data controls the function. Wait1Msec requires an integer from 1 to 32,767. What this data does depends on the function that you are using. The number of parameters vary depending on the function.
  3. Encapsulation - Marks the start and stop of the parameter(s). All functions must have the encapsulation parentheses, even when there are no parameters.
  4. End Marker - The semicolon marks the end of the command. Because while space is ignored, semicolons are used to separate instructions.

Function Libraries

Printer-friendly version

Some functions require more than one parameter. The way you can how many parameters is needed is by looking at the documentation. The first source of information is the Function Library within RobotC. This will generate when you compile a program. RobotC will also hide some of the more advanced functions from this list. To see them you need to change your menu level.

While you can go to Super User for a complete list, you might be overwhelmed by the number of functions available to you. Once you have the proper menu level set, you can find the function in the library and hover your mouse over it for a brief example. Let's try this for your next function.

-- nxtDisplayString --
  • Function Syntax: nxtDisplayString( nLineNumber , sFormatString );

  • Usage: Display a string (2nd parameter) on the specified (1st parameter) line of the NXT LCD.

To better decode this tooltip, we also need an example of the function in use.

  1. nxtDisplayString(1,"Hello World");

If you match up the example with the tooltip, you can then see what parameters are controlling this function. When looking at the parameters in the tooltip, the first letter of the parameter determines the data type. "n" is for integers, and "s" is for strings. The rest of the text in the label is a hint on what that parameter controls. When working with unknown functions, you may have to experiment to figure out how a function works.

When we talk about using a function, we add simple notes on how to use it. For this function, the first parameter is labeled "nLineNumber". This means that the first parameter must be an integer and will designate what line the command will display on. The second parameter is labeled "sFormatString". This means the second parameter must be a string which is what the function will put on the screen. The other parameters, "param1" and "param2", are optional and are only used in certain situations. We'll come back to this in later lessons.

If you still need help with using a particular function, there are a number of communities online that can help. We mention this because there is not a good repository of function definitions available at this time. The function library can help you get started, but is not your only resource.

Functions Without Parameters

Printer-friendly version
-- eraseDisplay --
  • Function Syntax: eraseDisplay()
  • Usage: Clears the NXT Display.

The last function we will look at doesn't have a parameter. It is important to remember that even if you don't have any parameters, you still need to use parentheses. These symbols are expected by the compiler and it will throw an error if you don't have them. The reason we erase the display before we write is to make sure we don't have accidental overwrites. We'll talk about this more in later chapters.