Trying to code a simple midi keypad matrix

Hi, all.

This is my first post, so hopefully this is quick and easy.
I built an arcade midi controller with 4 pot's, and I can't seem to marry any code and get it to work.
I can't get it to read the matrix other than for a keyboard.

I built a simple 8 button midi with the sample code, and that was ok.. I just wanted to prove the concept I guess.

Anywho, the matrix is wired as per with diodes across every switch. leaving me the usual 8 pins. 8 pins!!! Ahhhh

I've searched the internet high and low to no avail.

Please can someone help??

Much appreciated

I'm scrubbing what I said about analog.read()... I just looked at the picture and saw the matrix you wired. I do not know.

Hello k1rkj

Take a view to get some ideas.
https://www.instructables.com/howto/keypad+arduino/
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

1 Like

Thanks for the replies so far guys. I'm scouring through Instructables, and still can't find anything relating to midi and a keypad.

I'll be honest, it's very much a copy and paste job for me as I'm very new to coding.

Like I mentioned in the post, I can get it to read as a keypad, but not as a midi device too!

Here's the example of what I'm using;

#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS  7

const uint8_t button1 = 2;

const uint8_t button2 = 3;

const uint8_t button3 = 4;

const uint8_t button4 = 5;

const uint8_t button5 = 6;

const uint8_t button6 = 7;

const uint8_t button7 = 8;

const int intensityPot = 0;  //A0 input

const uint8_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7};

const byte notePitches[NUM_BUTTONS] = {pitchC3, pitchD3, pitchE3, pitchF3, pitchG3, pitchA3, pitchB3};

uint8_t notesTime[NUM_BUTTONS];

uint8_t pressedButtons = 0x00;

uint8_t previousButtons = 0x00;

uint8_t intensity;

void setup() {

  for (int i = 0; i < NUM_BUTTONS; i++)

    pinMode(buttons[i], INPUT_PULLUP);
}

void loop() {

  readButtons();

  readIntensity();

  playNotes();
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {

  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};

  MidiUSB.sendMIDI(event);
}

void readButtons()
{

  for (int i = 0; i < NUM_BUTTONS; i++)

  {

    if (digitalRead(buttons[i]) == LOW)

    {

      bitWrite(pressedButtons, i, 1);

      delay(50);

    }

    else

      bitWrite(pressedButtons, i, 0);

  }
}

void readIntensity()
{

  int val = analogRead(intensityPot);

  intensity = (uint8_t) (map(val, 0, 1023, 0, 127));
}

void playNotes()
{

  for (int i = 0; i < NUM_BUTTONS; i++)

  {

    if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))

    {

      if (bitRead(pressedButtons, i))

      {

        bitWrite(previousButtons, i , 1);

        noteOn(0, notePitches[i], intensity);

        MidiUSB.flush();

      }

      else

      {

        bitWrite(previousButtons, i , 0);

        noteOff(0, notePitches[i], 0);

        MidiUSB.flush();

      }

    }

  }
}

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {

  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};

  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {

  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};

  MidiUSB.sendMIDI(noteOff);
}

I know I can't use this code with a matrix, but would like to use the midi part of it with the 16 pad matrix I put together... Is that possible?

I also want to add some potentiometers to the board for intensity, resonance and so forth.

Again, any help is much appreciated

If you can read your matrix "as a keypad" that presumably means you get a different output for each key pressed. So isn't all you need to do to map those 16 'keys' to the 16 MIDI output notes you want?

It's not much more than changing void readbuttons() to read the keypad instead, though that's assuming you understand how that code that you posted works.

Steve

Ok so now trying to refer to this...
For the record I'm a total noob!!

