RBI Chapter 7
Chapter 7: Beep, Beep, Beep
Things You Will Learn
- Basics of Frequency & Sound
- Making your robot beep
When you used your gamepads, you must have noticed that your robot can beep. The gamepad only allowed the robot to beep in three different tones. What do you think determines these tones? Frequency is what allows the robot to play different tones. Let’s learn more about frequency.
Frequency & Sound
What is your favorite sport? Basketball, swimming, bicycling? Do you sometimes measure the amount of time it takes for you to shoot 10 baskets, the amount of time it takes for you to complete 2 laps when you go bicycling or swimming? Well, frequency is the opposite measure – it measures how many baskets you can shoot in one second or how many or much of a lap you can finish in one second. Like time is measured in seconds, the units for frequency are Hertz (Hz). Thus, 1 Hz means one lap or basket or cycle per second.
A wave is made up of many cycles. One such cycle in a wave is shown in Figure 1. You can make similar waves using a slinky. If you move one end of the slinky back and forth repeatedly while the other end is held steady, you will produce such waves.
Sound waves work similarly. When your friend talks to you, the sound travels in your direction in the form of waves to deliver the message to you. The number of cycles that a sound wave completes in a second is known as its frequency.
Now, let’s put sound and frequency together: A sound wave with a frequency of 300 Hz completes 300 cycles in a second. While humans can hear frequencies ranging from 20 Hz to 20,000 Hz, your robots only function well between a range of 200 Hz to 9000 Hz.
To make your robot beep a random note, type in the following:
This note played only for half a second. If you want to play a longer note, you can give the function the number of seconds you want it to play for inside the parentheses.
To make your robot play a note with a certain frequency, you can put this value inside the parentheses as well in the following way:
>>> beep(time, frequency)
For example, to make your robot beep the first note in “Do-Re-Me…”, type in the following:
>>> beep(1, 260)
Therefore, what you just typed in made your robot beep at a frequency of 260 Hz for 1 second.
Remember that frequency values can range from 200Hz to 9000 Hz. Time values should range from 1 second to 5 seconds.
When you typed in any of the commands shown above, you only heard one beep. What if you wanted a bunch of notes together? That’s right, you should use loops.
>>> loop(beep, 5)
This command will beep five notes with random frequencies.
Note: In the loop function, you will not be able to put in values for time or frequency.
If you want to be able to give the beep function different frequency values to play in a sequence, you must create a list. Here’s how you would do that: First, you would give your list a name – this can be anything, just like you learnt how to store variables. We will call our list “tune.”
>>> tune = [200, 500, 300, 500, 600]
Your list can contain numerous frequency values separated by a comma. Values of each list should be contained within square brackets as shown above.
You can now have the beep function play your list of frequencies by typing:
>>> beep(0.5, tune)
So the format is:
>>> beep(time, list)
Each note will beep for the same amount of time. If you are putting you’re frequencies in a list, you cannot play the notes for different amounts of time.
Note: Do not use the word “list” to name your variables or lists. This is a reserved word.
What if you want your robot to play “Do-Re-Me…”? The first step is to make your own list of frequencies. Let’s try playing “Do-Re-Me…” Table 1 shows the frequencies needed to play this tune.
Note Frequency (Hz) C 260 Hz D 290 Hz E 330 Hz F 350 Hz G 390 Hz A 440 Hz B 490 Hz C` 520 Hz Table 1
Create a list called doReMe that will contain all of these frequencies. To do this, we must type:
>>> doReMe = [260, 290, 330, 350, 390, 440, 490, 520]
Then, we must use the beep function:
You can use a list to play a scale going up or down according to the frequencies you put in your list, the way you just did when trying to play the “Do-Re-Me…” song. However, it does take a while to a put in all of those frequencies. What if you want your robot to play 30 increasing notes? You would have to enter 30 values in a list and then play it. Instead, you can use the scaleUp and scaleDown functions. Type in the following to hear an increasing scale:
>>> loop(scaleUp, 15)
and the following to hear a decreasing scale:
>>> loop(scaleDown, 15)
These functions will play 15 notes going up or coming down according to the function you type in.
Note: You will not be able to specify any frequency or time values for the scaleUp and scaleDown methods.
Your robot can make more music! Use what you have learnt today to play the rhyme Twinkle Twinkle Little Star.