Page 170 -
P. 170

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




           142    Masterpiece 3 • The LEGO Turning Machine
























                  Programming the Machine


                  In this section, we will see how we can actually give “intelligence” to our Turing
                  Machine. We will take the first example shown in this chapter.That is, making a machine
                  that adds two integers and translates them into NQC code.



                  NOTE

                       The program for the operation of the LEGO Turing Machine can be found on the
                       CD-ROM that accompanies this book.




                     We will follow good programming practice and split the programming into separate
                  procedures. We will examine them one by one.There are multiple procedures, identified
                  by the keyword void, meaning that they don’t return any value, and a main program called
                  task main().
                     First, we should define two variables:

                       ■  One to store the current state the machine is in, indicated by a number

                       ■  The other used by the program to tell when it has finished its job and should
                          stop (I choose to name it “done” because “stop” is a reserved keyword in NQC).

                     In NQC there are no Boolean variables to handle a True/False value, so the latter also
                  should be declared as int. Both are initialized to zero.
                  int state = 0;
                  int done = 0;

                     Some setup must be done to tell the RCX that we will be using two sensors: a touch
                  sensor for the step-by-step movements, and a light sensor to read the symbol inside the
                  cell. Here we set motor’s power to the maximum, and finally take advantage of a new fea-
                  ture of the RIS 2.0 firmware (version 0328) and set the RCX’s LCD display to always
   165   166   167   168   169   170   171   172   173   174   175