Page 173 -
P. 173
243_masterpieces_03.qxd 4/18/03 7:02 PM Page 145
The LEGO Turning Machine • Masterpiece 3 145
The main part of the program includes the task to coordinate the other functions and
implement the state transition table that is the key to the operation of the Turing
Machine. In NQC, there’s no better way to turn the table into code than by using the
switch...case and the if statements.The switch structure checks the state of the machine and
the if statement checks if a symbol is read on the tape.A convenient way to handle sensor
reading is to define a new constant: #define LIGHT_THRESHOLD 750
If the light sensor reading is higher than the LIGHT_THRESHOLD then the liftarm
is in its upper position and the cell contains a symbol, otherwise the cell is empty.You
will probably have to calibrate the threshold value for your configuration and your light
conditions. Use the View button to discover the highest reading (with the liftarm below
the sensor) and lowest reading (with liftarm in the other position) and find the mean
between the two: this is your threshold. My sensor reads from 720 to about 780, so I cal-
culated: (720+780)/2 =750.
task main()
{
setup();
do
{
switch(state)
{
case 0:
if (SENSOR_3 < LIGHT_THRESHOLD)
{
write();
state = 1;
}
move_right();
break;
case 1:
if (SENSOR_3 < LIGHT_THRESHOLD)
{
move_left();
state = 2;
}
else
move_right();
break;
case 2:
erase();
done = 1;

