RobotC, What does it all mean?

In my last post on RobotC, I introduced programing in RobotC with the following piece of code:

 task main() {
     motor[left] = 100;
     motor[right] = 100;
     wait1Msec(1000);
 }

This time I want to examine this program line by line and explain exactly what each line is doing. I will do my best to explain this program in the simplest way possible, but unfortunately some things are best explained in comparison to programming in C. Here are two resources I’ve used in the past for details on C

1. Programming in C.

2. Learn C the Hard Way.

Don’t be afraid of the title of the second link, while it does eventually go into some more advanced topics it starts nearly from scratch, but it does assume some previous programming experience.

Now back to RobotC, starting with line 1:


task main() {

This declares the main “task”, this is in contrast to standard C where main is a function and is declared as


int main() {

In this piece of code “int” is short for integer a positive or negative whole number (or 0) with a range that depends on the computer you are using, but typically from −2,147,483,648 to 2,147,483,647.

In C main is a function that roughly contains all of the work that your program will do, in RobotC main serves the same purpose except that it is declared as a task. The opening brace ({) just indicates the beginning of the body of the main task/function, which ends on line 5 with a corresponding closing brace (}).

Remember I mentioned last time that we are assuming that we have already used the RobotC IDE to setup the motors on our robot, so that we have two motors one labelled left and one labelled right. Moving on to lines 2 and 3:

     motor[left] = 100;
     motor[right] = 100;

In this snipped of code motor[left] indicates that motor is what is called an array in C, I will first give a brief and very basic discussion of arrays in C and then tie that back in with RobotC. In C we can declare an array (say of integers) like this:


int an_array_of_ints[5] = {0,1,2,3,4};

In this we declare an array called “an_array_of_ints” (similar to “motor” in the RobotC example) which can hold 5 integers (ints). An array is sort of like a list of things (in this case ints) that will hold a specific and predetermined number of them (in this case 5). The braces here are the items that we are storing in the array. We can access an item in an array as follows, suppose we want an expression which will add together all the things in our array of ints.


an_array_of_ints[0] + an_array_of_ints[1] + an_array_of_ints[2] + an_array_of_ints[3] + an_array_of_ints[4];

This expression is identical to 1+2+3+4+5; notice that the first item of the array begins at index 0 and the last at index 4, this is just the way that the C programming language was designed, so all arrays are 0-indexed (begin at 0). We can also change the value of one of our array items say the third (that would be index 2, not 3!) like


an_array_of_ints[2] = 7;

So now our array contains these five numbers {0,1,7,3,4}.

I think we know enough now to explain what is going on in the Robot C example.

     motor[left] = 100;
     motor[right] = 100;

So motor is an array that is declared for us by the RobotC programming environment to allow it’s values to alter the power level of the motors attached to our robot. When we assign the value 100 to motor[left] and motor[right] when we compile and run our program on the NXT motors left and right will run at 100% power.

So last, but not least let’s address line 4 of our program:


wait1Msec(1000);

In this lat bit of code “wait1Msec” is a function applied to the argument 1000, I will cover functions, and how you can define your own in a later post, but they are (mostly) similar  to functions from math. In this case “wait1Msec” is a function that waits or pauses the program for it’s argument value in miliseconds, in this case 1000 miliseconds or 1 second. This allows the program to run for long enough to allow the motors to actually be set to 100% power. Without this function call everything happens so quickly that it will appear as if the program is doing nothing, this wait enables you to see that the motors do run and everything will work correctly.

After the function call above, the program ends with a closing brace (}) corresponding to the opening brace denoting the body of the main task. This concludes the program ending its run causing the motors and anything else to cease running.

Hopefully this post gives a better look into the specific workings and details of RobotC. I will continue this series on RobotC programming introducing “if-statements”, loops, servos, and functions.
If you have any questions please don’t hesitate to ask, and please point them out if you notice any errors in either my code or explanations.

1 thought on “RobotC, What does it all mean?

  1. Pingback: Working with Joysticks. | danfeltey

Leave a comment