Jaws

Here is a fun operating mode, inspired by the famous movie of the same name. In this mode, the Timmyton will frantically move its jaws and tail, hunting down the infrared beacon while playing the infamous music! This is a beacon-seeking program, very similar to the one we made for the Security Tank.

Navigate over to the switch tab that has the right brick button highlighted. As usual, add the display reset and display print blocks at the beginning. Then, add a medium motor block (port A, On, 100 percent power). This motor block keeps the jaws and tail moving continuously while the operating mode is active:

Now, it is time for something new: drag out two loops and place one under the other. Name both loops Mode Loop. Split the programming's flow so that it branches off to both loops. Drag the program's flow cable to create a new branch by clicking on the small tab on the right edge of the last block in the original branch, and drag the cable over to the first block in the new branch:

Program the beacon follower inside the top loop. You can follow the same steps we used to make the beacon follower for the Security Tank (refer back to Chapter 2Security Tank – Object-Tracking Robot, for detailed instructions). Here, the beacon follower first checks for the presence of a nearby beacon using an IR sensor block and a logic switch.

If a beacon is present, the true case will activate and the robot will follow the beacon. The IR sensor will measure the beacon's heading and proximity and use them to adjust the robot's steering and power, respectively. The value for the heading/steering control is -2, and the value for the proximity/power control is 4.

As you read through the Mechanical design section of this chapter, you may have noticed that the IR sensor is placed upside down. This was done to make mounting the sensor more convenient. We compensate for this in the program by making the k value for the Timmyton's heading/steering adjustment negative.

If no beacon is detected, the false case activates, which simply halts the drive motors and turns the brick LED orange:

What about that second loop that we made? We will use it to run a second, smaller segment of code in parallel to the beacon tracking. This loop makes the Timmyton play a version of the ominous Jaws theme as it tracks the beacon. Place two sound blocks inside of the lower loop and set them both to Play Note. The first sound block will play a C4 (lowest note) for a duration of 0.25 seconds, and the second block will play a C#4 (half a step higher) for 0.3 seconds. This will repeat indefinitely while the Jaws mode is active, adding to the ominous mood while the Timmyton tracks its prey.

We also need to set the exit case for the lower loop to Brick Buttons | Compare. Set the middle brick button (button ID 2) as the target button, and select action 2, pressed and released. This is a redundant addition because of the loop exit branch we made when we set up the GUI earlier, but since Jaws mode has two branches running in parallel, the redundancy acts as a fail safe, adding an extra layer of security and ensuring the program runs smoothly without errors:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.196.182