Page 123 -
P. 123

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




                                                                      The Learning Brick Sorter • Masterpiece 2  95



                        Having described the algorithm in plain words, it’s time to write some code again.
                    First, we have to solve a small problem: our knowledge base is organized as a bi-dimen-
                    sional array, but NQC only supports arrays with a single dimension.The solution, how-
                    ever, is not overly complex. Imagine assigning each cell a progressive number,
                    row-by-row, column-by-column, this way (notice that the bins and ranges in Table 2.5 is
                    start from 0 instead of from 1.This trick would work with 1-based arrays as well, but it
                    would require a bit more math.

                    Table 2.5 Converting the Knowledge Base into a Single Array Capable of Being
                    Translated by NQC

                                     Bin 0                 Bin 1              Bin 2
                    Range 0          Cell 0                Cell 1             Cell 2
                    Range 1          Cell 3                Cell 4             Cell 5
                    Range 2          Cell 6                Cell 7             Cell 8

                        We can now store the cells in a mono-dimensional array with nine cells, numbered
                    from 0 to 8, and address them using a simple formula:

                    cell_address = range x 3 + bin
                        We already have a constant called RANGES that defines the number of rows of Table
                    2.5. Similarly, we can create a constant for the number of bins and make our program
                    completely parametric. Now the declaration of the knowledge base becomes:

                    #define BINS 3
                    int kb[RANGES*BINS];  // the knowledge base

                    And we can address any cell with the formula:
                    cell_address = range x BINS + bin

                        Now we have all the elements to start coding the routine Analyze(), which reads the
                    color of the brick, assigns it to a range, and decides which bin it must be placed into.This
                    routine incorporates our previous work about defining ranges:

                    void Analyze()
                    {
                       int light_value;
                       // wait for coupled touch sensor released
                       while (SENSOR_1>LITE_THRSHLD);
                       light_value = SENSOR_1;
                       // search if the light value is contained in
                       // one of the already defined ranges
                       for(range=0;range<=top_range;range++) {
                         if (light_value>=min[range] && light_value<=max[range]) break;
   118   119   120   121   122   123   124   125   126   127   128