#include <MIDIUSB.h>
// these are the arduino pins that the solenoids are hooked up to
enum drumPins {kickPin = 0, snarePin = 1, hhPin = 2, crashPin = 3, openhhPin = 4, ridePin = 5, floortomPin = 6, racktomPin = 7, cowbellPin = 8, sidestickPin = 9};
// these are the midi notes that each solenoid triggers on, as well as an alternate for each
enum midiNotes {kickMidi = 36, snareMidi = 38, hhMidi = 42, crashMidi = 49, openhhMidi = 46, rideMidi = 51, floortomMidi = 41, racktomMidi = 48, cowbellMidi = 56, sidestickMidi = 37};
enum midiNoteAlts {kickMidiAlt = 35, hhMidiAlt = 44, snareMidiAlt = 40, rideMidiAlt = 59, floortomMidiAlt = 43, racktomMidiAlt = 50};
enum midiNoteTest {testMidiC = 60, testMidiD = 62, testMidiE = 64, testMidiF = 65, testMidiG = 67, testMidiA = 69, testMidiB = 71, testMidiC2 = 72, testMidiD2 = 74, testMidiE2 = 76};
void setup() {
// the serial port is just used as a monitor for debugging
// it is not needed for midi
Serial.begin(115200);
// setup pins 0-9 as outputs
for(int i=0; i<=9; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
//listen for new MIDI messages
midiEventPacket_t rx = MidiUSB.read();
processMidi(rx);
}
void processMidi(midiEventPacket_t rx) {
switch (rx.header) {
case 0x0:
// do nothing
break;
// note on
case 0x9:
handleNoteOn(rx.byte1 & 0xF, rx.byte2, rx.byte3);
break;
// note off
case 0x8:
handleNoteOn(rx.byte1 & 0xF, rx.byte2, 0);
break;
// control change
case 11:
Serial.print("CC: ");
Serial.print(rx.byte2);
Serial.print(":");
Serial.print(rx.byte3);
Serial.print("\n");
break;
default:
Serial.println(rx.header);
break;
}
}
void handleNoteOn(byte channel, byte pitch, byte velocity) {
// it is possible to use the actual midi velocity here, just be sure to
// double to value because midi is 0-127
// and then change digitalWrite to analogWrite
if(velocity > 0) {
velocity = HIGH;
}
switch (pitch) {
case kickMidi:
case kickMidiAlt:
case testMidiC:
Serial.print("Kick: ");
digitalWrite(kickPin, velocity);
break;
case snareMidi:
case snareMidiAlt:
case testMidiD:
Serial.print("Snare: ");
digitalWrite(snarePin, velocity);
break;
case hhMidi:
case hhMidiAlt:
case testMidiE:
Serial.print("HH: ");
digitalWrite(hhPin, velocity);
break;
case crashMidi:
case testMidiF:
Serial.print("Crash: ");
digitalWrite(crashPin, velocity);
break;
case openhhMidi:
case testMidiG:
Serial.print("Open hat: ");
digitalWrite(openhhPin, velocity);
break;
case rideMidi:
case rideMidiAlt:
case testMidiA:
Serial.print("Ride: ");
digitalWrite(ridePin, velocity);
break;
case floortomMidi:
case floortomMidiAlt:
case testMidiB:
Serial.print("floortom: ");
digitalWrite(floortomPin, velocity);
break;
case racktomMidi:
case racktomMidiAlt:
case testMidiC2:
Serial.print("RackTom: ");
digitalWrite(racktomPin, velocity);
break;
case cowbellMidi:
case testMidiD2:
Serial.print("Cowbell: ");
digitalWrite(cowbellPin, velocity);
break;
case sidestickMidi:
case testMidiE2:
Serial.print("Sidestick: ");
digitalWrite(sidestickPin, velocity);
break;
default:
// print the midi note value, handy for adding new notes
Serial.print("Note(");
Serial.print(pitch);
Serial.print("): ");
break;
}
if(velocity == 0) {
Serial.println("off");
} else {
Serial.println("on");
}
}
Comments
Please log in or sign up to comment.