Page 71 - The Unofficial Guide to Lego Mindstorms Robots
P. 71

60


          These commands set the mode and direction of the outputs in one fell swoop, but you should still set the power level explicitly
          with a call to SetPower().

          For timed actions, the following command will come in handy:

          OnFor(const outputs, expression  time)
          T his command turns on the specified outputs for the given time, measured in hundredths of a second. Then the given outputs
          are turned off (in brake mode, not in float mode).

                                           d
                                                                       n re rses outputs A and C. After another second,
          The following example runs outputs  A an C forward, waits one second, the   ve
          th e outputs are turned off.

              task main() {
                  SetPower(OUT_A + OUT_C, OUT_HALF);

                  Fwd(OUT_A + OUT_C);
                  OnFor(OUT_A   + OUT_C, 100);
                  Rev(OUT_A + OUT_C);

                  OnFor(OUT_A + OUT_C, 100);
              }

          T he On(), Off(), Fl oat(), Fwd(),  Rev(), and Toggle() comman ds a re real ly shorthand for these lower-level output
          commands:

          SetOutput(c onst outputs, const mode)
          This command sets the mode  for the given outputs. The outputs are specified in the same way as in the Fwd() and Rev()
          commands. The value of mode should be one of the constants OUT_ON, OUT_OFF, and OUT_FLOAT.

          S etDirection(const outputs, const direction)
          T his command determines the direction  of the supplied outputs.  The  direction  parameter should be  OUT_FWD,
          OUT_REV , or OUT_TOGGLE. OUT_TOGGLE is a special value that sets the direction of the output to the opposite of its
          current value.

          In general, I recomm end you don't call SetDirection() with the OUT_TOGGLE value. If you explicitly set the directions
          o f your outputs, your program will be clearer. Furthermore, in programs with more than one task, your program is more likely
          to  behave as you expect.

          Input Commands

          Before you can read a value from one of the RCX's inputs, you need to tell the RCX what type of sensor is attached to the
          input. NQC  provides a command that does just this:
   66   67   68   69   70   71   72   73   74   75   76