Page 128 -
P. 128

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




           100    Masterpiece 2 • The Learning Brick Sorter



                      Finally, we are ready to write the main task, which reflects the sequence of steps we
                  defined at the beginning of this section.Together with all the constants and global vari-
                  ables we have used up to this point, summarized in a single place, here is that code:
                  #define BINS 3
                  #define RANGES 3
                  #define SWITCH_TIME 20
                  #define LITE_THRSHLD 98
                  #define OP_DELAY 75
                  #define RANGE_WIDTH 2
                  #define KB_YES 1
                  #define KB_NO -1
                  #define KB_UNKNOWN 0
                  int range;            // index of the selected range (0 based)
                  int top_range;        // highest used range index
                  int bin;              // index of the selected bin (0 based)
                  int pos;              // current arm position: -1=pick up station, 0=bin0,
                  1=bin1...
                  int min[RANGES];      // lower limits of the ranges
                  int max[RANGES];      // upper limits of the ranges
                  int kb[RANGES*BINS];  // the knowledge base
                  task main()
                  {
                    SystemSetup();
                    while (true)
                    {
                      // wait for button pressed
                      while (SENSOR_1 < LITE_THRSHLD && SENSOR_2 == 0);
                      PlaySound(SOUND_CLICK);

                      // analyze knowledge base (set the range & bin variable)
                      Analyze();


                      // take the brick
                      LowerArm();
                      Wait(OP_DELAY);
                      CloseHand();
                      Wait(OP_DELAY);
                      LiftArm();

                      // go to bin and drop it
   123   124   125   126   127   128   129   130   131   132   133