up previous next contents index
Next: Compile and run review Up: The RCX motors Previous: The RCX motors   Contents   Index

Example - moving the RCX motors

For the next example I've created a Java program named Motor1.java. This simple example shows how to run one of the motors in a "forward" direction, stop it, and then run it in a "backward" direction. The code does this once for the "A" motor, and then once for the "C" motor.


import josx.platform.rcx.*;

public class Motor1
{

  public static void main (String[] args)
  throws Exception
  {

    // run the "A" motor forward
    TextLCD.print (" A ");
    Motor.A.forward();
    Thread.sleep(2000);
    Motor.A.stop();
    // run the "A" motor backward
    Motor.A.backward();
    Thread.sleep(2000);
    Motor.A.stop();

    // run the "C" motor forward
    TextLCD.print (" C ");
    Motor.C.forward();
    Thread.sleep(2000);
    Motor.C.stop();
    // run the "C" motor forward
    Motor.C.backward();
    Thread.sleep(2000);
    Motor.C.stop();
  }
}

At this point I'm not going to try to explain the code much more than that. I hope you'll see that it is pretty self-explanatory, and I've tried to include a few comments in the code. For Java newbies, the comments are the lines that begin with the // characters.



Subsections
up previous next contents index
Next: Compile and run review Up: The RCX motors Previous: The RCX motors   Contents   Index