Diy MIDI Keyboard - Merge incoming MIDI messages

Hi there,

After weeks of searching and struggling, I’ve finally decided to do the sensible thing and ask for help.

I’m try to convert and old Hammond organ into an oversized, ridiculously cumbersome MIDI keyboard.
The actual keyboard part is simple by using the MIDI Controller sketch from notesandvolts.com (attached)
On an Uno R3, I have the 44 organ keys hooked up to 3 multiplexers, there’s a couple of potentiometers on the analog pins, and that works just fine as a super basic (but giant) MIDI keyboard.

What I’m struggling with is reading external MIDI input via a 5din jack on the RX pin and combining that with the MIDI messages created internally by the arduino so they are neatly merged into one on the output. (so if I made say 3 of these units, they could all be stacked or chained)

I’ve fumbled around with other peoples codes but I’m too much of a beginner to really understand how to achieve this.

Any help would be greatly appreciated!
Thanks in advance

(Bonus Issues!
*I would also love to add pitchbend functionality with a self-centering joystick potentiometer
*Octave select with a rotary switch is also something I’d like to add at a later date
HOWEVER main issue is just getting functional MIDI merge working)

Controller.cpp (3.84 KB)

Controller.h (1.64 KB)

MIDI_Controller_v1-2.ino (8.42 KB)

To merge two MIDI streams requires you to parse the incoming stream so that you don't
garble messages when you send your own messages to the common output stream. You need
to maintain a boolean variable indicating which source has control at any point in time, which for
relayed packets needs to be locked until the relayed packet finishes.

I'm sure someone has a library to do this kind of thing already.

Thanks for taking the time to reply.
I’m looking into this now and hoping that someone has already put something like this together. (It seems very unlikely nobody has yet)

Unfortunately I have no idea how to write the code myself, but hopefully these new keywords will point me in the right direction.

Add a call to MIDI.read() in the loop function. The MIDI library includes a software MIDI through feature which is what I think you want. But for the feature to work, MIDI.read() must be called even if the program ignores incoming data. If that does not work, replace MIDI_CHANNEL_OFF with MIDI_CHANNEL_OMNI.

You can only do this if you have two independent serial UARTS each feeding into a different serial buffer. Then it is relatively easy to extract MIDI messages one at a time and merge them. You can not do this with software serial as this only looks at one serial port at a time and so will inevitably miss some incoming messages.

So you need an Arduino like a Mega for this. I doubt if anyone has done a library for this as it is a bit of a rare thing to want to do. Normally each MIDI device would have its own MIDI input port in your DAW.

Hi Mike, I realise it’s probably an odd way of going about it, but the idea is that this organ has 2 rows of keys which I would like to act as 2 separate MIDI devices and then by flipping a passive MIDI switch, have them act as one large keyboard. (attached concept)

I did have it almost working by pasting in fragments of other peoples code (I know, don’t mess with it if you don’t understand it) but I was hoping maybe this was something somebody had done before and had some well written, non-slapped-together code for.

So you’re saying it would be better to have Device A and B both going into a dedicated merger (Mega) rather than trying to merge the internal messages of A with the external serial messages of B?

but the idea is that this organ has 2 rows of keys which I would like to act as 2 separate MIDI devices and then by flipping a passive MIDI switch, have them act as one large keyboard.

That is not the problem that you presented. It is nothing to do with merging your keyboard to an existing MIDI stream as the thread's title says.

If the only source of MIDI is your device and you want that device to send note on / off messages on different channels for different keyboards and then send on the same channel for both keyboards the the code is trivial.

Just have two variables indicating what channel each keyboards should send on. And then depending on the state of the switch make these variables have the same value.

You only need one Arduino for both keyboards.

So you're saying it would be better to have Device A and B both going into a dedicated merger (Mega) rather than trying to merge the internal messages of A with the external serial messages of B?

No, and nothing like the diagram you posted.

The diagram was my original plan. To have the 2 organ keyboards as 2 separate sources of MIDI.
The ability to plug the lower keyboard into say a small synth and the upper keyboard into maybe a MIDI to CV converter, but then also be able to flip the switch and have the lower keyboard run into, and then merge with the messages from the upper keyboard.
I think I explained what I was looking for quite poorly in the beginning.
Sorry the title is a little ambiguous. Basically, I have an Uno creating it's own MIDI stream (a very basic diy keyboard) and I would like to plug a second MIDI source into it's input and have it merge the new incoming messages with it's own and output them both.

I've seen a few MIDI sequencers and controllers based on the Uno that can achieve this result but I have no idea how to implement that function myself being a complete beginner

Basically, I have an Uno creating it's own MIDI stream (a very basic diy keyboard) and I would like to plug a second MIDI source into it's input and have it merge the new incoming messages with it's own and output them both.

Well as you have the need for only one input and one output you can do this with with just a single MIDI interface.

but I have no idea how to implement that function myself being a complete beginner

You start by simply writing code that looks at the input and transfers any MIDI message you find to the output. Then simply add your keyboard scan and MIDI message generator to that.

Basically, I have an Uno creating it's own MIDI stream (a very basic diy keyboard) and I would like to plug a second MIDI source into it's input and have it merge the new incoming messages with it's own and output them both.

This describes the software MIDI through feature of the MIDI library by Forty Seven Effects.

Also see my previous message.