[HELP] Midi button LED control code.

Hi I’m after some help please. I’m completely new to coding and have always wanted to make my own midi fighter. I’ve finally made a start and I have almost finished my housing for 16 arcade buttons and a Teensy 3.2. It’s made up from two pieces of wood top and bottom with an acrylic band in the middle which will be lit up by LEDs.
I’ve got the code sorted (copy & pasted) for the buttons to send midi to my DAW that’s all tickity boo.
My aim is to have the 5 LEDs on all the time then go off when any of the 16 buttons are pressed. I just can’t get my head around what I need to enter and where.
Any help would be greatly appreciated.

Heres my code so far.

#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS  16

const uint16_t button1 = 2;
const uint16_t button2 = 3;
const uint16_t button3 = 4;
const uint16_t button4 = 5;
const uint16_t button5 = 6;
const uint16_t button6 = 7;
const uint16_t button7 = 8;
const uint16_t button8 = 9;
const uint16_t button9 = 10;
const uint16_t button10 = 14;
const uint16_t button11 = 15;
const uint16_t button12 = 16;
const uint16_t button13 = 18;
const uint16_t button14 = 19;
const uint16_t button15 = 20;
const uint16_t button16 = 21;

const uint16_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16};
const byte notePitches[NUM_BUTTONS] = {pitchC2, pitchD2b, pitchD2, pitchE2b, pitchE2, pitchF2, pitchG2b, pitchG2, pitchA2b, pitchA2, pitchB2b, pitchB2, pitchC3, pitchD3b, pitchD3, pitchE3b};

uint16_t notesTime[NUM_BUTTONS];
uint16_t pressedButtons = 0x00;
uint16_t previousButtons = 0x00;
uint16_t intensity;

void setup() {
  for (int i = 0; i < NUM_BUTTONS; i++)
    pinMode(buttons[i], INPUT_PULLUP);
}
{
pinMode(4, OUTPUT); //Use digital out 4 for the LED
usbMIDI.setHandleNoteOn(OnNoteOn); //Specify which function to handle NoteOn events
usbMIDI.setHandleNoteOff(OnNoteOff); //Specify which function to handle NoteOff events
}



void loop() {
  readButtons();
  playNotes();
}
{
usbMIDI.read(); //Start listen for MIDI events from USB
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

void readButtons()
{
  for (int i = 0; i < NUM_BUTTONS; i++)
  {
    if (digitalRead(buttons[i]) == LOW)
    {
      bitWrite(pressedButtons, i, 1);
      delay(2);
    }
    else
      bitWrite(pressedButtons, i, 0);
  }
}

void playNotes()
{
  for (int i = 0; i < NUM_BUTTONS; i++)
  {
    if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
    {
      if (bitRead(pressedButtons, i))
      {
        bitWrite(previousButtons, i , 1);
        noteOn(0, notePitches[i], 100);
        MidiUSB.flush();
      }
      else
      {
        bitWrite(previousButtons, i , 0);
        noteOff(0, notePitches[i], 0);
        MidiUSB.flush();
      }
    }
  }
}

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}
{ //This is the function that handles NoteOn events
if(note == 60){ //If the note was C4 = 60
digitalWrite(4, HIGH); //5V ON
}
void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}
void OnNoteOff(byte channel, byte note, byte velocity){ //This is the function that handles NoteOff events
digitalWrite(4, LOW); //5V OFF, doesn't matter what note you release
}

To start with you need resistors on the LEDs.

to make my own midi fighter

I have written a book on MIDI and I don’t know what one of these is.

Where did you get that code from? I assume it is not written by you.

The PitchToNote.h is nothing to do with MIDI, it is used with the tone libiary.

Whoops yeah got resistors just forgot to put them on in a rush.

Google midi fighter 64 that’s the dream right there but I’ll stick with 16 buttons right now.

I got the code from a guy called nerd musician.

Cool so I can just delete the pitchnote bits to tidy it up.
Can you shed any light on the LEDs?

Cool so I can just delete the pitchnote bits to tidy it up.

Yes but you need to replace it with a table that links your buttons to the MIDI note numbers.

An led will light when you apply a logic high to the resistor / LED seriese circuit.

I still don’t understand what you are trying to make and how the thing is supposed to operate. Can you describe exactly what it is supposed to do.

It’s a 16 button midi controller to play notes in a DAW and I would like LEDs to be on all the time and flash/off when one of the 16 buttons are pressed.
I’ve attached a midi fighter which has light up buttons and then my concept is to have a band of light in the middle.
https://flic.kr/p/276wfN7
https://flic.kr/p/Hvwp8D

