Run Your Script
Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the
open icon in the upper right-hand corner of the editor window. You can also just download this
example by clicking the download button in the lower right-hand corner of the code window.
Note:
You may need to disable your ad/pop-up blocker to interact with the MakeCode programming
environment and simulated circuit!
Code to Note
Let’s take a look at the code blocks in this experiment.
If you are having a hard time viewing this code, click on the image above to get a better look!
Start Melody Repeating
The
Start
Melody
Repeating
block takes all of the frustration out of getting music out of a
microcontroller. It is as simple as selecting one of a number of songs that are preprogrammed into
MakeCode and how many times you want it to repeat and you are done! Note that when a melody is
playing no other code can run, this is called “blocking” code and has to be accounted for you in your
program.
Play Tone for
The
play
tone
for
block is pretty standard if you are used to making sound with other
microcontrollers. For example,
tone()
function in Arduino is pretty much the same as this block.
The
play
tone
for
block accepts a note that you would like the buzzer to produce and the length of
time in beats per second that you would like it to play. So if you are a musician, you are golden to
write horrible robot music for your friends!
What You Should See
What you should see — well, nothing! What you should hear — a song should start as soon as the
program starts to run on your micro:bit! When that song is done you can press one of the two
buttons, and another song will start. Each button has its own song, and there’s another song if you
press both buttons at the same time. Enjoy your sound machine and feel free to swap out the song
of your choice!