In this project, we continue using the sound functions to play a version of "Mary Had a Little Lamb."

This project only requires the speaker — make sure it is hooked up as it was in .

Below is an example of how you can play a musical tune using the CREATOR Kit:

The code above is pretty simple — we create a list of notes, and then we use a for loop to loop through them, play each in turn. Now, one issue with this code is that each note is of a fixed duration. But, in most songs, you have notes of varying length (some longer and some shorter).

We can modify this code to specify the length of each note independently. We do that changing the Note() into a list that contains both the note and the note's durations. Then, in our for loop, we use both the note and the duration in our play() function.

Here is what the code would look like:

There's an interesting thing going on with the above notes_and_durations list. It is no longer just a list of Notes — instead, it is now a lists of lists! Each "sublist" contains one Note and one number — the length of time to play the note, in seconds.

In the for loop, you'll see that there are two variables, note and duration, separated by a comma. When the for loop runs, the note and duration are set to each successive pair of values in the notes_and_durations list. For this to work, each sublist in notes_and_durations has to have exactly two items!

  1. Create your own song by changing the notes and durations.

  2. Can you modify the code to play a song with silence (instead of notes) during parts of the song? Hint: One possible way to do this is by adding another item to each sublist, say 1 or 0. If that item is 1 then play the note for the duration, if not then pause silently for the duration.

  3. Add a variable called SPEED to the beginning of the program. If you increase the SPEED, it will make the song run faster.

  4. When defining the notes in the first example, Note is written out each time a note is created. To create a more concise program, can you create the notes by looping through each note of a string? In other words, the whole song above could be represented by "EDCDEEEDDDEGGEDCDEEEEDDEDC".