arduino mega drum midi controller

hello friends! I have made a drum midi controller using arduino mega. I use the 16 analog inputs, not use digital inputs and not use such muxshield like another midi project. how to keep the sound produced can be in accordance with the force of the blow on the piezo buzzer when the velocity boolean enabled? I hope anyone can help me. thank you. :slight_smile:

-my sketch:

unsigned char status;
unsigned char PadNote[16] = {47,18,32,38,43,13,45,17,21,48,49,50,51,52,53,54};
int PadCutOff[16] = {100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};
int MaxPlayTime[16] = {60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60};
#define midichannel 0;
boolean activePad[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int PinPlayTime[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean VelocityFlag = false;
int analogPin[16] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15};
int pin = 0;
int hitavg = 0;

void setup()
{
Serial.begin(115200); // connect to the serial port 115200
}

void loop()
{
for(int pin=0; pin < 16; pin++)
{
hitavg = analogRead(pin);

if((hitavg > PadCutOff[pin]))
{
if((activePad[pin] == false))
{
if(VelocityFlag == true)
{
// hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));
hitavg = (hitavg / 8) -1 ;
}
else
{
hitavg = 127;
}

MIDI_TX(144,PadNote[pin],hitavg);
PinPlayTime[pin] = 0;
activePad[pin] = true;
}
else
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
}
}
else if((activePad[pin] == true))
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;

if(PinPlayTime[pin] > MaxPlayTime[pin])
{
activePad[pin] = false;
MIDI_TX(128,PadNote[pin],127);
}
}
}
}

void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
status = MESSAGE + midichannel;
Serial.write(status);
Serial.write(PITCH);
Serial.write(VELOCITY);
}

Use your hitavg value, and reduce it to a value between 0 and 127

vel = hitavg >> 3

Then use the vel variable as the third parameter of your MIDI note on message in place of hitavg like you use now.