That looks a lot like my project Mini Monome but with a direct MIDI output.

So the first thing you need to know is what note corresponds to what push button. Build that into an array and use it as a look up table.

Then you need to draw a schematic of your circuit, have you got 32 I/O lines free on your processor or do you need some multiplexing?

Then it should be a simple matter of scanning through the buttons and any you find pressed outputting the MIDI note on message with the note number from the array and turning off the corresponding LED. Then on release turning on the LED and sending the MIDI note off message.

Things to worry about is can the Teensy supply enough current to turn on all 16 LEDs at the same time. If each LED takes 20mA then that would be 320mA which would be too much for a Mega. The absolute maximum for various pin combinations should be in the data sheet. If you don’t have enough current available then you will need a transistor or Darlington driver to boost the output current.

Edit:- sorry just looked at your origional post and seen you only need 5 LEDs so you should be OK for current. But what exactly will determin the cut off LED pattern, do you want it random, fixed by a table or related to the MIDI note number somehow?

You could use a Neopixel strip along each acrylic edge for extra colourful LED flashing fun.

Oh the neopixel might be the way forward and then I could have the different rows light up different colours and what not.

Will these be ok?

Those will be fine. However they are only 30 LEDs per meter, so that is a spacing of 33mm between LEDs that reduces the number of LEDs per side you can get in your box. So a smaller strip of higher density LEDs might be better.

How long is the side of your box?
Four of these might be better NeoPixel Stick - 8 x 5050 RGB LED with Integrated Drivers | The Pi Hut

Thanks,I’ve ordered for of those to have a play with.

OK - just re reading your first post:-

and a Teensy 3.2.

And in the code you say:-

#include "MIDIUSB.h"

Do you know that the MIDIUSB libiary is not compatible with the Teensy?
If you want to use a Teensy then you have to use the USB_MIDI libiary :- Teensyduino: Using USB MIDI with Teensy on the Arduino IDE

Ok got a new code now, its all wired up and working. Just need to wait for the neopixels to arrive. I would like a row of four buttons do a different colour if that make sense?

row 1 (4 buttons) blue
row 2 (4 buttons) red
row 3 (4 buttons) yellow
row 4 (4 buttons) green

How does this look and where would I enter the code for the neopixels?

#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
  }

  while (usbMIDI.read()) {
  }
}

That code is a bit turgid to say the least.

You want to write a function that will light up the strip after the examples in the library that you use to drive them.
This function needs to be called every time you output something to MIDI.

oh ok, I really have no idea, sorry. What parts could I get rid of?

What parts could I get rid of?

Most of it.
When you write nearly the same line over and over you can replace it with one line in a for loop, with the part that changes as an array.
So for a start all those pin mode calls could be replaced by two lines.

Just as an exercise I reduced your code to simply this:-

// Push Button to MIDI by Grumpy Mike - May 2018
// Push button wired between input and ground
#include <Bounce2.h>

const int channel = 1;
const byte pinNumbers[] = {16,20,0,4,15,19,1,5,14,18,2,6,13,17,3,7,24,11,12,23,10,9};
Bounce pushButton[22];
byte velocity = 99;

void setup() {
  for(int i = 0; i<22; i++){
    pushButton[i] = Bounce(pinNumbers[i], 5);
    pinMode(pinNumbers[i], INPUT_PULLUP); 
  }
 }

void loop() {
 for(int i = 0; i<22; i++){
  pushButton[i].update();
  if (pushButton[i].fallingEdge()) {
    usbMIDI.sendNoteOn(61+i, velocity, channel); // or change 61 to 81 for original note 
  }
  if (pushButton[i].risingEdge()) {
    usbMIDI.sendNoteOff(61+i, 0, channel); // or change 61 to 81 for original note
  }
 }
}

However, your code puzzled me. If you want a 4 by 4 matrix arrangement why have you defined 22 push button inputs?
Also the comment by the note values you send say things like "// 61 = C#4" when you actually are sending note number 81. Why is this?

Thank you, I’ll have a go with your code. I have no idea about the code I used, I’ve just copied it from a midi controller tutorial and it seems to be working. I’m hitting the arcade buttons and the notes are playing in logic DMD.

I have entered your code and nothing is happing, was it ready to go or am I supposed to add to it?

plus now when I enter the old code back in only two buttons are working? any ideas?

Joeking:
I have entered your code and nothing is happing, was it ready to go or am I supposed to add to it?

No it was working and tested.
Are you compiling it with the MIDI option selected in the tools menu?