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

186


              while(true) {
                  if (BUMP_SENSOR == 1) {
                      // Say tag!
                      SendMessage(MESSAGE_TAG);
                      // Coast   to a stop.
                      tagCommand = CO   MMAND_FLOAT;
                      Wait(20);
                      // Check to see if we     got an acknowledgement.
                      if (Message() == MESSAGE_ACKNOWLEDGE) {
                           PlaySound(3);
                           SetPower(OUT_B, sco   re);
                           On(OUT_B);
                           if (scor e < 7) score = score + 1;
                      }
                      else PlaySound(2);
                      ClearMessage();
                      // B ack up.
                      tagCommand = CO   MMAND_REVERSE;
                      Wait(50);
                      // Turn left or right for a ran      dom duration.
                      if (Random(  1) == 0) tagCommand     = COMMAND_LEFT;
                      else tagC  ommand = COMMAND_RIGHT;
                      Wait(Random(200));
                      tagCommand = COMMAND_NONE;
                      }
                      else tagCommand = COMMAND_NONE;
                  }
              }

              int avoidCommand;
              task avoid() {
                  while(tru  e) {
                      if (LIGHT_SENSOR < averageLight - 3) {
                           // Back aw  ay from the border.
                           avoidCommand = COMMAND_    FLOAT;
                           Wait(20);
                           avoidCommand =   COMMAND_REVERSE;
                           Wait(50);
                           // Turn left or right for a random duration.
                           if (Random(1) == 0) avoidCommand = COMMAND_LEFT;
                           else avoidCommand = COMMAND_RIGHT;
                           Wait(Random(200));
                           avoidCommand = COMMAND_NONE;
                      }
                  }
              }

              int taggedCommand;

              task tagged() {
                  while(true) {
                      if (Message() == MESSAGE_TAG) {
                           taggedCommand = COMMAND_STOP;
   192   193   194   195   196   197   198   199   200   201   202