bgw -> Robotics -> Downloads -> LeJOS -> Sample Program

CSAS 4082: Intro to Robotics

A first Java program for the NXT

Make sure you installed the Java compiler, LeJOS, and Eclipse on your laptop and uploaded the Java VM to your NXT brick, as explained before.

Start Eclipse. Right-click on the NXT project you setup previously and select "New | Class". Name the class Hello and accept the rest of the default options.

The Eclipse Editor will open, with parts of the code already in place, including a few comments. You could delete these comments if you like. Your code should look as follows (minus the hand-written comments, of course).

Every Java program we'll write will look at a minimum like this template (with different program names, of course). You add code to the "main" method, or function, or you define new methods (functions) and "state" variables.

In our example, we'll add some code to play a sound, display some text on the NXT's display, and wait for 2 seconds. Note that I re-arranged the curly brackets so you can better see the beginning and end of code segments. Also note that I've added an "import" statement as first line, and modified the "main" method's first line just a little by adding "throws Exception". Add the following lines to your code:

The code you type is automatically spell-checked as you type, similar to MS Word. Also, if you type a statement such as Sound and hit period, you will get a list of available functions to choose from. You can either select a function from the list or type it manually. Note, finally, that your code is also color-coded as you type as follows:

  • special Java keywords are red
  • single-line comments begin with // and are green
  • multi-line comments are enclosed in /* ... */ and are light-blue
  • Strings (text) is colored blue
  • user-defined statements are black

Also note that statements are either followed by curly brackets (where each open bracket has to eventually have matching closing bracket) or a semi-colon. Also, multiple types of brackets are used: ( ... ), { ... }, and [ ... ]. They are sometimes hard to distinguish, but you must type them correctly. And finally, you must use the exact right capitalization: will work, but will not.

Now we are ready to compile the program (i.e. translate it into machine language) and upload it to your brick. To compile, click the green "play" arrow with a tool box drop-down and select "LeJOS Compile" (which you previously defined when you setup Eclipse).

If all goes well, no error message will appear. If so, plug in your NXT via the USB cable, then drop-down the green "Play + toolbox" button again, but this time pick "LeJOS Download". Your program should download to your NXT and you can execute it from there by making the appropriate selection.

Note: LeJOS will automatically attempt to use Bluetooth is the USB cable is unplugged:

  • unplug the USB cable

  • plug in your Bluetooth dongle and wait a few seconds

  • select "LeJOS Download" just as before

LeJOS should search for your NXT and, if it finds it, download your program wirelessly. The first time will take a while (up to 2 minutes) and you might get a choice of NXT's (if several are around) but subsequent downloads should go faster.