Problem with coding for my arduino midi piano from an old piano. Help please.

  • Scan matrix is on image below.
  • 5th wire is only work for the 1st note C shown on image below so that I wanted to make it read 1 time only but don’t know how.
  • note C and note F# only not working properly, they are upside down.
  • If I using 2 shift register, it will be more wrong note.

Help me and Thank you.

#define NUM_ROWS 6
#define NUM_COLS 9

#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127

//MIDI baud rate
#define SERIAL_RATE 31250

// Pin Definitions

// Row input pins
const int row1Pin = 7;
const int row2Pin = 2;
const int row3Pin = 3;
const int row4Pin = 4;
const int row5Pin = 5;
const int row6Pin = 6;

// 74HC595 pins
const int dataPin = 8;
const int latchPin = 9;
const int clockPin = 10;

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS];

// bitmasks for scanning columns
int bits[] =
{ 
  B11111110,
  B11111101,
  B11111011,
  B11110111,
  B11101111,
  B11011111,
  B10111111,
  B01111111
};

void setup()
{
  int note = 38;

  for(int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    for(int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    {
      keyPressed[rowCtr][colCtr] = false;
      keyToMidiMap[rowCtr][colCtr] = note;
      note++;
    }
  }

  // setup pins output/input mode
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

  pinMode(row1Pin, INPUT);
  pinMode(row2Pin, INPUT);
  pinMode(row3Pin, INPUT);
  pinMode(row4Pin, INPUT);
  pinMode(row5Pin, INPUT);
  pinMode(row6Pin, INPUT);

  Serial.begin(SERIAL_RATE);
}

void loop()
{
  for (int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    //scan next column
    scanColumn(colCtr);

    //get row values at this column
    int rowValue[NUM_ROWS];
    rowValue[0] = !digitalRead(row1Pin);
    rowValue[1] = !digitalRead(row2Pin);
    rowValue[2] = !digitalRead(row3Pin);
    rowValue[3] = !digitalRead(row4Pin);
    rowValue[4] = !digitalRead(row5Pin);
    rowValue[5] = !digitalRead(row6Pin);

    // process keys pressed
    for(int rowCtr=0; rowCtr<NUM_ROWS; ++rowCtr)
    {
      if(rowValue[rowCtr] != 0 && !keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = true;
        noteOn(rowCtr,colCtr);
      }
    }

    // process keys released
    for(int rowCtr=0; rowCtr<NUM_ROWS; ++rowCtr)
    {
      if(rowValue[rowCtr] == 0 && keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = false;
        noteOff(rowCtr,colCtr);
      }
    }
  }
}

void scanColumn(int colNum)
{
  digitalWrite(latchPin, LOW);

  if(0 <= colNum && colNum <= 9)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, B11111111); //right sr
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
  }
  else
  {
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum-8]); //right sr
    shiftOut(dataPin, clockPin, MSBFIRST, B11111111); //left sr
  }
  digitalWrite(latchPin, HIGH);
}

void noteOn(int row, int col)
{
  Serial.write(NOTE_ON_CMD);
  Serial.write(keyToMidiMap[row][col]);
  Serial.write(NOTE_VELOCITY);
}

void noteOff(int row, int col)
{
  Serial.write(NOTE_OFF_CMD);
  Serial.write(keyToMidiMap[row][col]);
  Serial.write(NOTE_VELOCITY);
}

hisakiknight,

  • it could be your code
  • it could be your electronics
  • this is too complex for one question

Try one note at a time, then we might be able to help.

Jesse

jessemonroy650,

First of all look at the image below(shift register), I got the Yamaha psr 125 piano with the scan matrix of (6 row x 9 column). In the 9 column got one 5th wire which only use to scan for one C note, therefore I need to make the code to scan 5th x 15th only once so it won't loop back to scan 5thx10th,11th... and scan other start over with the 1st, 3rd... wires.

I learn from here http://www.instructables.com/id/Add-MIDI-port-to-Keyboard/

Sorry for my weak explanation.

Image here: Dropbox - 29 December 2014 135812 GMT+0800.png - Simplify your life

hisakiknight:
jessemonroy650,

First of all look at the image below(shift register), I got the Yamaha psr 125 piano with the scan matrix of (6 row x 9 column). In the 9 column got one 5th wire which only use to scan for one C note, therefore I need to make the code to scan 5th x 15th only once so it won't loop back to scan 5thx10th,11th... and scan other start over with the 1st, 3rd... wires.

::::

hisakiknight,
I think you make my point with this explanation. The comments you add here SHOULD BE in the code. As you add comments, it's quite likely you will find the answer you are looking for.

In short, DEBUGGING is a highly personal task. You'll find answers when you

  • Explain it to yourself
  • Explain it to other people
  • Someone finds a typo
  • Someone explains something

It's generally like that. So as you do this one note at a time, you'll understand it better, which means you'll explain it better - smaller bugs will be discovered, and the next thing you know -- IT WORKS.

Do one small piece; explain it to us. We'll make it work. :slight_smile:

Jesse

I found the way to complete it just not perfect, by the way thanks you to help me. :slight_smile: