// using https://github.com/FortySevenEffects/arduino_midi_library
#include <MIDI.h>
// Create and bind the MIDI interface to the default hardware Serial port
MIDI_CREATE_DEFAULT_INSTANCE();
const uint8_t BUTTONS = 13;
uint8_t channel = 1;
uint8_t velocity = 0x45; // medium
const unsigned long debouncePeriod = 10; // milliseconds
// MIDI note numbers
// MIDI uses 60 for middle C, 36 for C two octave below
// (which I'd call C2, though some might call C1)
const uint8_t N_C2 = 36;
const uint8_t N_CS2 = 37;
const uint8_t N_D2 = 38;
const uint8_t N_DS2 = 39;
const uint8_t N_E2 = 40;
const uint8_t N_F2 = 41;
const uint8_t N_FS2 = 42;
const uint8_t N_G2 = 43;
const uint8_t N_GS2 = 44;
const uint8_t N_A2 = 45;
const uint8_t N_AS2 = 46;
const uint8_t N_B2 = 47;
const uint8_t N_C3 = 48;
struct button{
uint8_t pin;
uint8_t note;
int state;
unsigned long changeTime;
} buttons [BUTTONS] = {
// Analogue pins can be referenced as A0, A1 etc.
{13, N_C2, HIGH, 0},
{12, N_CS2, HIGH, 0},
{11, N_D2, HIGH, 0},
{10, N_DS2, HIGH, 0},
{9, N_E2, HIGH, 0},
{8, N_F2, HIGH, 0},
{7, N_FS2, HIGH, 0},
{6, N_G2, HIGH, 0},
{5, N_GS2, HIGH, 0},
{4, N_A2, HIGH, 0},
{3, N_AS2, HIGH, 0},
{2, N_B2, HIGH, 0},
{A0, N_C3, HIGH, 0}
};
void setup() {
//MIDI baud rate
Serial.begin(31250);
for (button b: buttons) {
pinMode(b.pin,INPUT_PULLUP);
} // for
}
void loop() {
unsigned long now;
int state;
for (button *bp = buttons; bp < buttons + BUTTONS; bp++) {
state = digitalRead(bp->pin);
if (state != bp->state) {
now = millis();
if (now - bp->changeTime > debouncePeriod) {
bp->state = state;
bp->changeTime = now;
if (state == LOW) {
MIDI.sendNoteOn(bp->note, velocity, channel);
} else {
MIDI.sendNoteOff(bp->note, velocity, channel);
} // else
}
}
} // for
}