I've created several midi devices with great success. Now I have a project using an Arduino Due (for the # of pins and usb midi output). For some reason, pins 22 and 23 (Transpose pins) aren't responding. Everything else works perfectly. Any suggestions (yes, the code is a little sloppy and I plan on creating some functions to simplify the midi messaging ;-). Thanks in advance!
#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
#include <MIDIUSB.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int MIDI_CHAN = 1;
const int DEBOUNCE_TIME = 40;
const int ButtonPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 22, 23}; // 13 notes & 4 chords & 2 Bank & 2 Transpose
byte note[] = {
B00100,
B00110,
B00101,
B00101,
B00100,
B11100,
B11100,
B11100
};
byte note1[] = {
B00111,
B00100,
B00100,
B00100,
B00100,
B11100,
B11100,
B11100
};
byte note2[] = {
B11111,
B00001,
B00001,
B00001,
B00001,
B00111,
B00111,
B00111
};
const int midi_note_nums[4][13] = {
{12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, // Bank 1 buttons 2-14 Cromatic Scale C - C octave
{24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}, // Bank 2 buttons 2-14 Cromatic Scale C - C octave
{36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}, // Bank 3 buttons 2-14 Cromatic Scale C - C octave
{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, // Bank 4 buttons 2-14 Cromatic Scale C - C octave
};
const int midi_chord_nums[4][12] = {
{29, 33, 36, 31, 34, 38, 34, 41, 38, 36, 40, 31 }, // Bank 1 buttons 15-18 - chords F, Gm, Bb, C
{43, 47, 38, 36, 40, 43, 38, 42, 45, 40, 43, 47 }, // Bank 2 buttons 15-18 - chords G, C, D, Em
{43, 48, 52, 43, 47, 50, 45, 40, 48, 41, 45, 48 }, // Bank 3 buttons 15-18 - chords C, G, Am, F
{35, 38, 42, 43, 47, 50, 52, 49, 57, 38, 42, 45 }, // Bank 4 buttons 15-18 - chords Bm, G, A, D
};
const char *chord_chords[4][4] = {
{"F ", "Gm", "Bb ", "C "}, // Display Chords Bank 1
{"G ", "C ", "D ", "Em "}, // Display Chords Bank 2
{"C ", "G ", "Am", "F "}, // Display Chords Bank 3
{"Bm ", "G ", "A ", "D "}, // Display Chords Bank 4
};
Bounce buttons[23];
int currentBank = 0;
int noteOn;
int noteOn1;
int noteOn2;
int noteOn3;
int transpose = 0;
int transposebank = 0;
void update_display() {
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" C# D# F# G# A#");
lcd.setCursor(0,3);
lcd.print("C D EF G A BC");
lcd.setCursor(0, 0);
lcd.print("Bank:" + String(1+currentBank));
lcd.setCursor(8, 0);
lcd.print("Chord: ");
lcd.setCursor(17, 0);
lcd.print("T:");
lcd.print(String(transposebank));
}
void setup() {
for (int i = 0; i < 22; i++) // check pins as 18+ cause a problem
{
pinMode(i, INPUT_PULLUP);
buttons[i].attach(i);
buttons[i].interval(DEBOUNCE_TIME);
}
//
// Initialize the display
// Clear the display
// Turn on the backlight
// Populate the display with Bank, Chord, and Notes representing button layout on beyboard
//
lcd.init();
lcd.backlight();
lcd.createChar(0, note);
lcd.createChar(1, note1);
lcd.createChar(2, note2);
lcd.setCursor(0, 2);
lcd.print(" C# D# F# G# A#");
lcd.setCursor(0, 3);
lcd.print("C D EF G A BC");
lcd.setCursor(0, 0);
lcd.print("Bank:" + String(1+currentBank));
lcd.setCursor(8,0);
lcd.print("Chord:");
lcd.setCursor(17, 0);
lcd.print("T:");
lcd.print(String(transposebank));
Serial.begin(31250);
}
void loop()
{
for (int i = 0; i < 22; i++) { // update buttons
buttons[i].update();
if (buttons[i].fell()) {
if (i==0) { // Check for Bank Down Button Press
if (currentBank==0)
{
currentBank=3;
lcd.setCursor(0, 0);
lcd.print("Bank:");
lcd.print(currentBank+1);
} else {
currentBank = currentBank-1;
lcd.setCursor(0, 0);
lcd.print("Bank:");
lcd.print(currentBank+1);
}
} else if (i==1) { // Check for Bank Up Button Press
if (currentBank==3)
{
currentBank=0;
lcd.setCursor(0, 0);
lcd.print("Bank:");
lcd.print(currentBank+1);
} else {
currentBank= currentBank+1;
lcd.setCursor(0, 0);
lcd.print("Bank:");
lcd.print(currentBank+1);
}
} else if (i==22) { // Check for Transpose Down Button Press
if (transposebank==0)
{
transpose=transpose+36;
transposebank=3;
lcd.setCursor(17, 0);
lcd.print("T:");
lcd.print(String(transposebank));
} else {
transpose = transpose-12;
transposebank = transposebank-1;
lcd.setCursor(17, 0);
lcd.print("T:");
lcd.print(String(transposebank));
}
} else if (i==23) { // Check for Transpose Up Button Press // Check for Transpose Up Button Press
if (transposebank==3)
{
transposebank=0;
transpose = transpose-36;
lcd.setCursor(16, 0);
lcd.print("T:");
lcd.print(String(transposebank));
} else {
transposebank= transposebank+1;
transpose = transpose+12;
lcd.setCursor(16, 0);
lcd.print("T:");
lcd.print(String(transposebank));
}
} else if (i==2) {
update_display();
lcd.setCursor(0, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127}; // Sends Midi note pressed from the midi_note_nums array
MidiUSB.sendMIDI(noteOn);
} else if (i==4) {
update_display();
lcd.setCursor(3, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==6) {
update_display();
lcd.setCursor(6, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==7){
update_display();
lcd.setCursor(7, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==9) {
update_display();
lcd.setCursor(10, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==11) {
update_display();
lcd.setCursor(13, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==13) {
update_display();
lcd.setCursor(16, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==14) {
update_display();
lcd.setCursor(17, 1);
lcd.write(byte(0));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==3) {
lcd.setCursor(1, 1);
lcd.write(byte(1));
lcd.write(byte(2));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==5) {
update_display();
lcd.setCursor(4, 1);
lcd.write(byte(1));
lcd.write(byte(2));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==8) {
update_display();
lcd.setCursor(8, 1);
lcd.write(byte(1));
lcd.write(byte(2));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i==10){
update_display();
lcd.setCursor(11, 1);
lcd.write(byte(1));
lcd.write(byte(2));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127}; // Sends Midi note pressed from the midi_note_nums array
MidiUSB.sendMIDI(noteOn);
} else if (i==12) {
update_display();
lcd.setCursor(14, 1);
lcd.write(byte(1));
lcd.write(byte(2));
midiEventPacket_t noteOn = {0x09, 0x90 | 0, midi_note_nums[currentBank][i-2]+transpose, 127};
MidiUSB.sendMIDI(noteOn);
} else if (i == 15) { // check for chord press button - button 15 to 18
lcd.setCursor(8, 0);
lcd.print("Chord:" + String (chord_chords[currentBank][i-15])); // Displays the chord played based on the chord_chords array
midiEventPacket_t noteOn1 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-15]+transpose, 127}; // Note 1 of Chord - NOTE # 0 of array
MidiUSB.sendMIDI(noteOn1);
midiEventPacket_t noteOn2 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-14]+transpose, 127}; // Note 2 of Chord - NOTE # 1 of array
MidiUSB.sendMIDI(noteOn2);
midiEventPacket_t noteOn3 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-13]+transpose, 127}; // Note 3 of Chord - NOTE # 2 of array
MidiUSB.sendMIDI(noteOn3);
} else if (i == 16) { // check for chord press button
lcd.setCursor(8, 0);
lcd.print("Chord:" + String (chord_chords[currentBank][i-15])); // Displays the chord played based on the chord_chords array
midiEventPacket_t noteOn1 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-13]+transpose, 127}; // Note 1 of Chord - NOTE # 0 of array
MidiUSB.sendMIDI(noteOn1);
midiEventPacket_t noteOn2 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-12]+transpose, 127}; // Note 2 of Chord - NOTE # 1 of array
MidiUSB.sendMIDI(noteOn2);
midiEventPacket_t noteOn3 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-11]+transpose, 127}; // Note 3 of Chord - NOTE # 2 of array
MidiUSB.sendMIDI(noteOn3);
} else if (i == 17) { // check for chord press button
lcd.setCursor(8, 0);
lcd.print("Chord:" + String (chord_chords[currentBank][i-15])); // Displays the chord played based on the chord_chords array
midiEventPacket_t noteOn1 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-11]+transpose, 127}; // Note 1 of Chord - NOTE # 0 of array
MidiUSB.sendMIDI(noteOn1);
midiEventPacket_t noteOn2 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-10]+transpose, 127}; // Note 2 of Chord - NOTE # 1 of array
MidiUSB.sendMIDI(noteOn2);
midiEventPacket_t noteOn3 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-9]+transpose, 127}; // Note 3 of Chord - NOTE # 2 of array
MidiUSB.sendMIDI(noteOn3);
} else if (i == 18) {
lcd.setCursor(8, 0);
lcd.print("Chord:" + String (chord_chords[currentBank][i-15]));
midiEventPacket_t noteOn1 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-9]+transpose, 127}; // Note 1 of Chord - NOTE # 0 of array
MidiUSB.sendMIDI(noteOn1);
midiEventPacket_t noteOn2 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-8]+transpose, 127}; // Note 2 of Chord - NOTE # 1 of array
MidiUSB.sendMIDI(noteOn2);
midiEventPacket_t noteOn3 = {0x09, 0x90 | 0, midi_chord_nums[currentBank][i-7]+transpose, 127}; // Note 3 of Chord - NOTE # 2 of array
MidiUSB.sendMIDI(noteOn3);
}
} else if (buttons[i].rose()) { // Check if button rose
update_display(); // update the display
midiEventPacket_t noteOff = {0x08, 0x80 | 0, noteOn, 0}; // if note button rose, turn off note
MidiUSB.sendMIDI(noteOff);
midiEventPacket_t noteOff1 = {0x08, 0x80 | 0, noteOn1, 0}; // if chord button rose, turn off note 1 of chord
MidiUSB.sendMIDI(noteOff1);
midiEventPacket_t noteOff2 = {0x08, 0x80 | 0, noteOn2, 0}; // if chord button rose, turn off note 2 of chord
MidiUSB.sendMIDI(noteOff2);
midiEventPacket_t noteOff3 = {0x08, 0x80 | 0, noteOn3, 0}; // if chord button rose, turn off note 3 of chord
MidiUSB.sendMIDI(noteOff3);
}
}
}