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 setup the Paino strings in the code the sound continues indefinitely. My guess is the delay function, but your input will be really appreciated.
//Music Instrument Shield_Keyboard renew
//MusicIntrument_Keyboard
// 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
mySerial.begin(31250);
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);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
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]);
delay(1);
if (buttonState[i] != lastButtonState[i]) {
if (buttonState[i] == HIGH) {
noteOn(0,note[i],100);
}
}
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) {
mySerial.write(cmd);
mySerial.write(data1);
//Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
//(sort of: http://253.ccarh.org/handout/midiprotocol/)
if( (cmd & 0xF0) <= 0xB0)
mySerial.write(data2);
}
Here is my video on youtube of the problem I had
Other examples from other people