## Lesson 11 – Lists with the Finch

Lists

#### Teaching Materials

In previous lessons, you have used the Finch sensors to enable the Finch to respond to its environment by making decisions based on light, temperature, acceleration, and the presence of obstacles. You can also use the Finch as a measurement tool. For example, the code below measures the value of the Finch’s left light sensor every second. It stores this data in a list called leftLightSensor.

#### Exercise 1:

Modify the code above so that the Finch moves along a path and records light sensor data at 15 locations. At each point, it should record the values of both light sensors and store this data in two lists.

#### Note:

When you look at your data, you may notice that the first measurement for each light sensor is much larger than the rest of the measurements. If this is the case, just measure and discard one pair of light sensor values at the beginning of your program, before you begin recording data.

#### Exercise 2:

Create a new list named sumLight. This list should contain the sum of the right and left light sensor values for each location.

#### Exercise 3:

Declare a variable named maxLight. Its initial value should be 0. Then use a for loop to move through the sumLight list. If you find a value bigger than the current value of maxLight, set maxLight equal to this new, larger value. At the end of the for loop, you have found the maximum light value observed by the Finch! How do you know this is the maximum value?

#### Exercise 4:

Declare a variable named minLight and use it to find the minimum value in sumLight . What should the initial value of minLight be?

Finch programs can use lists to store values relevant to the program, such as user input. For the rest of this lesson, you will create a way for young children to program the Finch.

The Finch will have two modes. In the recording mode, a child can tilt the Finch in different directions. Your program will save this sequence of tilts in a list. In the play mode, the Finch will translate the sequence of tilts into a sequence of movements.

#### Exercise 5:

First, define a list that contains the possible Finch orientations. An example is shown below. Write a function called findOrientation() that returns one of these orientations for the Finch based upon the Finch accelerometer values.

#### Exercise 6:

Next, write a function to handle the recording mode. In this mode, the user will tilt the Finch in different directions (Beak Up, Beak Down, Left Wing Down, Right Wing Down, and Level). Your goal is to record this sequence of tilts in a list called listOfOrientations. The requirements for record mode are listed below.

• The Finch should record for approximately 30 seconds. Hint: time.time() will return the current system time in seconds.
• The robot should record the Finch’s orientation every 0.1 seconds (you can use your function from Exercise 5!). This orientation should be added to the end of listOfOrientations. You may ignore the In Between orientation.
• When the Finch is in record mode, its beak should be red.
• When the Finch leaves record mode, its beak should turn off (no light).
• The function should return listOfOrientations.

#### Exercise 7:

Write a function to handle the play mode. In this mode, the program should move through listOfOrientations. For each orientation in the list, the Finch should make a movement. The requirements for play mode are listed below.

• The function should accept listOfOrientations as a parameter and move through each item in this list.
• The Finch orientations (Beak Up, Beak Down, Left Wing Down, Right Wing Down, and Level) should correspond to different movements of the robot. For example, Level should correspond to stopping the Finch. You should make the other four orientations correspond to these four Finch movements: forward, back, turn right, and turn left. You may choose which orientation corresponds to each movement.
• Each item on the list should make the Finch move (or stop) for 0.1 seconds.
• When the Finch is in play mode, its beak should be green.

#### Exercise 8:

Use your functions from the last two exercises to make the Finch record the user’s gestures and then play them. You may want to include a pause between the two modes to give the user time to set the Finch on the floor before it goes into play mode. When you have a working project, be sure to find some younger students to try it out!

#### Extension: Finch Plays Simon

Program your Finch to play a modified version of the memory game Simon. The Finch will print an orientation, either Beak Up, Beak Down, Left Wing Down, or Right Wing Down. You will then need to move the Finch to that orientation. If you do this successfully, the Finch will print a new orientation – you’ll have to move the Finch to the first orientation, then to the new one. This game goes on for as many moves as you can remember. When you finally mess up, the Finch will tell you how many moves in a row you got right and either compliment or insult your performance.

• Start by writing a function that accepts a Finch orientation and determines whether the user places the Finch in that orientation within 5 seconds.
• What components can you reuse from this lesson?
• If the user puts the Finch in the target orientation any time within 5 seconds, the function should return True.
• Otherwise, the function should return False.
• The main portion of the program should include a loop that ends when the player makes an incorrect move. Within this loop, the program should do the following:
• Randomly generate a new move: Beak Up, Beak Down, Left Wing Down, or Right Wing Down.
• Add the new move to a list called listOfOrientations.
• Print the move to the screen.
• Print that the user needs to repeat all of the moves done so far.
• Move through listOfOrientations and check that the user moves the Finch to each orientation in the list.
• If the player gets a move correct, the Finch’s beak should flash green for 1 second, and the Finch should beep happily.
• If the player gets the whole sequence correct, the game should continue and generate a new move for them to remember.
• If the user misses any of the movements in the list, the game is over.
• At the end of the game, the program should tell the user the maximum number of moves they successfully completed. Based on this number, the program should also compliment or insult the user.