## Lesson 11 – Buzzer

Lists

#### Teacher Materials

In the previous lessons, you have learned to use the Finch lights and motors, as well as the micro:bit display. There is one more output, the buzzer inside the Finch. In this lesson, you will learn to use lists to play simple songs using the buzzer.

### Using the Buzzer

Use the playNote() function to play a note using the buzzer. The method requires an integer representing the note and a number giving the number of beats. The note number is the MIDI number for a particular musical note. For example, middle C is 60. This website may be helpful in matching note numbers to names. The number of beats can be a decimal number. A beat is roughly equal to one second.

The playNote() function starts the note and moves on immediately to the next line of code. To play a series of notes, you will also need to add pauses between them, as shown in the code below.

#### Exercise 1

Try out the sample code above, and then modify it to play three different notes.

#### Exercise 2

What is the difference between Example 1 and Example 2? Make a hypothesis, and then test it out.

### Lists

If you want to play a song with the Finch, it is easier to use a list. A list in Python is a group of values. You can define a list as a by placing its elements in square brackets, separated by commas. For example, this code defines a list notes that includes five integers.

Remember, a for loop sets the variable i  equal to each item in a list. In these lessons, you have mostly been using lists created with the range() function, but you can use any list with a for loop. For example, the code below sets i equal to each of the elements in notes. The first time through the loop i is 60, the second time i is 65, etc.

#### Exercise 3

Try out the sample code above, and then modify it to play a song of your own. For some songs to try, see this website (if you read music) or this one (if you don’t).

### Accessing Elements in a List

You can access individual elements in a list using square brackets. The elements of the list are indexed from 0 to the length of the list minus 1. For example, in a list with 10 elements, the elements are indexed from 0 to 9. The code below prints the first and last elements of a list named notes. Notice that the len() function returns the length of a list.

You can use a for loop with the range() function to access the individual elements of a list. This loop will cause the same behavior as the one in Exercise 3.

#### Exercise 4

Modify your song from Exercise 3 to access the individual elements of the list using square brackets, as shown above. To make your song sound better, it would be helpful to vary both the note and the length of the note. To do this, create a second list that contains the number of beats for each note. Then modify your program so that each note is played for the correct number of beats.

You can also use lists to store data collected from the user. For example, the code below measures the value of the Finch’s distance sensor every second. It stores this data in a list called distanceMeasured.

#### Exercise 5

Use a list to store 20 measurements from a Finch light sensor. Then move through the list and use each measurement to make the Finch make a buzzing sound. Remember, the Finch can only play notes between 32 and 135.

#### micro:bit V2 Extra Challenge

If your Finch contains a micro:bit V2, you can also use a sound sensor in the micro:bit to measure the volume of sound around the Finch from 0 – 100. The getSound() method returns the value of this sensor. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use this method.

Write a program that measures how loud the Finch buzzer is. Play ten different tones with the buzzer, and during each tone, measure the volume with the sound sensor. Then print the results to the screen. Is the volume different for different tones?

### Measuring Reaction Time

In this part of the lesson, you will use the Finch to measure a user’s reaction time and store this data in a list. The reaction time is the amount of time that it takes someone to respond to a stimulus. A stimulus is something that happens in the person’s environment, like a beep or a change in color. As an example, try using this website to measure your visual reaction time. In this case, the visual stimulus is when the screen changes from red to green. The website measures how long it takes you to respond to this change.

#### Exercise 6

Write a function that uses the Finch to measure a person’s auditory reaction time. The stimulus will be a short buzz from the Finch. Once the buzz starts, you will measure how long it takes the user to react by moving the Finch out of the level position.

• At the beginning of the function, ask the user to place the Finch in a level position and wait until he/she has done so. Hint: Use your isLevel() function from the last lesson!
• The Finch should wait for a random number of seconds from 2-5 and then buzz. Why is it important that the timing of the stimulus be random?
• As soon as the Finch starts to buzz, measure the startTime. You can do this using the time() function, which you can import from the time module. This function returns the number of seconds since January 1, 1970 (arbitrary, but useful).
• Wait until the user moves the Finch out of the level position. How can you use your isLevel() function to do this?
• Use the time() function to measure the time again.

• Return the difference between endTime and startTime. This is the user’s reaction time!
• Remember to give the user instructions so they know what to do!

#### Exercise 7

Once you have a function to measure the reaction time, you should write a program that uses this function to measure 5-10 reaction times. You should save all the reaction times in a list. Print all the reaction times to the screen, and calculate and display the mean reaction time.

Hint: The sum() function may be useful.

#### Extra Challenge

Create a new function to measure the user’s mean visual reaction time. To do this, you will need to modify your reaction time function to use a visual stimulus, such as the Finch’s beak lighting up. How does the mean auditory reaction time compare to the mean visual reaction time?

### Extension: Making a Game

You can also use lists to store information as you make a game. You will program your Finch to play a modified version of the memory game Simon. The Finch will print an orientation: Beak up, Beak down, Tilt left, or Tilt right. 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.

#### Exercise 8

Start by writing a function that accepts a Finch orientation and determines whether the user places the Finch in that orientation within 5 seconds.

• 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.

#### Exercise 9

Write a function to randomly generate a new move: Beak up, Beak down, Tilt left, or Tilt right.

#### Exercise 10

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:

• Add a 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 0.5 seconds, 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.