# Midi LED coding help

Hi I’m in the process of making a midi controller based on a midi fighter. It has 16 arcade buttons hooked up to a teensy 3.2 I’ve managed to get it working so when I hit one of the buttons it plays the correct note in my DAW. Happy days! My next mission is to have LEDs lit up constantly then turn off when any of the 16 buttons are pressed. I know how to wire this part up, I just have no idea how to go about the code. I have tried to research how to achieve this but I am really struggling with what I need code and where to put it. If any one can help writing the code for me I would really appreciate it, obviously I would pay. Failing that could someone point me in the right direction.

Cheers

What you need to implement is “finite state machine”. The moment a button (any button) is pressed, you set the program to the “lights off” state. After some time or when the sound is finished, set it back to the “lights on” state. Switch lights accordingly.

As it’s a two-state you could do this with a simple boolean flag.

You may have done this already to prevent button presses from playing a new tone when the previous tone is still playing.

Hi heres my code I’m using if it helps.

``````#include <Bounce.h>

const int channel = 1;

Bounce button1 = Bounce(16, 5);
Bounce button2 = Bounce(20, 5);
Bounce button3 = Bounce(0, 5);
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(15, 5);
Bounce button6 = Bounce(19, 5);
Bounce button7 = Bounce(1, 5);
Bounce button8 = Bounce(5, 5);
Bounce button9 = Bounce(14, 5);
Bounce button10 = Bounce(18, 5);
Bounce button11 = Bounce(2, 5);
Bounce button12 = Bounce(6, 5);
Bounce button13 = Bounce(13, 5);
Bounce button14 = Bounce(17, 5);
Bounce button15 = Bounce(3, 5);
Bounce button16 = Bounce(7, 5);
Bounce button17 = Bounce(24, 5);
Bounce button18 = Bounce(11, 5);
Bounce button19 = Bounce(12, 5);
Bounce button20 = Bounce(23, 5);
Bounce button21 = Bounce(10, 5);
Bounce button22 = Bounce(9, 5);

void setup() {

pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
}

