Page 124 -
P. 124

243_MasterPieces_02b.qxd  4/18/03  6:59 PM  Page 96




           96     Masterpiece 2 • The Learning Brick Sorter



                    }


                    // if not, define a new range
                    if (range>top_range) {
                      top_range++;
                      // if all ranges have been used, beep for error
                      // and terminate program
                      if (top_range>=RANGES) {
                         PlaySound(SOUND_DOWN);
                         StopAllTasks();
                      }
                      range=top_range;
                      min[range]=light_value-RANGE_WIDTH;
                      max[range]=light_value+RANGE_WIDTH;
                    }
                    // look for a positive value in the knowledge base array
                    for (bin=0;bin<BINS;bin++)
                      if (kb[range*BINS+bin]==KB_YES) return;
                    // otherwise, look for an "unknown" value in the knowledge base array
                    for (bin=0;bin<BINS;bin++)
                      if (kb[range*BINS+bin]==KB_UNKNOWN) return;
                    // this point shouldn't ever be reached, unless the user provided some
                    // inconsistent replies. If this happens, the program makes a random
                    // choice.
                    bin=Random(BINS-1);
                  }
                      You probably remember that one of the touch sensors of the switchbox shares the
                  same Input Port as the light sensor. Because of this, we must be sure that the lever has
                  been released before taking a reading from the light sensor.The purpose of the constant
                  LITE_THRSHLD is to be able to distinguish when the touch sensor is closed.
                  #define LITE_THRSHLD 98

                      Under normal conditions, the light sensor never returns a value even close to 100. On
                  the contrary, the touch sensor returns 100.This simple test against LITE_THRSHLD
                  allows us to identify whether the touch sensor is pressed or not.
                      The routine continues with the code searching and assigning the ranges that define
                  colors, and finally starts looking up the knowledge base. First, it scans the row correspon-
                  ding to the given range for a positive value; that is. a cell assigned to the KB_YES value.
                  If it finds one, the range has already an assigned bin and the routine can return the global
                  variable bin, which contains the number of the bin to put the brick into.
   119   120   121   122   123   124   125   126   127   128   129