Help with MIDIUSB.h and Keypad.h

Hello, I apologize immediately for my poor English, but in English there is much more information about MIDI.

Well I have this problem / question using the libraries "MIDIUSB.h" and "Keypad.h"

When compiling, I get this error:::

*C:\Users\Roberto\Documents\Arduino\PRO MICRO\Piano_Midi_promicro_v17-07-2022\Piano_Midi_promicro_v17-07-2022.ino: In function 'void noteOn(byte, byte, byte)':*
*C:\Users\Roberto\Documents\Arduino\PRO MICRO\Piano_Midi_promicro_v17-07-2022\Piano_Midi_promicro_v17-07-2022.ino:6:42: warning: narrowing conversion of '(int)(144 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]*
*   midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};*
*                                     ~~~~~^~~~~~~~~*
*C:\Users\Roberto\Documents\Arduino\PRO MICRO\Piano_Midi_promicro_v17-07-2022\Piano_Midi_promicro_v17-07-2022.ino: In function 'void noteOff(byte, byte, byte)':*
*C:\Users\Roberto\Documents\Arduino\PRO MICRO\Piano_Midi_promicro_v17-07-2022\Piano_Midi_promicro_v17-07-2022.ino:11:43: warning: narrowing conversion of '(int)(128 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]*
*   midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};*

I would like to correct it but this is beyond my newbie knowledge.

I understand that there are problems converting data from (char) to (int).

please help

here I leave the code

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

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

const byte ROWS = 6; 
const byte COLS = 2; 

byte keys[ROWS][COLS] = {
{60,66},
{61,67},
{62,68},
{63,69},
{64,70},
{65,71}
};

byte rowPins[ROWS] = {7, 6, 5, 4, 3, 2};             
byte colPins[COLS] = {16, 10}; 
byte Channel = 0;
byte velocity =80;

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 )
            {
                switch (kpd.key[i].kstate) {
                    case PRESSED:
                    noteOn(Channel, int(kpd.key[i].kchar), velocity);
                    MidiUSB.flush();
                break;
                    case RELEASED:
                    noteOff(Channel, int(kpd.key[i].kchar), velocity);
                    MidiUSB.flush();
                break;
                }
            }
        }
    }
}

try to replace

noteOn(Channel, int(kpd.key[i].kchar), velocity);

to

noteOn(Channel, uint8_t(kpd.key[i].kchar), velocity);
void noteOn(byte channel, byte pitch, byte velocity) {
  uint8_t status = 0x90 | channel;
  midiEventPacket_t noteOn = {0x09, status, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

See Integral promotion.

You might find this useful: Control Surface: NoteButtonMatrix.ino

Thank you very much for taking your time in trying to help me. unfortunately it was not possible to solve the problem with that method, but thank you for trying to help me.

If the error remains, please show edited code and the error message

PieterP, thank you for taking your time to help me, it has been useful and I have corrected the problem :D, I really appreciate it.

I'll read the links you gave me.

At first I thought to use your library, but I plan to use the rotary encoder together with the button, to select different CCmidi parameters, but it was impossible for me to do it with its "control surface" library and my little knowledge, that's why I ended up falling to MIDIUSB, You did an excellent job with that though.

When I learn a little more about Arduino and programming, I will try again.

again thank you very much

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