void loop() {

button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();
button12.update();
button13.update();
button14.update();
button15.update();
button16.update();
button17.update();
button18.update();
button19.update();
button20.update();
button21.update();
button22.update();

if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(81, 99, channel);  // 61 = C#4
}
if (button2.fallingEdge()) {
usbMIDI.sendNoteOn(80, 99, channel);  // 62 = D4
}
if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(79, 99, channel);  // 63 = D#4
}
if (button4.fallingEdge()) {
usbMIDI.sendNoteOn(78, 99, channel);  // 64 = E4
}
if (button5.fallingEdge()) {
usbMIDI.sendNoteOn(77, 99, channel);  // 65 = F4
}
if (button6.fallingEdge()) {
usbMIDI.sendNoteOn(76, 99, channel);  // 66 = F#4
}
if (button7.fallingEdge()) {
usbMIDI.sendNoteOn(75, 99, channel);  // 67 = G4
}
if (button8.fallingEdge()) {
usbMIDI.sendNoteOn(74, 99, channel);  // 68 = G#4
}
if (button9.fallingEdge()) {
usbMIDI.sendNoteOn(73, 99, channel);  // 69 = A5
}
if (button10.fallingEdge()) {
usbMIDI.sendNoteOn(72, 99, channel);  // 70 = A#5
}
if (button11.fallingEdge()) {
usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B5
}
if (button12.fallingEdge()) {
usbMIDI.sendNoteOn(70, 99, channel);  // 71 = B5
}
if (button13.fallingEdge()) {
usbMIDI.sendNoteOn(69, 99, channel);  // 71 = B5
}
if (button14.fallingEdge()) {
usbMIDI.sendNoteOn(68, 99, channel);  // 71 = B5
}
if (button15.fallingEdge()) {
usbMIDI.sendNoteOn(67, 99, channel);  // 71 = B5
}
if (button16.fallingEdge()) {
usbMIDI.sendNoteOn(66, 99, channel);  // 71 = B5
}
if (button17.fallingEdge()) {
usbMIDI.sendNoteOn(65, 99, channel);  // 71 = B5
}
if (button18.fallingEdge()) {
usbMIDI.sendNoteOn(64, 99, channel);  // 71 = B5
}
if (button19.fallingEdge()) {
usbMIDI.sendNoteOn(63, 99, channel);  // 71 = B5
}
if (button20.fallingEdge()) {
usbMIDI.sendNoteOn(62, 99, channel);  // 71 = B5
}
if (button21.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel);  // 71 = B5
}
if (button22.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel);  // 71 = B5
}
if (button1.risingEdge()) {
usbMIDI.sendNoteOff(81, 0, channel);  // 61 = C#4
}
if (button2.risingEdge()) {
usbMIDI.sendNoteOff(80, 0, channel);  // 62 = D4
}
if (button3.risingEdge()) {
usbMIDI.sendNoteOff(79, 0, channel);  // 63 = D#4
}
if (button4.risingEdge()) {
usbMIDI.sendNoteOff(78, 0, channel);  // 64 = E4
}
if (button5.risingEdge()) {
usbMIDI.sendNoteOff(77, 0, channel);  // 65 = F4
}
if (button6.risingEdge()) {
usbMIDI.sendNoteOff(76, 0, channel);  // 66 = F#4
}
if (button7.risingEdge()) {
usbMIDI.sendNoteOff(75, 0, channel);  // 67 = G4
}
if (button8.risingEdge()) {
usbMIDI.sendNoteOff(74, 0, channel);  // 68 = G#4
}
if (button9.risingEdge()) {
usbMIDI.sendNoteOff(73, 0, channel);  // 69 = A5
}
if (button10.risingEdge()) {
usbMIDI.sendNoteOff(72, 0, channel);  // 70 = A#5
}
if (button11.risingEdge()) {
usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B5
}

if (button12.risingEdge()) {
usbMIDI.sendNoteOff(70, 0, channel);  // 71 = B5
}
if (button13.risingEdge()) {
usbMIDI.sendNoteOff(69, 0, channel);  // 71 = B5
}
if (button14.risingEdge()) {
usbMIDI.sendNoteOff(68, 0, channel);  // 71 = B5
}
if (button15.risingEdge()) {
usbMIDI.sendNoteOff(67, 0, channel);  // 71 = B5
}
if (button16.risingEdge()) {
usbMIDI.sendNoteOff(66, 0, channel);  // 71 = B5
}
if (button17.risingEdge()) {
usbMIDI.sendNoteOff(65, 0, channel);  // 71 = B5
}
if (button18.risingEdge()) {
usbMIDI.sendNoteOff(64, 0, channel);  // 71 = B5
}
if (button19.risingEdge()) {
usbMIDI.sendNoteOff(63, 0, channel);  // 71 = B5
}
if (button20.risingEdge()) {
usbMIDI.sendNoteOff(62, 0, channel);  // 71 = B5
}
if (button21.risingEdge()) {
usbMIDI.sendNoteOff(61, 0, channel);  // 71 = B5
}
if (button22.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel);  // 71 = B5
}

}
}
``````

Im now adding neopixels in the mix, would these be easy to code in?
I’m wanting to light up random colours each time a button is pressed, is this possible?

An array of Button objects, an array of note values, and 10% of the code you currently have would be a good start.

There is NOTHING in that code that creates a usbMidi object, so I can't see how that code even compiles.

Where is it sending the note? Not out the hardware serial port, obviously, since you have Button objects on those pins.

Im now adding neopixels in the mix, would these be easy to code in?

Yes, and no. Connecting the neopixels just requires one unused pin. So, creating the instance of the neopixel library is easy.

Determining how to light the pixels is the hard part. Dealing with the timing of lighting the pixels appropriately, while still being responsive to switch presses can be interesting.