The Program

Download the sketch.

It's a good idea to become familliar with how the Musical Instrument Shield works. Check out Sparkfun's Quickstart Quide for an example code and a brief explanation on the general principles.

The MIDI statements use the NewSoftSerial library to communicate with the Arduino.

The code is pretty straight-forward. It starts by defines all the variables for the inputs and outputs (i.e. LEDs and sensors). Arrays are used for the IR emitters and detectors, which will correspond to the arrays of notes the program will call upon given the state of each buttons count (We will discuss that in a bit). The setup function declares what time of pin (pinMode) each of the ports on the Arduino are.

Arduino code at its core has two functions. The "setup" (void setup()) is quite literally what it sounds like: it runs through a list of commands that set the board up properly. The "loop" (void loop()) is what happens continuously.

The next part of the setup the shield is initialized (i.e. told to listen to the code), and then it tells all of the IR-emitters to turn on (digitalWrite()).

We want to have a steady beam of infrared light coming from the emitter into the detector. So that means we never want the IR-emitters to turn off, which is why we declare them on in the setup.

The program reads the amount of infrared light coming into the detector and triggers a function (noteOn) if the detectors input goes above a certain threshold (which is caused when you block the detector with your finger). After a certain amount of time (tempo) the program checks again to see if the detector's value is still greater than the threshold and, if it is still greater than the threshold, triggers another function (noteOff). The amount of time between each "check" (tempo) can be manipulated by one of the potentiometers. The second potentiometer is what controls the function for the master volume.

Each of the 4 buttons keep a count every time they are clicked, and reset back to 0 after they've reached their limit. Each button has a different limit, which corresponds with a different function. The first button controls the function for the instrument you want to play. In this sketch their four selections to choose from. The Musical Instrument Shield comes equipped with General MIDI - which comes with 128 unique instrument sounds. This sketch only uses four unique sounds, but you can change that within the sketch.

If you look at line 49 of the sketch you will see:

// INSTRUMENT INFORMATION: const int instrument0 = 79; // Ocarina const int instrument1 = 107; // Koto const int instrument2 = 104; // Sitar const int instrument3 = 53; // Voice Oohs

By change the values of each of these you can load new sounds. To learn each sound's number reference page 32 of the MIS Datasheet, or the image below.

Know that the sounds on the board are zero indexed (meaning the count starts at 0 not 1). Therefore, 1 on the datasheet (Acoustic Grand Piano) is actually 0 in the code , and 128 on the datasheet (Gunshot) is 127. Just remember to subtract 1 from the number you see on the sheet to get the desired sound.

The next button controls the count of the key. In this sketch there are seven key choices (A, B, C, D, E, F, G). The count starts at the key of A. The next button controls the scale. In this sketch you can choose between Major, Minor Melodic, and Minor Harmonic. The last button controls the octaves, of which you have two choices.