Lesson 11 – Buzzer

Java Concepts

Arrays

Teacher Materials

Get Access

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() method to play a note using the buzzer. The method requires an integer representing the note and a double 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. A beat is roughly equal to one second.

The playNote() method 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.

Arrays

If you want to play a song with the Finch, it is easier to use an array. An array in Java is a group of values of a given type. To declare an array of a given type, place square brackets after the type and before the name of the array. You can define the elements in an array by placing them within curly braces, separated by commas. For example, this code declares an integer array notes that includes five elements.

You can access individual elements in an array using square brackets. The elements of the array are indexed from 0 to the length of the array minus 1. For example, in an array with 10 elements, the elements are indexed from 0 to 9. The code below prints the first and last elements of the array named notes. Notice that notes.length is the number of elements in the array.

You can use a for loop to access each of the elements of an array. 

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

Exercise 4

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 array that contains the number of beats for each note. Then modify your code from Exercise 3 so that each note is played for the correct number of beats.

You can also use arrays 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 an array to store 20 measurements from a Finch light sensor. Then move through the array 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 int 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 the last part of this lesson, you will use the Finch to measure a user’s reaction time and store this data in an array. 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 method 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.

  • This method will return a decimal value that is the user’s reaction time in seconds.
  • At the beginning of the method, ask the user to place the Finch in a level position and wait until he/she has done so. Hint: Use your isLevel() method 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 System.currentTimeMillis(). This function returns the number of milliseconds since January 1, 1970 (arbitrary, but useful). It returns a value of type long (a long integer).

      • Wait until the user moves the Finch out of the level position. How can you use your isLevel() method to do this?
      • Use System.currentTimeMillis() to measure the time again.
      • Return the difference between endTime and startTime multiplied by 0.001. This is the user’s reaction time in seconds!
      • Remember to give the user instructions so they know what to do!

      Exercise 7

      Once you have a method to measure the reaction time, you should write a program that uses this method to measure 5-10 reaction times. You should save all the reaction times in an array.

      Exercise 8

      Create a variable named sum. Use this variable in a for loop to add up the ten reaction times. Then calculate the mean reaction time and display it for the user.

      Extra Challenge

      Create a new method to measure the user’s mean visual reaction time. To do this, you will need to modify your reaction time method 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: Make a Game

      You can also use arrays 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 method 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 method 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 an array 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 array.
      • 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 array, the game is over. At the end of the game, the program should tell the user the maximum number of moves they successfully completed.