#include <MIDIUSB.h>
#include <Keypad.h>
#include "PitchToNote.h"
#define customKeypad


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'0','1','2','3'},
  {'4','5','6','7'},
  {'8','9','A','B'},
  {'C','D','E','F'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad


const int intensityPot = 0;  //A0 input
uint8_t intensity;

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 






void setup() {

  for (int i = 0; i < Keypad; i++)

    pinMode(Keypad[i], INPUT_PULLUP);
}

void loop() {

  readKeypad();

  readIntensity();

  playNotes();
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {

  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};

  MidiUSB.sendMIDI(event);
}

void readButtons()
{

  for (int i = 0; i < Keypad; i++)

  {

    if (digitalRead(Keypad[i]) == LOW)

    {

      bitWrite(pressedButtons, i, 1);

      delay(50);

    }

    else

      bitWrite(pressedButtons, i, 0);

  }
}

void readIntensity()
{

  int val = analogRead(intensityPot);

  intensity = (uint8_t) (map(val, 0, 1023, 0, 127));
}

void playNotes()
{

  for (int i = 0; i < Keypad; i++);

  {

    if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))

    {

      if (bitRead(pressedButtons, i))

      {

        bitWrite(previousButtons, i , 1);

        noteOn(0, notePitches[i], intensity);

        MidiUSB.flush();

      }

      else

      {

        bitWrite(previousButtons, i , 0);

        noteOff(0, notePitches[i], 0);

        MidiUSB.flush();

      }

    }

  }
}

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {

  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};

  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {

  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};

  MidiUSB.sendMIDI(noteOff);
}

Hello
In this case run some tutorials to the hardware to be used.

1 Like

When I said "noob" I meant fairly new. I'm at the point where I'm altering code, but can't seem to find any solid info on this??

Here's where I'm up to..

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'A','B','C','D'},
  {'E','F','G','H'},
  {'I','J','K','L'},
  {'M','N','O','P'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 6; 

boolean blink = false;
boolean ledPin_state;

void setup(){
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);              // Sets the digital pin as output.
    digitalWrite(ledPin, HIGH);           // Turn the LED on.
    ledPin_state = digitalRead(ledPin);   // Store initial LED state. HIGH when LED is on.
    keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}

void loop(){
    char key = keypad.getKey();

    if (key) {
        Serial.println(key);
    }
    if (blink){
        digitalWrite(ledPin,!digitalRead(ledPin));    // Change the ledPin from Hi2Lo or Lo2Hi.
        delay(100);
    }
}

// Taking care of some special events.
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
    case PRESSED:
        if (key == '#') {
            digitalWrite(ledPin,!digitalRead(ledPin));
            ledPin_state = digitalRead(ledPin);        // Remember LED state, lit or unlit.
        }
        break;

    case RELEASED:
        if (key == '*') {
            digitalWrite(ledPin,ledPin_state);    // Restore LED state from before it started blinking.
            blink = false;
        }
        break;

    case HOLD:
        if (key == '*') {
            blink = true;    // Blink the LED when holding the * key.
        }
        break;
    }
}

You may be a noob, but you can read, so read the forum guide in the sticky post. Never post images of code or error messages. Always post the text of code, error messages or serial monitor output in your post between code tags.

The error is telling you there is no such thing as "Keypad" in this sketch. Nothing of that name is declared.

In this case you don't need to initialise the pins used for the keypad because the keypad library will do this for you. So you can remove that for-loop from setup().

1 Like

okies. Thanks
So clearly I'm stuck :rofl:

If anyone has anything, that'd be awesome :+1:

Info on what exactly?

Ok, some different code. Does it work?

Hi, Thanks for responding.
It does work, but because it incorporates button presses in the midi I can only presume the triggering side of it is done with the MIDI code etc. I simply just want to be able to code it as a midi matrix with some pot's!
At the minute I can get it working as keyboard but not correctly with MIDI.

Cheers,
K

Good luck with that, because I found your description of the problem vague and confusing.

I can't see anything in the code posted that is making any attempt to do anything with MIDI. And just saying you want it "as a MIDI keypad" isn't very informative. What EXACTLY should happen when a key on the keypad is pressed? Send NoteOn, send CC or what?

Steve

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.