Activity: Making Music (Beginner)
Use the Rovers speaker to make it play songs. Learn to combine the world of music and robotics by going through sheet music and coding the Rover to play each note.
Basics of Music:
When it comes to reading sheet music for this task, we will be using two pieces of information from each note. The tone of the note and how long the note is played.
As the image to the right shows, a note can sit on a line or a space. You may find many notes of the same letter (tone) but at a higher pitch (different octave)
Songs can be divided into a rhythm, the constant beat the music follows. Shown in the diagram to the right, we have the note durations. One whole note (semibreve) lasts for 4 beats. Next, minums last for 2, and crotchets last for 1. We’ll only use semibreves, minums, and crotchets for basic songs.
Below is an example of ‘Mary Had a Little Lamb’ in sheet music. The letters have already been written below the notes. This is the example we will use for the guide, but feel free to find your own songs or even write your own music!
Code:
Constant - A constant is a variable that never changes when the code is run. In python, we write this in all capitals with underscores between words.
1) Setting the Constants:
Now we need to start setting up the framework in our code to make music. We’ll begin by setting constants for each of the note lengths. A constant is a value used repeatedly throughout the code that never changes, which is perfect for what we need here. As all the notes are multiples of the Crotchet, we can use the diagram above and some simple multiplication to set all the constants. We also need to decide on an octave to use, so make a comment to remember this. For this example, we’ll use octave 4.
2) Making the Music:
This part can take a little while, but we will use the ‘Play Note’ and the ‘Delay’ blocks to play each note individually. The ‘Play Note’ block starts playing the note on the Rover, and the ‘Delay’ block controls how long the note is played. We use the constants we made before to control how long we delay. Do this for each note in the song with the correct note lengths.
Complete Code:
We went through and completed part of the example song. Try to finish the rest of the song on your own:
Up Next:
Ready to challenge yourself with faster ways to code music? Head on over to the intermediate or advanced ‘Making Music’ guides!