“Twinkle twinkle little star, how I wonder what you are...”. Do you wanna try playing songs with keyboard? Micro:bit board can realize the functions of electronic organ, such as playing music, making musical accompaniment, adjusting playback speed, or even performing music. So many interesting functions, get started quickly if you like them! In this chapter, we are going to learn the “sound” module, micro:bit pins and button.
We will use sound module to write a program. The program needs to achieve the following functions: press down 1, 2, 3… on the keyboard, then the sound effect, Do, Re, Mi…will be displayed. Then we can create our own songs.
The eight numbers 1 to 8 on the keyboard respectively represent Do, Re, Mi, Fa, Sol, La, Ti, Do(high-pitch), and the corresponding English letters are C, D, E, F, G, A, B, C2. Shown as below.
- Set the sprite “keyboard”, there are two different built-in shapes for keyboard: keyboard a and keyboard b.
- Add background for the stage.
- Press down the keys on the keyboard to start the programs, play the notes
and switch the shape. The detailed programs are shown in chart 2-2：
Add background for the stage, run the programs and check the result.
Apparently, the sound module in Mind+ cannot meet all our needs. So we have to use the function module in “Extensions”. Open the “function” module in extensions, and select “music”. We can set the beat of the notes in this module.
Now, let’s take a look about the micro:bit pins. As figure 2-3 shows, there are 25 external connectors on the edge connector of micro:bit, which we refer to as “pins”. Program motors, LEDs, or other electrical components with the pins, or connect extra sensors to control your code!
The first three, labelled 0, 1 and 2 are flexible and can be used for many different things-which means they are often called “general purpose input and output” (GPIO for short).
Connect micro:bit board and the computer with micro USB. Let pin P0 play the music “twinkle, twinkle little star”, and P1 and P2 display different instruments. Press button A and button B to play faster or slower. As shown in Figure 2-4.
Code Blocks Descriptions:
Touch the micro:bit pin with your one hand, and touch the GND with another hand, by which to form a closed loop between the board and your body, then program starts. Touch pin P1 or P2 to add accompaniment for the performance, and press button A and B on the micro:bit to control the playback speed.
So far, we only tried the electronic organ, do you want to play other instruments? How to switch different instruments randomly, think over and give it a try!