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.