Help- Musical instrument shield not playing correct sound

Hello, I’ve had quite a tough time debugging my codes to output the correct sounds from my Musical Instrument shield vs1053 with Arduino (BLE 4.0 - Bluno). Basically, i’m doing a project that requires a MIDI controller app on iPad (piano Keyboard) to send MIDI messages (in bytes) to an Arduino via bluetooth to output sounds from the musical instrument shield attached to it. Everything seems to work fine between getting the iPad app to connect to the microcontroller via bluetooth. However, i am having troubles with outputting the the right sounds from the MIDI messages sent. I’m quite new to arduino codings and having used alot of sample codes to get mine right, the musical instrument plays the correct sounds but ONLY from the Serial Monitor of the IDE. I know i’m missing somethings out but not sure where.

Below are my codes:

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

    byte note = 0; //The MIDI note value to be played
    byte resetMIDI = 4; //Tied to VS1053 Reset line
    byte ledPin = 13; //MIDI traffic inidicator
    int instrument = 0;
    
    void setup() {
    Serial.begin(57600);//Serial.begin(31250);

    //Setup soft serial for MIDI control
    mySerial.begin(31250);
  
    //Reset the VS1053
    pinMode(resetMIDI, OUTPUT);
    digitalWrite(resetMIDI, LOW);
    delay(100);
    digitalWrite(resetMIDI, HIGH);
    delay(100);

    //talkMIDI(0xB0, 0x07, 127); //0xB0 is channel message, set channel volume to near max     (127)
  
  
    Serial.println("Press a key on your app");
    }

    void loop() {
    //For this bank 0x78, the instrument does not matter, only the note
    for(instrument = 3 ; instrument < 4 ; instrument++) {

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

    
    talkMIDI(0xB0, 0, 0x79); //Select the bank of melodic sounds
    talkMIDI(0xC0, instrument, 0); //Set instrument number. 0xC0 is a 1 data byte command
  
    while(Serial.available() == 0) {}

    int note = Serial.read(); // reads the notes from the iPad
    //int i=24;
    //byte note[]={48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71};
 
     Serial.print("Note=");
    Serial.println(note);
  
    //For this bank 0x78, the instrument does not matter, only the note
    noteOn(0, note, 100);
    delay(1);
    //noteOff(0, note, 100);
    //delay(10);

    //Note will dissapate automatically
      while (mySerial.available() > 0) {
      Serial.write(mySerial.read());
     
      }
     }
   
    }
   
    //channel ranges from 0-15
    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) {
    digitalWrite(ledPin, HIGH);
    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);

    digitalWrite(ledPin, LOW);
    }

Anytime i try to play the keyboard normally while connected to the arduino, it plays only a single note, for all keys pressed, in gibberish. I want to be able to play a note between 48 and 71 as my app is of two octaves, without going to serial monitor.

Any help will be appreciated.

akay001:
I’m quite new to arduino codings and having used alot of sample codes to get mine right, the musical instrument plays the correct sounds but ONLY from the Serial Monitor of the IDE. I know i’m missing somethings out but not sure where.

I haven't looked at the code but just from the above I suspect there's some ASCII oddness going on. Are the values for the variable note[] decimal or hex ? Are they supposed to be the ASCII for 0 - 9 (and more) ?

http://www.asciitable.com/

When you type a “0” on the keyboard, what’s actually sent over the serial link is a 48d (0x30). The code at the receiving end has to decode the ASCII to get the 0 back. My guess is that this is done in the Arduino code and so works when used w/the serial monitor. But your BT connection isn’t ASCII encoding the data sent and so the decoding at the receiving end is wrong for this usage.

Thanks Mee_n_Mac. I think i understand what you mean. In my software code, i declared the notes to be sent as an Unsigned integer (const UInt8), perhaps that could be the reason. Also for my BT connection, i used:

NSData * controlData = [NSData dataWithBytes:(void*)&noteOn length:sizeof(noteOn)];
[self.peripheral writeValue:controlData forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];

I don’t know if the problem could be in this above code.

Thanks

What do you press on the keyboard when it works via the serial monitor, the 0-9 number keys ? What does the serial monitor say (print) the note value as ? When you do it via the iPad, what does the serial monitor say ?

I don’t use the keyboard, however, when i do it via the ipad, the serial monitor shows numbers 48 up to 71, signifying the two octave piano app on my ipad where middle c is 60. But without using the serial monitor, the shield produces a single note regardless of what key is pressed on the ipad in a weird manner.

I don’t know if it has to do with my declarations in Xcode for ios.