You can use musical note frequencies in the chart below for the tone function’s f parameter to play music.
Example:
bot(22).tone(1975.5, 500)
This command plays a B6 note for half of a second. Using this chart, you are able to create songs with the piezospeaker and the cyber:bot.
The notes to the first line of the Happy Birthday song go in the following order: D - D - E - D - G - F. Matching the notes with their frequencies, we can make the following program.
Example script: happy_birthday
This example script plays out the first line of “Happy Birthday.”
- Set the project's name to happy_birthday, enter the script below, and then click Save.
- Click Send to micro:bit.
- Set PWR to 1.
# happy_birthday from cyberbot import * bot(22).tone(2349.3, 125) #D for an eighth bot(22).tone(2349.3, 62) #D for a sixteenth bot(22).tone(2637.0, 250) #E for a quarter bot(22).tone(2349.3, 250) #C for a quarter bot(22).tone(3136.0, 250) #G for a quarter bot(22).tone(2793.8, 500) #F for a half
- Did you hear your cyber:bot play Happy Birthday?
- Set PWR to 0.