By Valérie Gillon, team Wallonia
To see the original article in French, go to: https://drive.google.com/drive/u/2/folders/1UNS0W0EuDjFqqF16fiYe-b8nGj1zyZZ9?ogsrc=32
Dutch translation coming soon.
For this workshop you will need:
To complete this tutorial, we suggest using mu editor https://codewith.mu/.
For example, let's play a do:
To see the original article in French, go to: https://drive.google.com/drive/u/2/folders/1UNS0W0EuDjFqqF16fiYe-b8nGj1zyZZ9?ogsrc=32
Dutch translation coming soon.
For this workshop you will need:
• Two crocodile clips
• Earphones
• A micro: bit 😊
To complete this tutorial, we suggest using mu editor https://codewith.mu/.
1. Connect our earphones
Here we will use the 2 crocodile clips to connect
the microbit and the earphones.
First, place a clip on port 0 of the Microbit and
one on the GND port (ground) like this:
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.
The other crocodile
clip is then connected, linking port 0 to the tip of the earphone jack. Like
this :
Great, our earphones are now connected!
2. DADADADUM
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.
To be able to play music, we have to use the music library and import it into our code.
import music
Then we will use this library to play a melody.
music.play(music.DADADADUM)
Then we will use this library to play a melody.
music.play(music.DADADADUM)
Let's test these 2 lines of code… So you see what is dadadadum now !!!!
The Micro:bit
has a lot of music already programmed. Here is the list :
music.DADADADUM
music.ENTERTAINER
music.PRELUDE
music.ODE
music.NYAN
music.RINGTONE
music.FUNK
music.BLUES
music.BIRTHDAY
music.WEDDING
music.FUNERAL
music.PUNCHLINE
music.PYTHON
music.BADDY
music.CHASE
music.BA_DING
music.WAWAWAWAA
music.JUMP_UP
music.JUMP_DOWN
music.POWER_UP
music.POWER_DOWN
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!
Musical notes
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.
Do=C | Re=D | Mi=E | Fa=F | Sol=G | La=A | Si=B |
For example, let's play a do:
music.play('C')
Now the
duration!
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).
4. Sound Effects
And the Octaves?
Octaves are indicated by a number. 0 is the lowest and 8 is about as high
as you need, unless you do music for your dog. here find some examples:
music.play('C')
music.play('C1')
music.play('C5')
So, what is the default value? Did you guess?
Now the
duration!
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).
music.play('G:2')
music.play('G:2')
music.play('G:2')
music.play('E:4')
Silences
If you use the note named R (R for Rest or
silence) then the Micro:bit will pause (silence) for the specified duration.
music.play('G:2')
music.play('G:2')
music.play('G:2')
music.play('E:4')
music.play('R')
music.play('F:2')
music.play('F:2')
music.play('F:2')
music.play('D:4')
A little flat note
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?
It's actually quite
simple: a flat is indicated by b
(lowercase) and a sharp by a #
How to correct this
last note? Sharp or Flat? Try to find by yourself, before looking at the answer!!!
music.play('G:2')
music.play('G:2')
music.play('G:2')
music.play('Eb:4')
music.play('R')
music.play('F:2')
music.play('F:2')
music.play('F:2')
music.play('D:4')
Yes! It needed a Mi (or E) flat!
Another way to write all these notes is to make a
list first:
tune = ["C4:4", "D4:4", "E4:4", "C4:4", "C4:4", "D4:4", "E4:4", "C4:4",
"E4:4", "F4:4", "G4:8", "E4:4", "F4:4", "G4:8"]
music.play(tune)
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.
For example:
Chorus
Verse
Chorus
Verse
Bridge
Chorus
Chorus
Final
4. Sound Effects
Python allows you to make sounds that are not musical notes. For
example, here's how to create a police siren effect:
for x in range(5):
for freq in
range(880, 1760, 16):
music.pitch(freq, 6)
for freq
in range(1760, 880, -16):
music.pitch(freq, 6)
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".
So here it's for each
element in the range from 0 to 5. The loop goes through every number from 0 to
5, and then repeats itself 5 times (0, 1, 2, 3, 4 because it starts at 0 )
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.
ReplyDeleteBe 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.
To get rid of this "plop", you can start the next tone before the current tone has ended. Like so:
from music import pitch, set_tempo
from microbit import sleep
set_tempo(bpm=60)
def siren():
for x in range(5):
for freq in range(880, 1760, 16):
pitch(freq, 20, wait=False)
sleep(10)
for freq in range(1760, 880, -16):
pitch(freq, 20, wait=False)
sleep(10)
siren()
<pre>
ReplyDeletefrom music import pitch, set_tempo
from microbit import sleep
set_tempo(bpm=60)
def siren():
for x in range(5):
for freq in range(880, 1760, 16):
pitch(freq, 20, wait=False)
sleep(10)
for freq in range(1760, 880, -16):
pitch(freq, 20, wait=False)
sleep(10)
siren()
</pre>