To complete this tutorial, we suggest using mu editor https://codewith.mu/.
Here we will use the 2 crocodile clips to connect the microbit and the earphones.
Then connect our crocodile clips to one of the earphones. We connect the ground (GND port), here the black wire, at the base of the earphone plug.
Great, our earphones are now connected!
What a strange tittle? LOL 😉 wait, you will understand. Let's open our Python code editor: mu editor.
To be able to play music, we have to use the music library and import it into our code.
Then we will use this library to play a melody.
Let's test these 2 lines of code… So you see what is dadadadum now !!!!
You can take the example code and change the melody. What is your favorite? How could you use these tunes, as signals or clues, in some of your games?
3. Be your own Mozart!
Now you can create your own melodies too! That's really cooool!
The Micro:bit is from England, it does not use "Do Ré Mi" but the letters of the alphabet to identify the musical notes whose A is the "La" of the tuning fork.
So, what is the default value? Did you guess?
The note duration is also expressed using numbers.
The longer the value of the duration, the longer the note will be played. These values ​​are related to each other - a duration of 4 will last twice as long as a duration of 2 (and so on).
If you use the note named R (R for Rest or silence) then the Micro:bit will pause (silence) for the specified duration.
Hmmm, there is a problem with the code above, right? It doesn't sound right. It lacks alterations like sharp and flat. In code, how do we do it?
How to correct this last note? Sharp or Flat? Try to find by yourself, before looking at the answer!!!
Yes! It needed a Mi (or E) flat!
Go ahead, write the rest of the song!
For example, you could write a pop song using a variable for each part. Then play them using a typical canvas of a song.
First, like you don't want to drive insane friends next to you, we decided to ring the siren only 5 times.
To do this, we used a for loop that will repeat the siren 5 times. This loop for that amounts to saying "for each element in a certain collection, do stuff with".
You can note how the music.pitch method is used in this example. It is waiting for a frequency, that is, a pitch of sound. For example, a frequency of 440 is the same as a note A (which corresponds to the LA) used to tune a symphony orchestra.
In the above example, the range function is used to generate an assortment of numeric values. The three arguments of the range function are the start value, the end value, and the step size. In this way, the first use of range is to say, "created an assortment of numbers between 880 and 1760 from 16 to 16". His second use says "created an assortment of numbers between 1760 and 880 from -16 to -16". This is how you get a list of frequencies that go up and down like a siren.
We also used another for loop. More precisely, in the example above it says "for each frequency in the frequency set, play the height of this frequency during 6 milliseconds". Note that things to do in this loop are indented (as we saw earlier) so that Python knows which code to execute with each element.
Go ahead now, change these loops and the values ​​used to make another known sound.
Be very careful if your ninja's are using headphones or earpĥones because you cannot control the volume of the sound level from the micro:bit. To control the volume, you'll need some external circuitry or headphones or earphones with volume control.
For show and tell we use a speaker (like the Sony SRS-XB10). You will need a stereo kabel (3,5 mm, male-male).
We bought a micro:bit audio kabel (Kiwi Electronics). Makes changing earphones or speakers easier for our ninja's.
Your siren doesn't make a continuous sound. There is an annoying "plop" switching off a tone and switching on the next tone.