Problems with Sejway example from Maximum LEGO NXT

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, imaqine, 99jonathan

User avatar
New User
Posts: 7
Joined: Tue Oct 23, 2007 6:20 pm
Location: America's Wang

Problems with Sejway example from Maximum LEGO NXT

Postby DigitalOne » Mon Oct 29, 2007 2:26 am

I'm pretty green when it comes to programming, a bit of T-SQL and very little VB.NET. At this point I have no JAVA programming experience besides what I've read in the book.

So I put together the ChessBot and managed to get it to run through the program. So I moved to the Sejway.

I created the SejwayBad, downloaded it the brick and tried to run it.... the screen flashed? or more like drew a line on the LCD and then shut off. Here's the code I have.

Code: Select all

import lejos.nxt.*;

public class Sejwaybad {
   public static void main (String[] args) {
      int NEUTRAL = 37;
      int speed = 900;
      LightSensor s = new LightSensor (SensorPort.S3, true);
      Motor.B.setSpeed (speed);
      Motor.C.setSpeed (speed);
      while (!Button.ENTER.isPressed()) {
         int SENS_VAL = s.readValue();
         boolean forward = SENS_VAL > NEUTRAL;
         if (forward) {
         } else {

So I tried the Sejway. When I run this program the wheels just spin and the bot fall on its face and drives off. It also had to be shut off. Here is that code;

Code: Select all

import lejos.nxt.*;

public class Sejway {
   // PID constants
   static final int KP = 28;
   static final int KI = 4;
   static final int KD = 33;
   static final int SCALE = 18;
   // Global vars:
   int offset;
   int prev_error;
   float int_error;
   LightSensor ls;
   public Sejway() {
      ls = new LightSensor(SensorPort.S2, true);
   public void getBalancePos() {
      // Wait for user to balance and press orange button
      while (!Button.ENTER.isPressed()) {
         // NXTway must be balanced.
         offset = ls.readNormalizedValue();
         LCD.drawInt(offset, 2, 4);
      public void pidControl() {
         while (!Button.ESCAPE.isPressed()) {
            int normVal = ls.readNormalizedValue();
            // Proportional Error:
            int error = normVal - offset;
            // Adjust far and near light readings:
            if (error < 0) error = (int)(error * 1.8F);
            // Integral Error
            int_error = ((int_error + error) * 2)/3;
            // Derivative error:
            int deriv_error = error - prev_error;
            prev_error = error;
            int pid_val = (int)(KP * error + KI * int_error + KD * deriv_error) / SCALE;
            if (pid_val > 100)
               pid_val = 100;
            if (pid_val < -100)
               pid_val = -100;
            // Power derived from PID value:
            int power = Math.abs(pid_val);
            power = 55 + (power * 45) / 100; // NORMALIZE POWER
            if (pid_val > 0) {
            } else {
      public void shutdown() {
         // Shutdown LightSensor, motors
      public static void main(String[] args) {
         Sejway sej = new Sejway();

Is the code out date like some other areas of the book? Or is my sensor not working properly? And if not is there a way to calibrate it?

Thanks for any advice you have.
"I would kill everyone in this room for a drop of sweet beer." -Homer Simpson

User avatar
Site Admin
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby bbagnall » Sun Nov 04, 2007 7:11 pm

Hi there,

Try downloading the code right from the book's website (bottom of page):

Make sure you have the robot built exactly as described, including plugging the motors and sensors in the right ports. Also, read the part on page 284 called "Using Sejway". It explains that you have to position it in the balanced position, then hit the orange button and it will start trying to balance.

Return to “NXJ Software”

Who is online

Users browsing this forum: Google [Bot] and 2 guests