The Research Experience for Teachers Program /

Melody Lab Activity

Introduction/Motivation:

When you hear Beethoven’s Moonlight Sonata, oftentimes your first thought is not “How could I program my computer to do this?” In fact, you may think it’s really difficult to do this on the computer, however, with the littleBits Arduino, and a little bit of code, we can create a melody we want, effectively combining two vastly different aspects of our everyday life: Computer Science and Music. The goal of this lab is to first play a small melody – but then to see if we can play a much larger piece of music!

Materials List:

  • Two Micro-USB to USB adapters (should come with the Arduino and littleBits kit)
  • littleBits w6 Arduino Module
  • littleBits p3 USB Power Module
  • littleBits o24 Synth Speaker
  • The PC, Linux Machine, or Mac Desktop that downloaded and installed the software required for the “Setting Up the littleBits Arduino” lab activity

Procedure:

Background/Preparation:

You should know how to read and understand ISO C++ code for the Arduino, as well as modify the code for the Arduino to suit your own purposes. For the musical side, should have the notes of a song picked out, or must be able to read music.

Activity:

This lab activity is designed to be completed by a singular person. It should only take around 30 -45 minutes to complete. In this activity we will explore how to make music for the o24 Synth Speaker.

The first step towards music is to open the Sample Arduino code given in the IDE. If you have the Arduino IDE open, the example file is under File -> Examples -> 02. Digital -> “toneMelody”. Once this file is open, we must interpret the code that is given in it. Because this code contains many comments, it is easy for us to understand the code. However, as some of the code can be challenging to read, I have written a description of the important parts, essentially the code necessary to understand how to play the melody.

If we want to understand the melody, we must first look at the “pitches.h” file. You may have not seen this file, but if you look in the top left corner, just below the upload button, you will see the tab called “toneMelody”. Right next to this tab is another one called “pitches.h.” A quick look at “pitches.h” may not reveal much, it seems like a series of notes next to a series of numbers. These numbers are essentially coordinates, which tell the o24 Synth Speaker which note to play. This way we can type in the note the Synth Speaker should play, “pitches.h” will look up the associated coordinate and tell the Synth Speaker.

Going back to the “toneMelody” tab, we can look at where the notes are kept – in a array called melody[], which consists of a series of 8 notes. (Notice how one of the eight notes is a “0,” this is a rest) We can then see that the melody array is accessed later in the void setup () in the function tone() as so: tone(8, melody[thisNote], noteDuration). Now, if you’re asking what thisNote is, it’s quite simple, it’s an integer variable that tells us which note in the melody we are playing. thisNote is created in the for loop, starting at 0 (playing the 0th [remember computers start counting at 0] note) , and increasing by 1 (thisNote++) each time we travel through the loop.

thisNote also accesses another array, called noteDurations[]. This array tells us exactly how long the note is supposed to last. A 4 means a quarter note, an 8 means an 8th note, so on and so forth. This noteDurations[] array is then used in the variable noteDuration, which is equal to 1000/noteDurations[thisNote]. This is a simple and easy way to make a rhythm for our music.

There is one more variable accessed by the function tone(), the first 8. This 8 is the digital pin 8 on our Arduino. WE need to change it to an OUTPUT pin we can use with littleBits: 1, 5 9.

With this knowledge, we know exactly how to change the code to make our melody. If we need to change the number of notes in our melody, we have to add items to both arrays, and increase the “thisNote < 8”, in our for-loop to how many notes we want to play in our melody. If we want to increase the tempo, we need to decrease noteDuration by decrease the 1000. This means we can implement any melody or piece of music we want to play!

Try to make the speaker play the first line of “Twinkle Twinkle!” For reference, the first line is given below.

This shows the note tone and the rhythm of the tone. You will have to input the notes correctly into melody[] and noteDurations[]. Also, take note that this melody only has 7 notes, remember to make the appropriate changes!

Results:

There is one obvious result in this lab: Music! We can play “Twinkle Twinkle’s” first line, and even code for the rest of the song! We canmake the melodies of other songs now too. But that is not the only way we have benefited from this lab. With this lab, we learn more about for loops and arrays. We learn how to access two arrays with one variable, so that we can make sure we’re playing the right note for the right length. This will be extremely vital knowledge for anyone who wants to learn more about computer science.

Future Work:

If you want to do more with music, try creating a piano keyboard on your computer keyboard, so that any key you press you produce a note. You could also attach another Synth Speaker to pair a bass line or harmony line with your melody. There are many possibilities to do with just the Synth Speaker and your computer, you simply have to explore them.

Additional Resources: