Skip to content

Instantly share code, notes, and snippets.

@lean8086

lean8086/punchy.ino

Last active Jun 26, 2020
Embed
What would you like to do?
#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};
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, pitch, 127});
MidiUSB.flush();
}
void noteOff(byte pitch) {
MidiUSB.sendMIDI({0x08, 0x80, pitch, 0});
MidiUSB.flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.