#### Programming Language

Python, Java

#### Teacher Note

In this activity, your students will program the Finch using either Python or Java. To install the Finch library for one of these languages, please see either the Python or Java tutorial. This activity assumes that your students already know how to create, compile, and run a program in one of these languages. In addition, students must know how to define and use a function.

This activity uses recursion to draw Koch fractals. It assumes that students have been introduced to the idea of recursion, but completing this activity will help them to develop a deeper understanding of this concept via a physical representation of recursion.

#### Moving the Finch (Python)

To use the Finch in Python, you must import the relevant library and declare an object that represents the Finch. This is shown in the sample code below. In addition, you should import the time module. You will use the sleep() function to pause the program to give the Finch time to move.

The Finch object contains the methods that interact with the Finch. In this activity, you will use the wheels() method to make the Finch move and turn. This method requires two parameters. The first controls the power to the left wheel of the Finch, and the second controls the power to the right wheel. Both parameters must be between -1 and 1. For example, the method call below sets both wheels to move forward at full power.

To move the Finch forward, you must set the power to the wheels, pause the program, and then turn the wheels off. An example is shown below. The sleep() function takes a single parameter that is a number of seconds.

Try out the sample code above. It should move the Finch forward for three seconds. Then try several motor powers between 0 and 1 in the first call to finch.wheels(); keep the power for the left and right motors the same.

What does it mean for the power to be negative? Try several motor powers between 0 and -1; again, keep the power for the left and right motors the same.

**Tip:** You may need to carry the USB cord as the Finch moves. Otherwise, the cord may keep the Finch from moving and turning freely.

When the left wheel power is equal to the right wheel power, the robot moves in a straight line. When these are not equal, the Finch will turn. Try the sample code shown below. How are these two turns different? How can you make the robot turn in the other direction?

#### Moving the Finch (Java)

To use the Finch to create a Java class, you must import the Finch library and declare an object that represents the Finch. In the class constructor, you must create a new Finch object. This is shown in the sample code below.

The Finch object contains the methods that interact with the Finch. In this activity, you will use the setWheelVelocities() method to make the Finch move and turn. This method requires three parameters. The first controls the power to the left wheel of the Finch, the second controls the power to the right wheel, and the third sets the time in milliseconds that the Finch should move. Both power parameters must be between -255 and 255. For example, the method call below will move the Finch forward at full power for three seconds.

Try out the setWheelVelocities() method with several motor powers between 0 and 255; keep the power for the left and right motors the same.

What does it mean for the power to be negative? Try several motor powers between 0 and -255; again, keep the power for the left and right motors the same.

**Tip:** You may need to carry the USB cord as the Finch moves. Otherwise, the cord may keep the Finch from moving and turning freely.

When the left wheel power is equal to the right wheel power, the robot moves in a straight line. When these are not equal, the Finch will turn. Try the sample code shown below. The sleep() method pauses the program for a time given in milliseconds so that you can distinguish the two turns. How are these two turns different? How can you make the robot turn in the other direction?

#### Drawing Fractals

Fractals are geometric shapes that are defined recursively. For example, the base case (order 0) for a Koch fractal of size L is a straight line of length L, and a Koch fractal of order n and size L is defined as shown below. This algorithm is given in How to Think Like a Computer Scientist: Learning with Python 3 by Wentworth, Elkner, Downey, and Meyers.

- Draw Koch fractal of order n – 1 and size L/3.
- Turn left 60°.
- Draw Koch fractal of order n – 1 and size L/3.
- Turn right 120°.
- Draw Koch fractal of order n – 1 and size L/3.
- Turn left 60°.
- Draw Koch fractal of order n – 1 and size L/3.

For example, a Koch fractal with order 2 and size 27 is composed of 4 Koch fractals of order 1 and size 9. Each of those, in turn, is composed of 4 Koch fractals of order 0 and size 3. Thus, the Koch fractal of order 2 is made up of 16 line segments of size 3.

Use this algorithm to write a program that can use the Finch to draw a Koch fractal given the order (greater than or equal to 0) and size. For the Finch, the length of a line is roughly proportional to the time period for which the wheels move, so you can assume that the “size” of the fractal is a time period in milliseconds. To get a good picture of your fractal, make sure that only the left wheel moves when the Finch is turning; the right wheel should always have a power of 0 for turns.

**Hint: **The Finch moves and turns more reliably at low power.

When you complete your program, you will want to see the Finch draw fractals of different orders. It is possible to attach a marker to the Finch, but it is hard to get a good picture of your fractal this way. Instead, attach a CR2302 battery to a small LED and tape it to the Finch over the right wheel (the wheel that is stationary during turns).

Now you can use a long exposure photograph follow the “light trail” of the LED as the Finch moves. The pictures below were taken with the Slow Shutter Cam app, but other methods of taking long exposure photographs can also be used (LongExpo is a free app). You will need to make sure that the camera is very still during the exposure, so a tripod is a good idea if you have access to one. The light trail can also be a good way to measure the angle that the Finch is turning as you test and debug your program.