Tutorial Details:
Difficulty Level:
Basic
Topics Covered: Using
Includes to save time and simplify code.
Assumed Knowledge:
THe Basics, my
first program
Written By: BILL LANE
Print Version
When we start writing programs we find that there are certain
things that we need to write everytime we create a new program.
Which could get very tedious. Especially if they are things
that someone else has already created for us and that we don't
really understand. Fortunately there is a solution! If you
have a look in your Spybotics folder you'll find a folder
called include. This folder is full of files with a .h extension.
Here's the first 5 lines from the spybot.h file:
//Spybot.h
//Spybot firmware header
//output ports
output right on 1
output left on 2
What you'll notice is that it's declaring the motors for
use. If you have a look at that file you'll notice it goes
on for about another 235 lines declaring all sorts of useful
stuff. By using #include
in the declaration section of our program we can get it to
include all of those declarations into our program. With just
one line we get 240 lines of declarations. Your not limit
to one include. You can include as many header (that's what
they're called ) files as you need. You can make your own
header files to include code you use often. So lets have a
look at our first program with the include instead of the
declaration:
program myFirst
{
#include<spybot.h>
main
{
on [left]
}
}
Too simple really! The biggest issue with using #include
is the include path. In other words how do we get from where
our program file lives to where the file we want to include
lives. The simplest way to deal with this is to save them
both in the same folder. If you used the default install for
the Mindstorms SDK then your Spybot .h (header) files will
be in:
C:\Program Files\LEGO Software\LEGO Mindstorms SDK\Bin\VPBrick2\Spybot
If your using ScriptEd
then you can set the include path by going to Specials
> Set Include Path.
That all there is to using #include
I think you'll find that it saves you lots of time
and trouble.
|