adding velocity with a Potentiometer to a non velocity sensitive keyboard

Hi!

I added midi to a (non midi of course) casio keyboard. It has a simple button matrix (6 rows 9 colums for 49 keys, the highest column is just for the highest key).It is not velocity sensitive.

I found the “Midi_Controller” library by tttapa to be perfect for that.
There is already an example of a matrix included, so i just adapted it, and it
works perfect.

Now i was thinking about adding velocity to the keyboard…best would be to add some kind of
Fsr underneath the keys, so velocity can be read from that (of course its “just” sensor for all keys, but that would be ok for me)

In the Matrix sketch velocity is fixed to 127, which makes total sense for buttons but i cant figure out how to change that with a variable read from a potentiometer or fsr which is connected to A0.

(i am trying it right now with a potentiometer, because i have no fsr)

Can you give me a hint?
Thanks in advance!

/*
  This is an example of the "ButtonMatrix" class of the MIDI_controller library.
  Connect a 6 × 9 matrix of buttons
  Pull-up resistors are not necessary, because the internal ones will be used.

  If you want to be able to press multiple buttons at once, add a diode
  in series with each button, as shown in the schematic on the Wiki:
  https://github.com/tttapa/MIDI_controller/wiki/Hardware

  The note numbers are specified in the 'addresses' array.

  Written by tttapa, 24/09/2017
  https://github.com/tttapa/MIDI_controller
*/

#include "MIDI_Controller.h" // Include the library




const uint8_t velocity = 0b1111111; // Maximum velocity (0b1111111 = 0x7F = 127)
const uint8_t addresses[6][9] = {  // the note numbers corresponding to the buttons in the matrix
  {  36, 42, 48, 54, 60, 66, 72, 78, 84},
  {  37, 43, 49, 55, 61, 67, 73, 79},
  {  38, 44, 50, 56, 62, 68, 74, 80},
  {  39, 45, 51, 57, 63, 69, 75, 81},
  {  40, 46, 52, 58, 64, 70, 76, 82},
  {  41, 47, 53, 59, 65, 71, 77, 83}

};

// Create a new instance of the class 'ButtonMatrix', called 'buttonmatrix', with dimensions 6 rows and 9 columns,
//that sends MIDI messages with the notes specified in 'addresses' on MIDI channel 1, with velocity 127
ButtonMatrix<6, 9> buttonmatrix({2, 3, 4, 5, 6, 7 }, {8, 9, 10, 11, 12, A2, A3, A2, A3}, addresses, velocity, 1);

void setup() {}

void loop() {
  // Refresh the buttons (check whether the states have changed since last time, if so, send it over MIDI)
  MIDI_Controller.refresh();

}

Well your code incorporates the velocity as part of the instance of the button matrix class so there is nothing much you can do with this code. In other words, if you want to get there I wouldn’t start from here.

So

Change

const uint8_t velocity

To

uint8_t velocity

Then use

velocity = analogRead(0) << 3;

On each keypress. But you will need use real code not libraries that has a fixed velocity. Maybe there is a way of adding that separately but as what you want to do is trivial ( just three digitalWrite calls ) I would ditch it and write your own.

There might be a way of changing the velocity with other calls from this library, you will have to look at the documentation to see if there is.

Thanks Mike!

That makes sense, altough i had the hope i just could change the fixed "velocity" part with a variable read by the potentiometer.

Ok, so the plan is to write a new code with a button matrix that reads the keypresses as midi notes,
and the poti values as velocity.

Is it clever to incorporate the keypad library and map that to midi?

I will report back!

Is it clever to incorporate the keypad library and map that to midi?

Yes I think that is the way to go.
Good luck, ask again if you get stuck.

The MIDI_Controller library is no longer supported, it has been replaced with the Control Surface library.

The ButtonMatrix code is very similar: NoteButtonMatrix.ino
It also has a setVelocity() method to change the velocity:

[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color]
 
[b][color=#d35400]USBMIDI_Interface[/color][/b] [color=#00979c]midi[/color][color=#000000];[/color]
 
[color=#434f54]// The note numbers corresponding to the buttons in the matrix[/color]
[color=#00979c]const[/color] [color=#000000]AddressMatrix[/color][color=#434f54]<[/color][color=#000000]4[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]addresses[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]{[/color]
  [color=#000000]{[/color][color=#000000]1[/color][color=#434f54],[/color] [color=#000000]2[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#000000]}[/color][color=#434f54],[/color]
  [color=#000000]{[/color][color=#000000]4[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#434f54],[/color] [color=#000000]6[/color][color=#000000]}[/color][color=#434f54],[/color]
  [color=#000000]{[/color][color=#000000]7[/color][color=#434f54],[/color] [color=#000000]8[/color][color=#434f54],[/color] [color=#000000]9[/color][color=#000000]}[/color][color=#434f54],[/color]
  [color=#000000]{[/color][color=#000000]10[/color][color=#434f54],[/color] [color=#000000]11[/color][color=#434f54],[/color] [color=#000000]12[/color][color=#000000]}[/color][color=#434f54],[/color]
[color=#000000]}[/color][color=#000000]}[/color][color=#000000];[/color]
 
[b][color=#d35400]NoteButtonMatrix[/color][/b][color=#434f54]<[/color][color=#000000]4[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]buttonmatrix[/color] [color=#434f54]=[/color] [color=#000000]{[/color]
  [color=#000000]{[/color][color=#000000]2[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#000000]}[/color][color=#434f54],[/color] [color=#434f54]// row pins[/color]
  [color=#000000]{[/color][color=#000000]6[/color][color=#434f54],[/color] [color=#000000]7[/color][color=#434f54],[/color] [color=#000000]8[/color][color=#000000]}[/color][color=#434f54],[/color]    [color=#434f54]// column pins[/color]
  [color=#000000]addresses[/color][color=#434f54],[/color]    [color=#434f54]// address matrix[/color]
  [color=#00979c]CHANNEL_1[/color][color=#434f54],[/color]    [color=#434f54]// channel and cable number[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#434f54]// 7-bit analog input[/color]
[b][color=#d35400]FilteredAnalog[/color][/b][color=#434f54]<[/color][color=#000000]7[/color][color=#434f54]>[/color] [color=#000000]velocityPot[/color] [color=#434f54]=[/color] [color=#00979c]A0[/color][color=#000000];[/color]
 
[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
 
[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]velocityPot[/color][color=#434f54].[/color][color=#d35400]update[/color][color=#000000]([/color][color=#000000])[/color][color=#000000])[/color] [color=#434f54]// if the analog value changed[/color]
    [color=#000000]buttonmatrix[/color][color=#434f54].[/color][color=#d35400]setVelocity[/color][color=#000000]([/color][color=#000000]velocityPot[/color][color=#434f54].[/color][color=#d35400]getValue[/color][color=#000000]([/color][color=#000000])[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

Pieter

Thanks Pieter, that works!!!!