Wednesday, May 14, 2008

MIDI With Two 4021s and Arduino

Here is an example of how to read two 4021s and send the state out as MIDI data.
A transition from a low to a high state on the 4021s will result in a note on message.
A transition from a high to a low state on the 4021s will result in a note off message.

Arduino Code:

The Arduino code can be downloaded here:
http://milkcrate.com.au/_other/downloads/projects/MIDI_With_Two_4021s/

Here are some important parts of the code.

byte switchVar1 = 0;
byte switchVar2 = 0;
These are the bytes that contain the information for the 4021 states. Each 4021 has a byte. If more 4021s are to be added, make some extra variable bytes such as switchVar3, switchVar4 and so on.

byte MIDI_channel = 0;
This sets the channel minus one. So for example, setting this to 15 results in sending data on channel 16.

byte MIDI_pitches_offset = 60;
This sets the lowest pitch (so, the note that corresponds to button 0 from the first 4021). The each subsequent button will be one semitone higher.

byte MIDI_on_velocity = 127;
This sets the note on velocity. A high on any of the inputs of a 4021 will trigger a note on event. A low will trigger a note off event.

switchVar1 = shiftIn(dataPin, clockPin);
switchVar2 = shiftIn(dataPin, clockPin);
midiShadows(switchVar1, 0);
midiShadows(switchVar2, 8);
This is where the 4021s are read in and the MIDI data is sent out. To add additional 4021s, simply add additional lines of code like this:

switchVar1 = shiftIn(dataPin, clockPin);
switchVar2 = shiftIn(dataPin, clockPin);
switchVar3 = shiftIn(dataPin, clockPin);
switchVar4 = shiftIn(dataPin, clockPin);
midiShadows(switchVar1, 0);
midiShadows(switchVar2, 8);
midiShadows(switchVar3, 16);
midiShadows(switchVar4, 24);

and so on.

And that's pretty much it. You can connect up to 128 buttons using 16 4021s using this method.

Breadboard

Max/MSP Patch

5 comments:

dontmarryme said...

coolest site ever had....really.
i appreciate that m8.
now i just go on to make my 128 shiftin (if it doesn't work i'll just make it 64. (64 has been done so far)
so if i just follow the tip how to connect 4021, i can have many as i want with just 3 port on arduino board, am i right?

Sebastian Tomczak said...

yes that is correct, however...

1) set the MIDI_pitches_offset byte to 0

(there fore you have 128 different notes, from 0 to 127)

and

2) it will take more time to read all 16 shift ins.

3) you need a midi interface

So i think it will work, dont know how well. Let me know if there are any problems. Get it working with one only...

dontmarryme said...

i will
but now i dont have time to study midi input, yet. however i will do it when i've got my shiftin done.

cheers

Sebastian Tomczak said...

So, just to be sure:

The arduino is sending the MIDI data to max via the MIDI interface.

Good luck!

Unknown said...

Hi Sebastan, great blog, really its SOOOOO useful :D The link you provided for the code with the two 4021's is dead, any chance of a revival?

Thanks so much
Harminder