Page 171 -
P. 171

243_masterpieces_03.qxd  4/18/03  7:02 PM  Page 143




                                                                     The LEGO Turning Machine • Masterpiece 3  143



                    show the value of the state variable.This is very interesting since, when running the
                    machine, we can observe and know in what state the machine is.This will also be very
                    useful when debugging the program.
                    void setup()
                    {
                       SetSensorType(SENSOR_2, SENSOR_TYPE_TOUCH);
                       SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);
                       SetSensorType(SENSOR_3, SENSOR_TYPE_LIGHT);
                       SetSensorMode(SENSOR_3, SENSOR_MODE_RAW);
                       SetPower(OUT_A+OUT_B+OUT_C, OUT_FULL);
                       SetUserDisplay(state,0);
                    }

                        Now we need to write a procedure to control the movement of the Tape sub-
                    assembly one cell at a time.The motor of the Direction Control sub-assembly should be
                    activated in either direction; then it has to run until the gray peg disengages the touch
                    sensor and another gray peg engages it again.To do this, we’ve created two constants at
                    the very beginning of the program:
                    #define TOUCH_LOW_THRESHOLD 500
                    #define TOUCH_HIGH_THRESHOLD 1000

                        The motor should stop only after reading a value higher than the TOUCH_HIGH_
                    THRESHOLD and a value lower than the TOUCH_LOW_THRESHOLD. I chose to
                    define these two values as constants, so they are easily modifiable and simple to determine
                    the values that are acceptable for your sensor. (Remember that motors and sensors can be
                    very different from one another, so you need some way to adjust the reading).
                        For this particular program, I chose to have the touch sensor use RAW values, instead
                    of the usual Boolean mode.There are mainly two reasons for this choice. First, every
                    sensor is different from the others, and the touch sensor makes no exception. In addition,
                    a touch sensor doesn’t have only two positions, on and off, but rather a series of interme-
                    diate values.To handle a movement that has to be very precise, like moving exactly one
                    cell of the tape, you need to be able to control the reading in a very precise way.
                        Note that the values 500 and 1000 are just examples for two values that should be on
                    the low part and high part of all the possible readings of the touch sensor (that usually
                    range from around 250 to 1020).They should be just fine, but you may have to calibrate
                    them to your system with the help of the View button on the RCX.
                        Here are the procedures to move one cell to the right or to the left:

                    void move_right()
                    {
                       // run motor until the touch sensor engages a new peg
                       OnFwd(OUT_A);
   166   167   168   169   170   171   172   173   174   175   176