Basic leJOS Programming


Topics:
How to write a program in leJOS and put it on the RCX

Classes Learned
Motors

Lesson:
Start by having the students open a text editor and copying down this program into their text editor.
NOTE: I heavily recommend the use of some sort of "smart" editor or at least one that colors the text. I use a program called TextPad which is very simmiler to Notepad except it colors text and if properly setup will compile and download RCX programs. Instructions on downloading and setting up TextPad can be found here However, programs like NotePad will also work.


import josx.platform.rcx.*;
public class Hello
{
   public static void main (String[] args)
   throws Exception
    {
       Motor.A.forward();
       Motor.C.forward();
       Thread.sleep(2000);
   }
}
Walk the students through compiling, linking and downloading the program onto their RCX. (The instructions for doing this can be found here.) When properly downloaded this program should make the RCX go forward for 2 seconds. (1000 milliseconds = 1 second)

Common Problems:
If you are using Notepad it automatically saves the file as a text file unless the "Save as Type" is set to "All Files". If lejos can't find your file try saving it again and make sure "Save as type" is set to "All Files"
If compiling and downloading in DOS and it can't find the file make sure that you are in the same directory as the file.

Now that all the students have got that program working explain the program to them line by line.

    import josx.platform.rcx.*;

This line ALWAYS needs to be included at the top of ALL programs writen for the RCX. Whenever we use a command in a program line LCD.clear() the RCX has to know what that means. This line tells the program where to find a "library" of all the terms. (If you are using TextPad the lejos command "links" the program it looks at this line to find the "library" and uses it to interpret the code.)

    public class Hello

This line tells the program that it's name is Hello and it is of type public which means any other program can run it. All programs we will be writing in these lessons will be type public.

    {

This is a begining bracket. Everything between the { } brackets is part of the program called Hello.

    public static void main (String[] args)

This line needs to be in every program. This denotes the "main" method. (we will learn about methods latter) This is what will be run when the program starts.

    throws Exception

This line deals with exceptions. For now just type it in exactly as shown here. We will explain exceptions latter.

    {

This is another begining bracket. This one begins the main method, the main method ends with the } bracket.

     Motor.A.forward();
    Motor.C.forward();

This tells the RCX to turn on Motors A and C both in the forward direction.
    Thread.sleep(2000);

This tells the program to wait or sleep for 2000 milliseconds.

      }
    }


These two lines end the main method and the Hello class.

Try having the students modify their programs to make the robot go backwards instead of forwards.