// initialisation byte data; // working variable for incoming digital reads byte kp[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // array to keep track of previous states, in order to measure transitions and filter out repeating data byte channel = 0; // this variable sets the MIDI channel minus one (ie. set this number to 0 if you want to send data on MIDI channel 1) byte pitches[[] = { 60, 61, 62, 63, 64, 65, 66, 67, 68, 69 70, 71 }; // array to hold the outgoing MIDI pitches - the first number is equal to the first keypad button, the second to the second and so on byte velocity; // set the velocity for a note-on event when a button is pushed // setup void setup() { Serial.begin(32150); // set MIDI baud rate pinMode(2, OUTPUT); // Connect to KP ROW 1 pin pinMode(3, OUTPUT); // Connect to KP ROW 2 pin pinMode(4, OUTPUT); // Connect to KP ROW 3 pin pinMode(5, OUTPUT); // Connect to KP ROW 4 pin pinMode(6, INPUT); // Connect to KP COL 1 pin pinMode(7, INPUT); // Connect to KP COL 2 pin pinMode(8, INPUT); // Connect to KP COL 3 pin // The input pins (arduino digital i/o pins 6, 7 and 8) will need to be // pulled down to ground using a resistor each // The value of the resistor should be 10k½ - 30k½ or so. // This stops the digital input pins from floating } // main void loop() { // set up a looping function, which loops 12 times (one for each button) for(byte i = 0; i < 12; i ++) { // set the row pin high digitalWrite((i / 3) + 2, HIGH); // a short delay delayMicroseconds(75); // read the column pin into the variable data data = digitalRead((i % 3) + 6); // if the state of that particular button has changed since last time, send a MIDI note if(data != kp[i]) { // set the byte in the array to the new data value kp[i] = data; // send a MIDI status byte to indicate a note-on event printByte(0x90 + channel); // send a MIDI data byte with the MIDI note number retrieved from the pitch array printByte(pitches[i]); // send a MIDI data byte with the velocity. the data point in the array will either be 1 or 0 // so we either send a note-on (velocity > 0) or a note-off (velocity = 0) printByte(kp[i] * 127); // a short delay delay(2); // end if function } // set the row pin low, ready for the next loop digitalWrite((i / 3) + 2, LOW); // a short delay delayMicroseconds(75); // end the looping function } // and loop the loop forever }