## Lesson 7 – Finch Accelerometer

Elif statements

#### Teaching Materials

Inside the Finch, there is an accelerometer. This sensor can be used to measure the orientation of the Finch. For example, you can detect when the Finch is upside down. The Finch accelerometer is very similar to the accelerometer that enables a smartphone or tablet to change the screen from portrait to landscape when the user turns the device.

The accelerometer measures any force that causes acceleration in the x-, y-, or z-direction. Because the Finch is usually moving pretty slowly, the accelerometer mostly measures the acceleration due to gravity. When the Finch is sitting on a level surface, the acceleration due to gravity points through the base of the Finch, along the positive z-axis. If you instead position the Finch so that its beak is pointing at the ground, the acceleration due to gravity points along the positive x -axis. The accelerometer enables you to measure these differences.

The acceleration() method returns a tuple objects with five values. The first three are float values that measure the acceleration in the x-, y-, and z-directions; these values are between -1.5 and 1.5 (units are g’s of acceleration). The last two items in the tuple are Boolean values that indicate whether the robot is being tapped or shaken. The code below stores all five values in variables with informative names. The two Boolean values will not be used in this lesson because sometimes unintentional movements result in unexpected True values.

#### Exercise 1:

Write a program that repeatedly prints the x-, y-, and z-values for acceleration to the screen. The program should stop when the Finch detects that it is dark. Test your program by tilting the Finch in different directions. When is the z-acceleration close to -1? How can you detect that the Finch’s left wing is pointing at the ground?

#### Exercise 2:

Write a program that makes the Finch buzz until you stand it on its tail.

#### Exercise 3:

Write a program that uses the x-, y-, and z-values for acceleration to set the amount of red, green, and blue in the Finch’s beak. The program should run until you turn the Finch upside down.

#### Exercise 4:

Turn your Finch into an timer. Allow the user to input a number of seconds. When that time is up, the Finch should do whatever is necessary to get the user’s attention. The Finch should continue to act until the user shakes the Finch with an acceleration that has a magnitude greater than 2g. Remember that the acceleration is each direction is between -1.5g and 1.5g. You will need to combine the acceleration measurements in each direction to find the total magnitude of the acceleration: a = sqrt(ax2 + ay2 + az2).

#### Exercise 5:

Write a program that tells the user the orientation of the Finch. For example, if you run the program when the Finch is standing on its tail, it should print “beak up” to the screen. The project should be able to identify these orientations: level, upside down, beak up, beak down, left wing down, and right wing down. If the Finch is not in any of those orientations, the program should print “other” to the screen.

#### Exercise 6:

For this exercise, you will need a flashlight (the one on your phone may not be bright enough). You are going to make the Finch follow the light! Use the value of the left light sensor to set the speed of the right wheel and the value of the right light sensor to set the speed of the left wheel. Your program should do this repeatedly until you set the Finch on its tail. Try it out with your flashlight! Can you make the Finch avoid the light instead?

#### Exercise 7:

In this exercise, you will make the Finch climb a hill. You will need a board or table that you can tilt to make your “hill.” It should be tilted about 15°.

• Start by making your robot turn until the y-acceleration is close to 0. When this part of your program is finished, the Finch should pointed either uphill or downhill.
• Now your Finch is ready to drive, but you have to figure out whether it is pointed uphill or downhill. Which acceleration measurement can you use to determine that?
• If your Finch is pointed uphill, drive forward up the hill. If your Finch is pointed downhill, drive backwards up the hill instead!