Page 120 -
P. 120
243_MasterPieces_02b.qxd 4/18/03 6:59 PM Page 92
92 Masterpiece 2 • The Learning Brick Sorter
PlaySound(SOUND_DOWN);
StopAllTasks();
}
range=top_range;
min[range]=light_value-RANGE_WIDTH;
max[range]=light_value+RANGE_WIDTH;
}
There are a few things to notice about this portion of code: I used two new variables,
light_value to store the reading of the light sensor, and range to point to the current range
in the arrays. More important, I wrote a piece of code to handle the case where if the
user attempts to use more than three different colors (the amount allowed by our pro-
gram) the program plays the predefined SOUND_DOWN sound pattern and stops. If
you use your Learning Brick Sorter properly, this portion of code will never execute, but
foreseeing any possible problems or “unexpected” behavior of the user is a very good
programming practice.
Finally, you might have noticed that I assigned the min[range] and max[range] variables
with the value from the sensor plus and minus a constant called RANGE_WIDTH.This
provides the tolerance for which we designed the ranges. Suppose RANGE_WIDTH is
set to 1, when the Brick Sorter encounters a brick of a new color reading—for
example—45, it will define a new range limited by the values 44 and 46. What happens if
we set RANGE_WIDTH to higher values? We widen the bounds of the range, thus
making the Brick Sorter more tolerant toward differences in the readings. For example,
with RANGE_WIDTH set to 3, the brick of our previous case—whose reading was
45—will define a range limited by 42 and 48 instead of 44 and 46.The reason for want
to increase the value of RANGE_WIDTH is to make the Learning Brick Sorter less sen-
sitive to small fluctuations in the readings that might affect the behavior of the robot.The
most typical cause for these fluctuations is the distance to which the brick is placed from
the light sensor. In fact, the same brick placed at different distances results in different
readings. On the other hand, if the values of RANGE_WIDTH are too high we limit the
possibility of the Learning Brick Sorter being able to distinguish different colors up to
the point that if ranges overlap, the Learning Brick Sorter can’t work properly anymore.
Low values of RANGE_WIDTH increase sensitivity, thus expanding the possibility for
the Learning Brick Sorter to distinguish colors, at the price of making the placement of
the brick more critical. I tested my Brick Sorter with the RANGE_WIDTH set to 2, but
feel free to experiment with different values.
Now that we are able to define and distinguish colors, it’s time to design the knowl-
edge base.The knowledge base is the data structure that will contain the association
between the colors and the bins. Let’s think of the terms of the problem. Given a specific
color and a specific bin, the Learning Brick Sorter may know it is the right combination,
or that it is a wrong combination, or may know nothing about the two (when the
program starts).This third case is the status of the knowledge base for any possible