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

68


          readable  names for things that  might otherwise  be cryptic. It also lets  you  define things that might need to  be adjusted
          th roughout your program in one place. Your program, for example, might have multiple places where it set the outputs to
          power level 5. Instead of explicitly putting 5  all the way through your program, you can use the constant value POWER. If you
          la ter decide you want the power level to be 7, you just have to change the definition of POWER, instead of finding all the places
                        he  t  output power i
          in your program w re he          s set.

          You can also  create  macros  with  #define. A   macro  is a kind  of  miniature program. Usually you'll define a macro for
          so mething you want to do frequently. The following program uses three macros:

              #define forward(power) \
                  SetPower(OU  T_A + OUT_C,    power); \
                  OnFwd(OUT_A + OUT_C);
              #define left(power) \
                  SetPower(OUT_A + OUT_C, power); \
                  OnRev(OUT_A); OnFwd(OUT_C);
              #define right(power) \
                  SetPower(OUT_A + OUT_C,      power); \
                  OnFwd(OUT_A); OnRev(OUT_C);

              task main() {
                  forward(OUT_FULL);
                  Wait(100);
                  left(OUT_HALF);
                  Wait(100);
                  right(OUT_HALF);
                  Wait(100);
                  Off(OUT_A + OUT_C);
              }

          The preceding example shows off two features of macros. First, each macro has a parameter, power, that is used in the bod y
          of  the macro. Second, the macros are split out to multiple lines by using a backslash.

          Sounds and Music

          Your RCX can play various prepacka ged sounds, using the following command:

          PlaySou nd(const n)
          This command plays the so und represented by n. NQC includes constant names for each available sounds, as detailed in Table
          4-5.

          Table 4-5. RCX System Sounds

          Sound Name             Description

          SOUND_CLICK       Short beep

          SOUND_DOUBLE_BEEP  Two medium beeps

          (table continued on next page)
   74   75   76   77   78   79   80   81   82   83   84