For all the code presented here, the include file
  #include "direct-sensor.h"
will need to called out in the source.

Passive Sensors

There are 3 builtin macros to read the inputs from passive sensors connected to the RCX brick and one to read the battery state:
  SENSOR_1 SENSOR_2 SENSOR_3 BATTERY
The value returned is a 16 bit unsigned value (most of the time, some sensors cannot drive the voltage to the rails and would reduce this range). Since all of the input ports are run through an A/D converter, push buttons will need to have their inputs converted to a logical value.
    ...
    /* is the button on port 1 pressed? */
    if (SENSOR_1<32768) {
      ...
    }
    ...

Active sensors

Active sensors have to be initialized before use so that the RCX brick will feed them power. The light sensor is one of these. Without power from the brick it will works as a passive sensors and will read whatever light is present in the environment. When the sensor is switched to active mode the small LED will light up to provide the light sensor with its own light. In either mode there are macros to read the sensor and scale it's output to a 0 to 100 range.
    ... 
    /* light sensor on port 2 - set as active sensor, turns LED on */
    ds_active(&SENSOR_2); 
    ... 
    if (LIGHT_2>75) {
      /* looking at light colored or reflective object */
      ...
    }
    ...
    /* now set light sensor as passive sensor, turns LED off, use ambient */
    ds_passive(&SENSOR_2); 
    ...
The rotation sensor needs more support code since it needs to have some processing done by legOS to track the sensors position (the sensor sends out 4 different values for each 22.5 degree of rotation and then repeats, a small state machine tracks the values to get the full 16 steps per rotation).
    ...
    /* rotation sensor on port 3, active, flag as rotation sensor */
    ds_active(&SENSOR_3);
    ds_rotation_on(&SENSOR_3);
    /* set our starting rotation value */
    ds_rotation_set(&SENSOR_3,0);

    ...
    calc_deadreckoning_position(ROTATION_3);
    ...

Previous - Programming Model Main Next - Motors
Monty Stein Dec 18, 1999
CLUG Home