Matrix Key Array Question

I am just learning to code to go easy on me...
I have a 25key midi keyboard with velocity sensitive keys with aftertouch. I have the keymatrix connected to an arduino micro(32u4). Each key has 2 switches (second switch for velocity) The keymatrix circuit is output to a 16 pin header.

My code is using the NoteButtonmatrix class, I have added a second button matrix for the "velocity" switches. I am using MidiView and I am able to see each key press with a clear time delay between the first and second key switch press. How would I measure the time from the first switch to the second switch to determine the key press velocity? Let me know if I am miss understanding how the 2 switches are intended to work. This is what my code looks like so far.

I would also like to connect the built in FSR strip witch I assume is for aftertouch.

miniviewsnip

#include <Control_Surface.h>

USBMIDI_Interface MPK25V2;

//A0,A2,A4,11 >>>> NOTE ON SWITCHES (addresses)(buttonmatrix)
//A1,A3,A5,12 >>>> VELOCITY ON SWITCHES (addresses2)(buttonmatrix2)
///--------------------------------------------
// The note numbers corresponding to the buttons in the matrix
const AddressMatrix<4, 8> addresses {{
{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},
}};

NoteButtonMatrix<4, 8> buttonmatrix {
{A0,A2,A4,11}, // row pins
{2,3,4,5,6,7,8,9}, // column pins
addresses, // address matrix
CHANNEL_1, // channel and cable number
};
///---------------------------------------------
const AddressMatrix<4, 8> addresses2 {{
{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},
}};

NoteButtonMatrix<4, 8> buttonmatrix2 {
{A1,A3,A5,12}, // row pins
{2,3,4,5,6,7,8,9}, // column pins
addresses2, // address matrix
CHANNEL_1, // channel and cable number
};

void setup() {
Control_Surface.begin();

}

void loop() {
Control_Surface.loop();

}

For each key, you need to start a software stopwatch when the first switch goes active, and stop it when the second switch goes active. The time between those two events, once suitable scaled, gives you the velocity. Getting a usable result requires capturing the two events with a reasonably high sample rate, so the velocity is captured with something like no more than a very few milliseconds accuracy. You'll need to figure out, empirically, based on how your specific keyboard responds, how to scale the raw "stopwatch" readings to appropriate velocity values.

I looked at the stopwatch library, however I am sure the control surface midi library has a way to do this already, I am just not having any luck figuring out the exact was this is implemented using control surface.
Control-Surface

I see nothing in the control surface library that even suggests it can do velocity sensing on a keyboard matrix. It appears to me to support matric keyboards only as single-event keys for UI functions, NOT for generating note events, and with no velocity sensing.

You don't need a stopwatch library. You just need to keep an array of micros() timestamps, one for each switch. You record the current micros() into the array when the first switch is turned on, and then subtract it from the micros() when the second switch is turned on. Finally, map the number of microseconds between these two events to a MIDI velocity value, as described by RayLivingston.

Control Surface keyboard matrices do not measure velocity, they simply send note messages with a fixed velocity when a switch is pressed/released.

You could reuse some of the logic from Control Surface's ButtonMatrix class:

Thank you so much for answering and please forgive my ignorance. I am still learning. Im not sure how this section of logic from the ButtonMatrix.ipp would look in Arduino/c. If you could post this same section of code but in Arduino/c then I could compare them and that would help me learn what info needs to go where in the Arduino code. I don't expect you to write all of the code for me. I want to gain a basic understanding of how to implement a template/class from a .ipp or .hpp file and be able to translate it into Arduino correctly. Any pointers you think would help me learn feel free to share.

I have a 25 key matrix with 50 switches in total, each key has 2 switches.
The matrix is working perfectly.
But I need to add velocity timing for the second switch on each key.

I need to do the following.

How would I create an array of micros() timestamps, one for each switch.
Then record the current micros() into that array when the first switch is turned on.
Then subtract it from the micros() when the second switch is turned on.
Finally, map the number of microseconds between these two events.

Exactly as you described it.

What is really your question? How to define an array ?

My question is how would the code be written for this??

How would I create an array of micros() timestamps, one for each switch.
Then record the current micros() into that array when the first switch is turned on.
Then subtract it from the micros() when the second switch is turned on.
Finally, map the number of microseconds between these two events.

Just define an array

uint32_t  timeCapture[25];

When you detect switch number n (0 to 24) is pressed then you record the time

timeCapture[n] = micros();

When you detect the second switch number n

timeCapture[n] = micros() - timeCapture[n];

Just do your mapping

long x = map(timeCapture[n], 0, 100000, 28, 42); // whatever map is supposed to do

Thank you!!

@PieterP can you put together an example showing how this would be written?

If i have the following working NoteButtonMatrix how would I capture the key press time from that Matrix? I want to use that data to calculate the velocity of each keypress.

const AddressMatrix<4, 8> addresses {{
{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},
}};

NoteButtonMatrix<4, 8> buttonmatrix {
{A0,A2,A4,11}, // row pins
{2,3,4,5,6,7,8,9}, // column pins
addresses, // address matrix
CHANNEL_1, // channel and cable number
};

@randyryan, please do not cross-post. Threads merged.

How would the new timecapture array get the state of each switch of the the switches are part of a separate buttonmatrix/array??

I have no idea how to "re use" this in my sketch I would need an example as a starting point.

Write a code capturing switches presses and releases and post it here

See Post #13

I don’t see a code showing the presses ?

The key presses from that code are shown in post #1

miniviewsnip