|
Lesson 2.0: Your first programs
Introduction: In the last lesson you learned how to open programs and run them. Now you
will learn how to write them. This manual will assume that you have never
programmed before and will start at the very basics of programming. We will
then gradually step of the complexity, and when done you will have mastered
many programming skill. Review the sample programs included in the projects folder to better understand
the fuzzy logic that makes your robot tick.
Hello World: 1. Turn OFF the RoadRunner, and remove power.
4. Now it is time to begin writing your program. First of all you need to
tell the compiler what libraries it needs. The compiler is the part of the
JAVELIN STAMP IDE program that will read your program and translate it into
commands that the computer can understand. Libraries are sets of programs
that have already been written that you can use in your programs. Basically
speaking the computer does not inherently know how to do anything. Thus in
order to carry out even the simplest of tasks such as displaying a character
to the monitor, a program must be written in order to tell the computer how
to carry out the task. However, if you had to re-write every step, in every
program that you create it would take an enormous amount of time to write
even small programs like the hello world program. Thus, languages such as
JAVA let you reuse or call other programs in your program. However, before
you can do this you have to tell the computer which program libraries you
are going to use. Therefore start off by writing the following line of code
(each command you give the computer will be on a different line, and is called
a line of code) import stamp.core.*;
This line of code lets your program use many of the more common STAMP abilities.
Take care to note that there is a semicolon at the end of the line. Every
line of code must end in a semicolon, this is how the computer knows that
you are done with your line of code.
public class hello_world
Note the word public. This means that any program may be able to access and
use your class. Also note that there is no semicolon on this line. In addition
note the open bracket. This signifies the start of the class hello_world This
is how the computer knows that the class has begun. Later on you will have
a close bracket to let the computer know that the class is done.
public static void main()
At this point you now need to give the main function something to do. Well
in this case we want it to display the message Hello World. How do you do
that? Remember the import line that you put on the top of your program. It
just so happens that there is a class that is included inside of the stamp.core
library that does exactly what you need. This class is called System, and
within System there is a class called out, and within out there is a function
called print. So now we can use the function print to display a message to
screen whit the following line.
"Hello World"Thus the line of code to display the message will look as follows
System.out.print("Hello World");
Don't forget the semicolon. Now you are done with main, so you must let the
computer know that you are done. To do this, add a close bracket. } Now you could add another function to the hello_world class, but for now you are done. Thus end the class with another close bracket. } Take care to keep track of the open and close brackets as it can get confusing as you add more and more functions to your class. Your entire program should look as follows.
import stamp.core.*;
public class hello_world
{
public static void main()
{
System.out.print( "Hello World" );
}
}
5. Turn on the RoadRunner To do so, place the switch into the NC position, after applying power to the robot. The light should go on. 6. Now run the program. To do so, select project and then program on the task
bar. 7. The output of your program should look as follows. If not go over your
code (program) and look for a mistake. 8. There is however, a problem with this program. While it will run, it is formatted poorly. In order to make your code (program) more readable by others you need to add comments to your program. First lets add a description of the program at the top of the program. To do this write your description between the following symbols /* and */ The computer will ignore anything between these symbols.
9. Now add comments to the lines inside of your program. To do this add the symbol // to a line of code, and then write your comment. The computer will ignore anything after the // until the next line of code.
10. To make the code even more readable, use tabs to indent your code
This completes your introduction to the RoadRunner Review the sample programs included in the projects folder (some of these programs require different sensors not included with your RoadRunner). Have fun and let us know of your developments with the RoadRunner! | |||||||