Skip to content

Instantly share code, notes, and snippets.

@lean8086
Last active October 16, 2020 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lean8086/866e279a7a13efacbcc3d6ae7bf344c2 to your computer and use it in GitHub Desktop.
Save lean8086/866e279a7a13efacbcc3d6ae7bf344c2 to your computer and use it in GitHub Desktop.
#include "MIDIUSB.h"
const byte TOTAL_BUTTONS = 16;
const byte BUTTONS_PIN[TOTAL_BUTTONS] = {2,3,4,5,6,7,8,9,10,11,12,A0,A1,A2,A3,A4};
const byte BUTTONS_PITCH[TOTAL_BUTTONS] = {36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
byte channel = 0; // 16 channels (0-15) Reported to the user as 1-16
void setup() {
for (byte i = 0; i < TOTAL_BUTTONS; i++) {
pinMode(BUTTONS_PIN[i], INPUT_PULLUP);
}
}
void loop() {
for (byte i = 0; i < TOTAL_BUTTONS; i++) {
byte buttonState = digitalRead(BUTTONS_PIN[i]);
byte pitch = BUTTONS_PITCH[i];
delay(2); // See https://arduino.cc/en/Tutorial/Debounce
if (buttonState == LOW) {
noteOn(pitch);
} else {
noteOff(pitch);
}
}
}
void noteOn(byte pitch) {
MidiUSB.sendMIDI({0x09, 0x90 | channel, pitch, 127});
MidiUSB.flush();
}
void noteOff(byte pitch) {
MidiUSB.sendMIDI({0x08, 0x80 | channel, pitch, 0});
MidiUSB.flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment