First Draft of Code

First draft of the code to convert keyboard input to midi. There is a bug somewhere, the midi sounds continuously with new notes added but not switched off.

// Convert thirteen input wires from bass pedal into midi output
// Rob Ives 2012
// This code is realeased into the Public Domain.

unsigned char data1; //data from chip 1
unsigned char data2; //data from chip 2
unsigned char keys[16]; //storage array for key states.
byte notevalue = 0; //lowest note on the bass board

void setup() // the setup loop

  Wire.begin(); // setup the I2C bus
  Serial.begin(9600); // serial set up for midi
  for (unsigned int i = 0; i < 16; i++) { //clear the keys array
    keys[i] = 1;
// ----------------------------------------------------------------
void loop() // the main loop
  byte velocity; //note velocity
  velocity = 90; //initially set at 90 - add volume control later
  Wire.requestFrom(0x38, 1); // read the data from chip 1 into data1
  if (Wire.available()){
     data1 =; 

  Wire.requestFrom(0x39, 1); // read the data freom chip 2 into data2
  if (Wire.available()){
     data2 =;

  for (unsigned char i = 0; i < 8; i++) {// puts data bits from chip 1 into keys array     if (keys[i] != ((data1 >> i) & 1)){ // edge detect. If the key state has changed.
      if (keys[i] == 1){// if the key was on send an Start Note midi command
        playnote(0x90, notevalue + i, velocity); // channel 1 midi on
      else{ // send a end note midi command
        playnote(128, notevalue + i, 0); // channel 1 midi off
    keys[i] = ((data1 >> i) & 1); // set the key variable to the current state.

 /* for (unsigned char i = 0; i < 8; i++) {// puts data bits from chip 2 into keys array     if (keys[i+8] != ((data2 >> i) & 1)){ // edge detect. If the key state has changed.
      if (keys[i+8] == 1){// if the key was on send an start Note midi command
          //Serial.println(" Midi On");
          Serial.print(0x80); // channel 1 midi on
          Serial.print(notevalue+i+8); //note value
          Serial.println(velocity); // note velocity
      else{ // send a end note midi command
        //Serial.println(" Midi Off");
        Serial.write(0x90); // channel 1 midi off
        Serial.write(notevalue+i+8); //note value
        Serial.write(velocity); // note velocity
    keys[i + 8] = ((data2 >> i) & 1);
  } */

  /* for (unsigned char i = 0; i < 16; i++) {
  Serial.println("-"); */

//play note
void playnote(byte cmd, byte data1, byte data2) {
   //--------------- debug
   Serial.print(" | ");
   Serial.print(" | ");

Cabling up the Keys

A small step today. I’ve removed the old bundle of cable and replaced it with a ribbon cable. Its a twenty strand flat ribbon, I’ve used thirteen strands for the keys and one for ground leaving six wires unused. I’ve left them as part of the cable on the grounds that they might come in for something later on.

Electronics – First Steps

The pedal board is ready with all the parts cleaned and screws tightened. I have a bundle of thirteen wires, one for each pedal. I’m planning to convert the input from these wires into a midi output. I’m using the Arduino Uno board to convert between the two. Rather than clogging up all the arduino inputs with wires I’m using a pair of PCF8574A port expander chips to encode the keyboard inputs.

The white wires in the above diagram are all inputs, sixteen of them, three spare. the diagram above was prepared using Fritzing, a fabulous open source tool for circuit design.

Here’s the result ┬áset up on a breadboard.

I’ll be testing this out with a few jump leads before wiring in the keyboard wires. Code next. Meanwhile, I’m thinking of replacing the bundle of cable with ribbon cable which would be easier to connect to the completed circuit board.