1

I'm trying to replace this code with something that uses the Arduino MIDI library:

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 1

SoftwareSerial midiSerial (rxPin,txPin);

void setup() {
  midiSerial.begin(31250); }

while ( midiSerial.available()) {
    incomingCommand = midiSerial.read();
    incomingNote = midiSerial.read();
    incomingVelocity = midiSerial.read()*8.05511811024; 
  if (incomingVelocity <=1023) {
    midiSerial.write(incomingCommand);
    midiSerial.write(incomingNote);
    midiSerial.write(incomingVelocity); } }

Here's what I came up with:

#include <MIDI.h> // MIDI Output
MIDI_CREATE_DEFAULT_INSTANCE();

byte MIDIChannel;
byte programByte1;
byte controlByte1;
byte controlByte2;

void setup() {
  MIDI.begin();
}

void loop() {
 if (MIDI.read()) switch (MIDI.getType()) {
      MIDIChannel = MIDI.getChannel();
    case midi::ProgramChange:
      programByte1 = MIDI.getData1();
      break;
    case midi::ControlChange:
      controlByte1 = MIDI.getData1();
      controlByte2 = MIDI.getData2();
      break; 
    default: break; 
    }
}

Does that look correct? It doesn't function as smoothly as the version that uses SoftwareSerial

6
  • what exactly is your question?
    – jsotola
    Jun 24, 2021 at 1:43
  • "Does that look correct?" It doesn't seem to take in any MIDI input data Jun 24, 2021 at 1:54
  • how do you know this?
    – jsotola
    Jun 24, 2021 at 1:56
  • I've updated the code above and added the rest of the commands to my switch-case setup, so I'll keep searching for the data I'm looking for, which is specifically the 'command' data. Should be straightforward with some digging Jun 24, 2021 at 6:10
  • 1
    The second piece of code doesn't do anything useful. I think it will not even compile (instruction after switch without case)
    – PMF
    Jun 24, 2021 at 11:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.