I’ve now mixed the single button MIDI code with the I2C code to create a Multi Button MIDI. This is just the starting point but it works perfectly. Using the same board layout as the earlier post, upload this sketch and you’ll have sixteen inputs for your MIDI device, perfect for wiring into your Bass Pedal!
// MultiButtonMIDI.ino
// Driving MIDI using a Multiple Buttons
// Rob Ives 2012
// This code is released into the Public Domain.
#include <MIDI.h>
#include <Wire.h>
int keyispressed[16]; //Is the key currently pressed?
int noteisplaying[16]; //Is the Note currently playing?
unsigned char data1; //data from chip 1
unsigned char data2; //data from chip 2
void setup() //The Setup Loop
{
Wire.begin(); // setup the I2C bus
for (unsigned int i = 0; i < 16; i++) { //Init variables
keyispressed[i] = 1; //clear the keys array (High is off)
noteisplaying[i] = 0; //no notes are playing
}
MIDI.begin(); //initialise midi library
}
//---------------------------------------------
void loop() //the main loop
{
readkeys();
sendMIDI();
}
//-------------------------------------
void readkeys() { //Read the state of the I2C chips. 1 is open, 0 is closed.
Wire.requestFrom(0x38, 1); // read the data from chip 1 in data1
if (Wire.available()){
data1 = Wire.read();
}
Wire.requestFrom(0x39, 1); // read the data freom chip 2 into data2
if (Wire.available()){
data2 = Wire.read();
}
for (unsigned char i = 0; i < 8; i++) {// puts data bits from chip 1 into keys array
keyispressed[i] = ((data1 >> i) & 1); // set the key variable to the current state. chip 1
keyispressed[i + 8] = ((data2 >> i) & 1); //chip 2
}
}
//-------------------------------------
void sendMIDI() { // Send MIDI instructions via the MIDI out
for (unsigned char i = 0; i < 16; i++) { //for each note in the array
if (keyispressed[i] == LOW){ //the key on the board is pressed
if(!noteisplaying[i]){ //if the note is not already playing send MIDI instruction to start the note
MIDI.sendNoteOn(36+i,127,1); // Send a Note ( vel.127 ch. 1)
noteisplaying[i] = 1; // set the note playing flag to TRUE
}
}
else{
if(noteisplaying[i]){ //if the note is currently playing, turn it off
MIDI.sendNoteOff(36+i,0,1); // Stop the note
noteisplaying[i] = 0; // clear the note is playing flag
}
}
}
}
This is just the start. My next step will be to add a volume control and an octave switch.
Possible plan for octave switch: Two foot buttons, one for up, one for down, Four LEDs showing which octave is currently selected.