MIDI KEYBOARD WITH MIDIUSB.h?

Hi!

First of all sorry for my english and my noobie knowledge about coding (its my first time posting here so any comment or correction would help).

Im trying to do a midi keyboard with an old broken piano. It has a 8x8 matrix which i make it work with this code and an Arduino Uno (from another project, obviously):

#include <Keypad.h> // Cargarmos libreria "Keypad".
#include <MIDI.h>   // Cargarmos libreria "MIDI Library".

MIDI_CREATE_DEFAULT_INSTANCE();

const byte ROWS = 6; //base 6 Filas (Do,Fa#)(Do#,Sol)(Re,Sol#)(Re#,La)(Mi,La#)(Fa,Si)
const byte COLS = 11; //x 5 columnas (Octava_1a)(Octava_1b)(Octava_2a)(Octava_2b)(Octava_3a)

byte keys[ROWS][COLS];

byte rowPins[ROWS] = {14, 15, 16, 17, 18, 19}; //A0, A1, A2, A3, A4, A5
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //Un Pin por cada 6 tonos

byte tecla_presionada;

int nota = 36;// nota inicial

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

String msg = "";

void setup() {
    MIDI.begin(); //iniciamos transmicion Midi.
    Serial.begin(115200);

  //llena matriz de teclas con los valores de las notas
    for (int c=0; c<COLS; c++){
      for (int r=0; r<ROWS; r++){
        keys[r][c]=nota;
        nota++;
      }
    }   
}

void loop() {
  //busca teclas precionadas (hasta 10 al mismo tiempo)
    if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   // Se escanea toda la Matriz.
        {
            if ( kpd.key[i].stateChanged )   // Si cambia de estado una tecla.
            {
                switch (kpd.key[i].kstate) {  // Pulsado or Soltado
                    case PRESSED:
                    msg = " PRESSED.";
                    tecla_presionada=kpd.key[i].kchar;
                    MIDI.sendNoteOn(tecla_presionada, 80, 1);
                break;
                    case RELEASED:
                    msg = " RELEASED.";
                    tecla_presionada=kpd.key[i].kchar;
                    MIDI.sendNoteOff(tecla_presionada, 20, 1);
                }
            }
        }
    }
}  // End loop

It worked very well but now I would like to take this code and transform it (I dont know how to say it) to MIDIUSB.h library to use it natively with my Arduino Leonardo and mac.
Actually I tried looking for different ways around the internet but none of them had work for me (asking to ChatGPT also). Every code gives me error or the board makes nothing.

This is an example of one of the codes that the AI gave me:

#include <MIDIUSB.h>
#include <Keypad.h>

const byte ROWS = 8;
const byte COLS = 8;  // Cambiado a 8 columnas
char keys[ROWS][COLS] = {
  {36, 37, 38, 39, 40, 41, 42, 43},
  {44, 45, 46, 47, 48, 49, 50, 51},
  {52, 53, 54, 55, 56, 57, 58, 59},
  {60, 61, 62, 63, 64, 65, 66, 67},
  {68, 69, 70, 71, 72, 73, 74, 75},
  {76, 77, 78, 79, 80, 81, 82, 83},
  {84, 85, 86, 87, 88, 89, 90, 91},
  {92, 93, 94, 95, 96, 97, 98, 99}
};
byte rowPins[ROWS] = {A2, A3, A4, A5, 10, 11, 12, 13};
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9};  // Añadida una columna

Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (kpd.getKeys()) {
    for (int i = 0; i < LIST_MAX; i++) {
      if (kpd.key[i].stateChanged) {
        uint8_t tecla_presionada = static_cast<uint8_t>(kpd.key[i].kchar); // Convertir a uint8_t
        switch (kpd.key[i].kstate) {
          case PRESSED: {
            midiEventPacket_t noteOn = {0x09, 0x90 | 1, tecla_presionada, 80};
            MidiUSB.sendMIDI(noteOn);
            break;
          }
          case RELEASED: {
            midiEventPacket_t noteOff = {0x08, 0x80 | 1, tecla_presionada, 0};
            MidiUSB.sendMIDI(noteOff);
            break;
          }
        }
      }
    }
  }
}

That time the TX light of the Arduino turned on while I pressed the keys but didnt make any sound in Ableton. That was its best shot I think.

I know about a Control.Surface.h library which I think can do everything I want but not much more. Which would be the best answer to this? It is possible really?
Again, any comment would help me.

Thxx :wink:

Get an app for the Mac Called MIDI Monitor. This allows you to see what is being sent by your Leonardo.

If you are getting MIDI messages from that then you need to do some other stuff to get Ableton to recognise it and send sound out.

I have moved your post to a better section of the Forum.

Please post the error messages here.

What do you mean by “not much more”?

With minimal changes to your code, you could use Control Surface as follows:

#include <Control_Surface.h>

USBMIDI_Interface midi;

// [...]

void setup() {
  midi.begin();
  // [...]
}

void loop() {
  midi.update();
  // [...]
    midi.sendNoteOn(tecla_presionada, 80);
  // [...]
}

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