MIDI keyboard encoder

I just successfully tested code and breadboard prototype for a MIDI keyboard encoder. It utilizes 8 74LS151Ns so the switches it’s scanning can be parallel-wired (opposed to a matrix). It scans 64 switches, although my design intent is for 61-note organ manuals. As of this post, I have only tested by grounding the inputs on each 74151.

I am utilizing the 3.0.1 MIDI library, you can find the link to the most current version at: http://www.arduino.cc/playground/Main/MIDILibrary. The 74151 scanning logic I pulled from the playground at http://arduino.cc/playground/Code/MUX151. I also pulled ideas (but no copy/pasting) from chloris’s Yet Another Arduino MIDI Drum Kit thread in the Interactive Art subforum http://arduino.cc/forum/index.php/topic,54927.0.html Big thanks to everyone who coded those, I am a total noob at C++ and your sketches helped me make this work.

I do not have a schematic drawn up for the external hardware. It is basically the 8 multiplexers wired to pins 6 through 13, control pins 2-5, and serial TX wired to a DIN jack through a 220 ohm resistor, ala MIDI standards.

#include <Compatibility_v2.5.h>
#include <MIDI.h>

//mux pins
#define mux8 13  // digital inputs to arduino from mux
#define mux7 12
#define mux6 11
#define mux5 10
#define mux4 9
#define mux3 8
#define mux2 7
#define mux1 6
#define strobe 5   //digital outputs to control mux  
#define cee 4
#define bee 3
#define aye 2

//array for played notes
volatile int note[64] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,
                LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,};
//array for scanned switch state                
volatile int swst[64];

//arrays to control 8 mux inputs
int c_bin[]={LOW,LOW,LOW,LOW,HIGH,HIGH,HIGH,HIGH};
int b_bin[]={LOW,LOW,HIGH,HIGH,LOW,LOW,HIGH,HIGH};
int a_bin[]={LOW,HIGH,LOW,HIGH,LOW,HIGH,LOW,HIGH};

//extra variables
int select=0;
int a_val=0;
int b_val=0;
int c_val=0;

void setup() {

  MIDI.begin();
  pinMode(mux1, INPUT);
  pinMode(mux2, INPUT);
  pinMode(mux3, INPUT);
  pinMode(mux4, INPUT);
  pinMode(mux5, INPUT);
  pinMode(mux6, INPUT);
  pinMode(mux7, INPUT);
  pinMode(mux8, INPUT);
  pinMode(strobe, OUTPUT);
  pinMode(cee, OUTPUT);
  pinMode(bee, OUTPUT);
  pinMode(aye, OUTPUT);
}

void loop() {
  scanning ();
  notewrite ();
}

void scanning (){

  for(select=0;select<=7;select++) {

    //select mux input
    a_val=a_bin[select];
    b_val=b_bin[select];
    c_val=c_bin[select];

    digitalWrite(aye,a_val);
    digitalWrite(bee,b_val);
    digitalWrite(cee,c_val);

    //strobe LOW to read
    digitalWrite(strobe,LOW);

    //read mux and store switch state to memory
    swst[select] = digitalRead(mux1);
    swst[select + 8] = digitalRead(mux2);
    swst[select + 16] = digitalRead(mux3);
    swst[select + 24] = digitalRead(mux4);
    swst[select + 32] = digitalRead(mux5);
    swst[select + 40] = digitalRead(mux6);
    swst[select + 48] = digitalRead(mux7);
    swst[select + 56] = digitalRead(mux8);

    //strobe HIGH to avoid jitters
    digitalWrite(strobe,HIGH);
  }
}

void notewrite () {
  for (int i=0;i<=64;i++) {
    if (note[i] != swst[i]) { //detects switch that has changed state

      if (swst[i] == LOW) {
      MIDI.sendNoteOn (i+36,0,1);} //zero velocity aka note off

      else if (swst[i] == HIGH) {
      MIDI.sendNoteOn (i+36,100,1);} //100 velocity note on

      note[i] = swst[i]; //sent note written to array
    }
  }
}

Please have a look and give me some crap for my noob code :slight_smile:

I plan in the future to implement MIDI THRU, or possibly send MIDI up the USB jack. If anyone knows more about the MIDI library, I would love to hear from you, I don’t know how to code the MIDI IN/THRU :blush:

Could you please check my topic: http://arduino.cc/forum/index.php?topic=116324.new%3btopicseen#new I'm having trouble building my MIDI keyboard encoder. I wouldn't mind if I also use multiplexer IC's and have less piano keys.

give me some crap for my noob code

OK, if you insist :slight_smile:

int note[64]

The values for this array (and a_bin, b_bin, c_bin) can only be HIGH or LOW so you can use “char note[64]” instead and save a bit of sram, although at the moment your sketch isn’t big enough to worry about it.

But your “for” loop which is used to index the note array isn’t quite right. It should be:

for (int i=0;i<64;i++) {

Your code would access note[64] which doesn’t exist. There are 64 items in the array but they are numbered 0 to 63.

There are other things I’d write differently as well but that’ll do for now.

Pete