This is the second part of a serie of tutorials called "First steps in programming". These tutorials are for people who has never programmed and people who has programmed and want to know how to do it in Lego Mindstorms NXT. We are going to be programming in NXC (Not eXactly C). This is a C-like programming language that has some great features and it's easy to learn, so don't be afraid!. I'm going to explain everything, so if you know C please have a little patience.
If you want to see the first tutorial "Installing the software" click here.
Part 2: First Lines
Connect your NXT turned on to your computer and open Bricx Command Center. Select NXT in the "Brick Type" and "Automatic" in Port. Now, click in 'file' and select New to create a new document.
Create a new document |
The variable's names can contain only letters, numbers and underscores ( _ ), but must begin with a letter. Also, the name can't contain spaces and can't be a reserved word. Here is the list of reserved words in NXC:
Don't use any of this words as a variable name |
type name = value;
The type tells to the computer what to expect. The most common types are int (an integer number), string (text), float (a number with decimals) and bool (true or false, don't worry about this yet). The semicolon MUST be at the end of every instruction that have your program, this tells to the computer that you're finishing a sentence or instruction.
So, let's write:
string textA = "Hello World!";This line creates a variable called textA that cotains text (string). Note that the names are case sensitive so textA is not the same as texta or TEXTA. When you write strings you have to put it in quotation marks.
Alright, let's create a task, this is a block of code that groups instructions and sentences. Every program need at least one task called "main". When you run the program, the task main will be executed automatically. Add the main task to your program:
string textA = "Hello World!";The syntaxis of the tasks are:
task main() { }
task name() {
program
}
The parentheses are used to pass parameters to the task. We'll discuss that later, so now just put the () at the end of the name. The { } indicates the limits of task, so every sentence inside of them will be part of 'main'. The spaces between words and lines are ignored by the program so you can write:
int string textA = "Hello World!" ;
We're creating a program that will show a text on the NXT brick screen. To do that we have to use some functions. Functions are a block of code that performs a specific task.
To put text in the screen is recommendable to clear it first, if you don't do it the text can be below other things on the screen. The function ClearScreen() do that:
string textA = "Hello World!";Note that if you put clearscreen(), the program will give you an error. Remember that we're working on a case sensitive lenguage. Now that we have our screen cleared, let's add the text. To do that we are going to use the function TextOut(). This function needs 3 paremeters:
task main() {
ClearScreen();
}
TextOut(int X coordinate, int Y coordinate, string Text)
X and Y are integer numbers that represent the position of the text in the screen. The third parameter is what we are going to show. I recommend you to use the NXT Mindstorms software to find the X and Y. Just add a Display block, select text and move it to the right position. Now look at the X and Y coordinates.
In the Mindstorms NXT software |
Perfect! We're going to show the string in our variable textA, so the code should look like this:
string textA = "Hello World!";If you run the program now, you will see nothing. That's because the program starts and ends too fast, to fix that, let's add the function Wait( time ). This function stop the program time milliseconds. We are going to put 5 seconds (5000 milliseconds):
task main() {
ClearScreen();
TextOut(16, 25, textA);
}
string textA = "Hello World!";Alright, our program is done. Now we have to compile it. This converts your code to a lenguage that the NXT Brick can understand, so you have to compile every program you do before download it into the brick.
task main() {
ClearScreen();
TextOut(16, 25, textA);
Wait(5000);
}
Click in the button 'Compile' in the menu and then click in "Run Program".
If you just want to download the program use the download button |
int coorX = 16;Play with the code! That's the way that this things are learned... Weeell, that's it for this tutorial.
int coorY= 25;
task main() {
ClearScreen();
TextOut(coorX, coorY, "Hello World!");
Wait(5000);
}
----
int coorX = 4*4;
int coorY= 20+5;
task main() {
ClearScreen();
TextOut( coorX, coorY, "Hello World!" ) ;
Wait(5000);
}
----
int coorX = ((15*2)+2)/2;
int coorY= coorX + 9;
string textB;
int ms= 5000;
task main() {
ClearScreen();
textB = "Hello World!";
TextOut(coorX, coorY, textB);
Wait(ms);
}
In the next tutorial we're going to be using motors, sensors, brick buttons and much more.
Thanks for reading. If you have any questions just comment below, I would be glad to help.
Please! If you copy this tutorial to your website give us credit.
No comments:
Post a Comment