Arduino + Musical Instrument Shield

Hello I made this code by pulling from different sources, and it works great with my music instrument shield. When I play piano or bells the sound stops right after I hit the button, However, When I play strings like on a keyboard; the sound continues indefinitely. My guess is the delay function, but your input would be appreciated

//Music Instrument Shield_Keyboard  renew
// Music Instrument Shield_Keyboard

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //Soft TX on 3, we don't use RX in this code

byte inputPins[] = {5,6,7,8,9,10,11,12,13};
byte note[]={72,74,76,77,79,81,83,84,86};
int pinCount = 9;

int buttonState[9];
int lastButtonState[9];  

byte resetMIDI = 4; //Tied to VS1053 Reset line
int instrument = 40;

void setup() {

  //Setup soft serial for MIDI control
  for(int i=0; i<pinCount; i++){
  pinMode(inputPins[i], OUTPUT);//pinMode(outputPins, OUTPUT);
  digitalWrite(inputPins[i], LOW);// this enables the pull up resistors to reduce noise
  pinMode(inputPins[i], INPUT);

  //Reset the VS1053
  digitalWrite(resetMIDI, LOW);
  digitalWrite(resetMIDI, HIGH);
  talkMIDI(0xB0, 0x07, 126); //0xB0 is channel message, set channel volume to near max (127)
  talkMIDI(0xB0, 0, 0x79); //Default bank GM1
  talkMIDI(0xC0, instrument, 0); //Set instrument number. 0xC0 is a 1 data byte command


void loop() {
for (int i=0; i<pinCount; i++){

  buttonState[i] = digitalRead(inputPins[i]);
   if (buttonState[i] != lastButtonState[i]) {       
    if (buttonState[i] == HIGH) {                      
  lastButtonState[i] = buttonState[i];

//Send a MIDI note-on message.  Like Pressing a piano key
void noteOn(byte channel, byte note, byte attack_velocity) {
  talkMIDI( (0x90 | channel), note, attack_velocity);

//Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
  talkMIDI( (0x80 | 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) {

  //Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes 
  //(sort of:
  if( (cmd & 0xF0) <= 0xB0)


When I play strings like on a keyboard

Can you please explain this an bit more ? How exactly do you use the program to "play strings" ?

I hooked my digital pins with a 5V connector to a piezo switch which then goes to ground. When I click the switch it makes a note like this

I also attached a 10k resistor to the ground.

I still don;t understand how the inputs are wired and what the difference is between playing piano or bells and strings ?

Here is a video I posted on youtube to show you what I am doing

The way you have the switch wired is unusual, Normally the input pin would be held HIGH or LOW by connecting it To 5V or GND through a resistor then you would use the switch to pull the pin to the opposite by wiring it to 5V or GND. I can't see which wires are connected together on the switch but you don't need 2 resistors in the circuit.

In fact, if you set the pin up using

pinMode(pin, INPUT_PULLUP);

you do not need any external resistors at all but you need to check the logic of the program because the pin will be pulled LOW when pressed.

Using the internal resistor is much neater than using external resistors and you don't have to worry about its value.

Interesting, I will check that out and get back with you.