Arduino MIDI Drums


Arduino runs the code from SpikenzieLabs, modified to work with The Hairless MIDI serial bridge and to support a momentary hi-hat switch.

//  *****************************************************************************************************************
//  *                                                                                                               *
//  *                                         SpikenzieLabs.com                                                     *
//  *                                        StanleyProjects.com                                                    *
//  *                                                                                                               *
//  *****************************************************************************************************************
//
// The Hairless MIDI supported
// momentary switch as a hi-hat pedal



byte PadNote[6] = {52, 16, 66, 63, 40, 65};             // MIDI notes from 0 to 127 (Mid C = 60)

int pedal[3] = {2, 5, 70};                              // pedal input pin / open hi-hat piezo pin / closed hi-hat midi value

int PadCutOff[6] = {400, 400, 400, 400, 400, 400};      // 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  = false;                          // Velocity ON (true) or OFF (false)


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

byte status;

int pin = 0;
int hitavg = 0;

void setup()
{
  pinMode(pedal[0], INPUT_PULLUP);
  Serial.begin(57600);                                  // connect to the serial port 115200
}


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

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

        if (pedal[1] == pin && digitalRead(pedal[0]) == LOW)
        {
          MIDI_TX(144, pedal[2], hitavg);
        }
        else
        {
          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;
        if (pedal[1] == pin && digitalRead(pedal[0]) == LOW)
        {
          MIDI_TX(128, pedal[2], hitavg);
        }
        else
        {
          MIDI_TX(128, PadNote[pin], 127);
        }
      }
    }
  }
}

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