The leJOS Tutorial

| Start of Tutorial > Start of Trail

Trail: Essential leJOS classes
Lesson: Controlling the Hardware

# Controlling the Hardware: Motors

The static `Motor` class is used to control actuators, i.e., devices connected to one of the three black output ports mounted beneath the RCX's display:
Most of its methods are intented to be used with actual LEGO® motors, but may also be used in connection with, say, a light bulb.
Not very surprisingly, the three ports are implemented as static fields of the `Motor` class:

- Motor.A
- Motor.B
- Motor.C

Hence a typical call would be

                Motor.A.forward();

Controlling one of these motors is rather simple:

- public static void forward()
- public static void backward()
- public static void reverseDirection()
- public static void flt()
- public static void stop()

for forcing the motor turning clock- or counterclockwise, floating (which means that the motor is idling) or stopping it,

- public static void setPower(int aPower)

for setting the motor power from 0 to 7 (which has only a noticeable effect on the motor when it's heavy loaded!)
and the associated information methods

- public static boolean isForward()
- public static boolean isBackward()
- public static boolean isFloating()
- public static boolean isMoving()
- public static boolean isStopped()

and `public static char getID()`, which delivers 'A', 'B' or 'C'.

Note that the actual direction the motor is turning to depends on the way the connectors are mounted on the ports and the motors, resp. - so if your robot is running backwards when it shouldn't it may suffice to turn one of the connectors for 90°.

## A complete example which uses the Motor class

is Rotator.java which may be found in the `examples/rotator` section of the leJOS tree.

## The Motor API

may be found here.

The leJOS Tutorial

| Start of Tutorial > Start of Trail