Dragon Drag Racing

Walldyn loves flying around, but its a little clumsy and bumps into things sometimes. We’re gonna help by building it with the distance sensor and programming it to fly around without bumping into anything! Here’s how to build Walldyn the Dragon:

Programming Walldyn

Let’s start with this program. Walldyn won’t drive around the Lego box yet, but you’ll see how it works first. Here’s what happens. The motor turns on at power = 4. Then the distance sensor sends a message with its reading which is a number from zero to ten – like a letter in the mail.
The other two chains of blocks are waiting to get a message. If it’s zero, the top one gets the message and turns the motor in one direction and lights a purple light. (You’ll have to put something that touches right up to the sensor for it to get a zero.) If it’s ten, the bottom chain gets the message. It turns the motor the other way and lights an orange light. The loop on the main chain makes it keep repeating this, over and over.
Try it. Walldyn should spin one direction or the other.
Now you see how the messages work.
So what if the sensor reads 1, 2, or 3 ? Something besides 0 or 10?
This program gets fancy, but follow it through with your finger.
The sensor sends its message. All the chains in the middle will get that message, whatever number it is.
Then they send another message: the letter r or the letter l . We know these mean left and right, but it really doesn’t matter which letter you use.
The two chains on the right get these messages, r or l . They turn the motor’s direction.
So really all Walldyn has to do is turn one way or the other way. It turns away from the box if it is close to the box and the sensor sends a small number. And then Walldyn turns the other way if it is far from the box and the sensor sends a larger number.
Change the program and see what happens!