Concept-based Lesson: cbl test 3

Printer-friendly version
Overview: 

This is the overview for the lesson.

Objectives: 

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.
Lesson: 

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:

U1L1-TaskMain

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.

Concepts: 

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 */ “