Go Down

Topic: MIDI lib, controlling volume on ch 1 with pot (Read 4164 times) previous topic - next topic

FranPhony

Hello again.

I had no luck with the code for the keyboard above. I have made some progress with this bit of code from youtube

Code: [Select]
#define NUM_ROWS 3
#define NUM_COLS 8

#define NOTE_ON_CMD 0x91
#define NOTE_OFF_CMD 0x81
#define NOTE_VELOCITY 127

//MIDI baud rate
#define SERIAL_RATE 9600

// Pin Definitions

// Row input pins

const int row1Pin = 6;
const int row2Pin = 7;
const int row3Pin = 8;

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

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[3][8] ={
                                        36, 37, 38, 39,
                                        40, 41, 42, 43, 44, 45, 46, 47,
                                        48, 49, 50, 51, 52, 53,  54, 55
                                          };

// bitmasks for scanning columns
int bits[] =
{
  B00000001,
  B00000010,
  B00000100,
  B00001000,
  B00010000,
  B00100000,
  B01000000,
  B10000000
};

void setup()
{
  int note = 36;

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

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

  pinMode(row1Pin, INPUT);
  pinMode(row2Pin, INPUT);
  pinMode(row3Pin, 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);
 

    // 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 <= 7)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //right sr
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //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);
}


My keyboard consists of a 20 button matrix ( 3 x 8 ) and a 29 button matrix ( 4 x 8 ). I have been trying to get the 20 button matrix two work with this code, although my results are less than ideal.  As it is I get only three notes from each ground instead of 4 for ground 1 and 8 for grounds 2 and 3 each. So out of the 20 keys I only get three notes and I can change the value of those notes with the keytomidimap fuction but I can't get nabouring notes to work properly if at all.

Where am I going wrong here?
Also, can anyone tell me whether it is possible to use this bit of code with the midi library!?

FranPhony

I managed to get the keys working with this last bit of code. I then tried to structure it like the ondes sketch so that I can just add them together with minimal hassles. Here is my attempt
Code: [Select]
#include <MIDI.h>
#define NUM_ROWS 3
#define NUM_COLS 8

// Pin Definitions

// Row input pins
const int row1Pin = 6;
const int row2Pin = 7;
const int row3Pin = 8;


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

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS]={                  32, 33, 34, 35,
                                            36, 37, 38, 39, 40, 41, 42, 43,
                                            44, 45, 46, 47, 48, 49, 50, 51,
                                            52, 53, 54, 55,};


// bitmasks for scanning columns
int bits[] =
{
  B00000001,
  B00000010,
  B00000100,
  B00001000,
  B00010000,
  B00100000,
  B01000000,
  B10000000
};

MIDI_CREATE_DEFAULT_INSTANCE();

void SendMIDI (char cmd, char data1, char DATA2) {
  Serial.write(byte (cmd));
  Serial.write(byte (data1));
  Serial.write(byte (DATA2));
}


void keyNotes()
{

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

    }
  }



  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);


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

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

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

  if(0 <= colNum && colNum <= 7)
 
  {
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //right sr

  }
  else
  {
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum-8]); //right sr
  }
  digitalWrite(latchPin, HIGH);
  }
 
void setup() {

  MIDI.begin(1);
  Serial.begin(9600);
    // setup pins output/input mode
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

  pinMode(row1Pin, INPUT);
  pinMode(row2Pin, INPUT);
  pinMode(row3Pin, INPUT);

}

void loop(){
 
  keyNotes ();
 
}


I am getting constants streams of note-ons with note values of 0 - 7. I don't think I'm calling the corresponding key number from 'keyToMidiMap' properly.. I'm out of ideas for today, help would be appreciated

Go Up