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

152


                  start lightWatcher;
                  start touchWatcher;
                  start heartbeat;
              }
              task touchWatc   her() {
                  while (true) {
                      if (SENSOR_2 == 1) {
                           SendM essage (FORWARD_MESSAGE);
                           Wait(10);
                           until (SENSOR_2 == 0);
                           SendMessage (STO   P_MESSAGE);
                      }
                      if (SENSOR_3 == 1) {
                           SendMessage (SPIN_MESSAGE);
                           Wait(10);
                           until (SENSOR_3 == 0);
                           SendMessage (STOP_MES    SAGE);
                      }
                  }
              }

              #define TOLERANC   E 3
              int curre  nt;
              int minimum;
              int maximum;
              int midline;

              int lastArmMessage;

              task light  Watcher() {
                  minimum = 100;

                  maximum = 0;
                  while (true) {
                      current = SENSOR_1;
                      if (current < minimum) minimum = current;
                      if (current > maximum) maximum = current        ;
                      midline = minimum + (maximum - minimum) / 2;

                      if (SENSOR_1 <= (midline - TOLERANCE) &&
                             lastArmMessage != RELEASE_MESSAGE) {
                           PlayTone(494, 10);
                           PlayTone(660, 10);
                           SendMessage (RELEASE_MESSAGE);
                           lastArmMessage = RELEASE_MESSAGE;
                      }
                      if (SENSOR_1 >= (middle + TOLERANCE) &&
                            lastArmMessage != GRAB_MESSAGE) {
                           PlayTone(660, 10);
                           PlayTone(494, 10);
                           SendMessage(GRAB_MESSAGE);
                           lastArmMessage = GRAB_MESSAGE;
                      }
   158   159   160   161   162   163   164   165   166   167   168