Skip to content

Music Instrument Shield

Setup:
Assembly
To be able to connect the Music Instrument Shield to the mainboard, some soldering is required. The Music Instrument Shield needs to be soldered on some headers. Once it is done, connect it to your arduino.

Software/Firmware
If your arduino IDE version is 1.0 or later, the NewSoftSerial Library is installed so you won’t have any trouble at all.If you have an earlier version of the IDE, you need to upgrade or install the library.
Example Code:
#include
NewSoftSerial mySerial(2, 3);
byte note = 0;
byte resetMIDI = 4;
byte ledPin = 13;
int instrument = 0;

void setup() {
Serial.begin(57600);
mySerial.begin(31250);
pinMode(resetMIDI, OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
}

void loop() {
talkMIDI(0xB0, 0×07, 120); //

/*
//Demo Basic MIDI instruments, GM1
//
Serial.println(“Basic Instruments”);
talkMIDI(0xB0, 0, 0×00); //Default bank GM1
//Change to different instrument
for(instrument = 0 ; instrument

Serial.print(” Instrument: “);
Serial.println(instrument, DEC);
talkMIDI(0xC0, instrument, 0);

//Play notes from F#-0 (30) to F#-5 (90):
for (note = 30 ; note
Serial.print(“N:”);
Serial.println(note, DEC);

//Note on channel 1 (0×90), some note value (note), middle velocity (0×45):
noteOn(0, note, 60);
delay(50);

//Turn off the note with a given off/release velocity
noteOff(0, note, 60);
delay(50);
}

delay(100);
}
//
*/

//Demo GM2 / Fancy sounds
//
Serial.println(“Demo Fancy Sounds”);
talkMIDI(0xB0, 0, 0×78);
//For this bank 0×78, the instrument does not matter, only the note
for(instrument = 30 ; instrument

Serial.print(” Instrument: “);
Serial.println(instrument, DEC);

talkMIDI(0xC0, instrument, 0);
//Play fancy sounds from ‘High Q’ to ‘Open Surdo [EXC 6]‘
for (note = 27 ; note
Serial.print(“N:”);
Serial.println(note, DEC);

noteOn(0, note, 60);
delay(50);
noteOff(0, note, 60);
delay(50);
}
delay(100);
}

/*
//Demo Melodic
//
Serial.println(“Demo Melodic? Sounds”);
talkMIDI(0xB0, 0, 0×79);
//Change to different instrument
for(instrument = 27 ; instrument

Serial.print(” Instrument: “);
Serial.println(instrument, DEC);

talkMIDI(0xC0, instrument, 0);
//Play notes from F#-0 (30) to F#-5 (90):
for (note = 30 ; note
Serial.print(“N:”);
Serial.println(note, DEC);

noteOn(0, note, 60);
delay(50);
noteOff(0, note, 60);
delay(50);
}
delay(100);
}
*/
}
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) {
talkMIDI( (0×90 | channel), note, attack_velocity);
}
void noteOff(byte channel, byte note, byte release_velocity) {
talkMIDI( (0×80 | channel), note, release_velocity);
}
//Plays a MIDI note. Doesn’t check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin, HIGH);
mySerial.print(cmd, BYTE);
mySerial.print(data1, BYTE);
//(sort of: http://253.ccarh.org/handout/midiprotocol/)
if( (cmd & 0xF0)
mySerial.print(data2, BYTE);

digitalWrite(ledPin, LOW);
}
There are sixty “notes” that can be played in this bank, with values from 27 to 87. In order to “play” a note, we need to send the Note On command with the value of the note we want to play. This is the next MIDI instruction in the example code:

noteOn(0, note, 60);

After the noteOn() call, there is a delay of 50 milliseconds. This means that whatever note we chose will play for 50 milliseconds. After the delay, we have to tell the chip to turn the note off. Hence, the noteOff() function call:

noteOff(0, note, 60);