Adding hi hat pedal to drum sketch

Hi! Im running a version of SpikenzieLab’s DrumKitAI Sketch on an Arduino mega 2560.
The problem is that the original sketch doesnt contemplate any hi hat pedal and I want to add one.

Im new to programming and arduino and I cant find the way to add it with a pusher button.

This is the original code (the only modification is that I only use 3 piezo sensor for now):

//*******************************************************************************************************************
// User settable variables
//*******************************************************************************************************************

unsigned char PadNote[6] = {22,36,60,62,64,65}; // MIDI notes from 0 to 127 (Mid C = 60)

int PadCutOff[6] = {900,950,900,900,900,900}; // Minimum Analog value to cause a drum hit

int MaxPlayTime[6] = {90,90,90,90,90,90}; // Cycles before a 2nd hit is allowed

#define midichannel 0; // MIDI channel from 0 to 15 (+1 in “real world”)

boolean VelocityFlag = true; // Velocity ON (true) or OFF (false)

//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************

boolean activePad[6] = {0,0,0,0,0,0}; // Array of flags of pad currently playing
int PinPlayTime[6] = {0,0,0,0,0,0}; // Counter since pad started to play

unsigned char status;

int pin = 0;
int hitavg = 0;

//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************

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

//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************

void loop()
{
for(int pin=0; pin < 3; pin++)
{
hitavg = analogRead(pin); // read the input pin

if((hitavg > PadCutOff[pin]))
{
if((activePad[pin] == false))
{
if(VelocityFlag == true)
{
// hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)
hitavg = (hitavg / 8 ) -1 ; // Upper range
}
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);
}
}
}
}

//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
status = MESSAGE + midichannel;
Serial.write(status);
Serial.write(PITCH);
Serial.write(VELOCITY);

}

I want to be able to swith sounds when the button is pushed and when its not.
So when the buton is pushed and the sensor for the hi hat is hit, I want it to make the close hi hat sound. And when the buton isnt pushed and the sensor is hit, I want it to make the open hihat sound.
Also, I want it to make the closed hihat sound every time the buton is pushed (the moment when hi hat closes)

Can someone help me?

OK, so I manage to modify the code to deliver the first part of my question, that is, “I want to be able to swith sounds when the button is pushed and when its not”

with the if statement in the biginning of the for loop (the led was only to know if that was working)

//*******************************************************************************************************************
// User settable variables      
//*******************************************************************************************************************



unsigned char PadNote[6] = {22,36,38,62,64,65};         // MIDI notes from 0 to 127 (Mid C = 60)

int PadCutOff[6] = {900,950,900,900,900,900};           // Minimum Analog value to cause a drum hit

int MaxPlayTime[6] = {90,90,90,90,90,90};               // Cycles before a 2nd hit is allowed

#define  midichannel  0;                              // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)

int pedalhihat = 40;                                     // entrada digital pedal hihat entrada digital 40

int led = 13;

//*******************************************************************************************************************
// Internal Use Variables     
//*******************************************************************************************************************

boolean activePad[6] = {0,0,0,0,0,0};                   // Array of flags of pad currently playing
int PinPlayTime[6] = {0,0,0,0,0,0};                     // Counter since pad started to play

unsigned char status;

int pin = 0;    
int hitavg = 0;

//*******************************************************************************************************************
// Setup      
//*******************************************************************************************************************

void setup()
{
 Serial.begin(57600);                                  // connect to the serial port 115200
 pinMode (led, OUTPUT);
}

//*******************************************************************************************************************
// Main Program     
//*******************************************************************************************************************

void loop()
{
 
 for(int pin=0; pin < 3; pin++)
 {
   hitavg = analogRead(pin);                              // read the input pin
   
  if (digitalRead(pedalhihat) == HIGH)                   //THIS IS THE NEW PART I ADDED
   {
      PadNote[0] = 22;
      
      digitalWrite(led, LOW);
   }   
   else 
   {
      PadNote[0] = 26;
      digitalWrite(led, HIGH);
   }
   
   if((hitavg > PadCutOff[pin]))
   {
     if((activePad[pin] == false))
     {
       if(VelocityFlag == true)
       {

//          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
         hitavg = (hitavg / 8 ) -1 ;                                                 // Upper range
       }
       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);
     }
   }
 }
}


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

But I still cant figure out how to make the closed hi hat sound every time i press the button.
Please help me!

"I want to be able to swith sounds when the button is pushed and when its not"

If that is what you want to do then just ignore any button because the state of the button is irrelevant to whether you get sound or not. Or do you not mean what you write?

  if (digitalRead(pedalhihat) == HIGH)                   //THIS IS THE NEW PART I ADDED
   {
      PadNote[0] = 22;
     
      digitalWrite(led, LOW);
   }   
   else
   {
      PadNote[0] = 26;
      digitalWrite(led, HIGH);
   }

All that does is to change the number in the first element of the PadNote array. It will not play any note.
Is the button actually a switch? Because if not as soon as the button is released the number in the PadNote array reverts back to what it was.

Also the contents of the PadNote array are MIDI notes, they are not percussion numbers. The percussion numbers are here:-

Note that in order to get a percussion sound you have to use MIDI channel 10, which means that:-

#define  midichannel  0; 
// should be
#define  midichannel  9;