0% found this document useful (0 votes)
130 views

Arduino MIDI Drums

This Arduino code defines variables and functions for a MIDI drum pad system with 10 pads. It reads analog sensor values on each pad, sends MIDI note on/off messages for any pads pressed above a threshold, and includes velocity sensitivity and timing to allow notes to retrigger.

Uploaded by

Rio Stevano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Arduino MIDI Drums

This Arduino code defines variables and functions for a MIDI drum pad system with 10 pads. It reads analog sensor values on each pad, sends MIDI note on/off messages for any pads pressed above a threshold, and includes velocity sensitivity and timing to allow notes to retrigger.

Uploaded by

Rio Stevano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

//

***************************************************************************
************************************** // *
*
// * Arduino MIDI Drums *
// * 2017 (c) StanleyProjects.com *
// *
*
// * Custom code - no hi-hat pedal, 10 drum pads *
//
***************************************************************************
**************************************
#define NUMBER_OF_PADS 10 // number of drum pads (analog inputs)
#define VELOCITY_ACTIVE 1 // velocity sensitive ON-OFF [1-0] #define
MIDI_CHANNEL 1 // MIDI channel [1-16]

#define MIDI_MAX_VELOCITY 127


#define MIDI_NOTE_ON 0x90 // MS nibble for note on status message #define
MIDI_NOTE_OFF 0x80 // MS nibble for note off status message
uint8_t padNote[NUMBER_OF_PADS] = {46, 37, 40, 50, 41, 49, 51, 36, 45, 47}; // MIDI
notes [0-127]
uint8_t padThreshold[NUMBER_OF_PADS] = {30, 30, 30, 30, 30, 30, 30, 30, 30, 30}; //
trigger treshold values [0-1023]
uint8_t padCycles[NUMBER_OF_PADS] = {20, 20, 20, 20, 20, 20, 20, 20, 20, 20}; //
number of cycles before the second trigger is allowed
uint8_t activePad; // each bit represents a pad state
uint8_t padCurrentCycles[NUMBER_OF_PADS]; // number of cycles since the pad was
triggered

void setup() {
Serial.begin(31250);
// initialize the serial port at baud rate 115200
}

void loop() {
for (uint8_t pin = 0; pin < NUMBER_OF_PADS; pin++) {
// loop through all of the pads
uint8_t val = analogRead(pin);
// read the input pin
if ((val > padThreshold[pin]) && (!padActive(pin))) {
// if hit strong enough

val = VELOCITY_ACTIVE ? velocityAlgorithm(val) : MIDI_MAX_VELOCITY;


// if velocity sensitive, calculate the new value, otherwise apply the maximum value

midi_tx_note_on(padNote[pin], val);
// send a note on MIDI message
padCurrentCycles[pin] = 0;
// reset the current pad cycle counter
activePad |= 1 << pin;
// set corresponding bit (active flag)
}

if (padActive(pin)) {
// enter if pad is active
padCurrentCycles[pin] += 1;
// increment the cycle counter by 1

if (padCurrentCycles[pin] >= padCycles[pin]) {


// enter if cycle counter reached the desired number of cycles
midi_tx_note_off(padNote[pin]); // send a note off MIDI message
activePad &= ~(1 << pin);
// clear corresponding bit (active flag)
}
}
}
}

uint8_t velocityAlgorithm(uint8_t val) {


return (val - 0) * (127 - 0) / (1023 - 0) + 0;
// remap the val from [0-1023] to [0-127]
}

uint8_t padActive(uint8_t currentPin) {


// check if current pad active
return (activePad >> currentPin) & 1;
}

void midi_tx_note_on(uint8_t pitch, uint8_t velocity) {


// send note on MIDI message
Serial.write((MIDI_NOTE_ON << 4) | (MIDI_CHANNEL - 1)); Serial.write(pitch);
Serial.write(velocity);
}

void midi_tx_note_off(uint8_t pitch) {


// send note off MIDI message
Seriazl.write((MIDI_NOTE_OFF << 4) | (MIDI_CHANNEL - 1)); Serial.write(pitch);
Serial.write(0);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy