Adding leds to follow generated MIDI notes

i need to add instructions to this sketch to use a led to follow each note when i press a switch and stay on until i press another switch to get another note and so on

#include <MIDI_Controller.h> // Include the library



const uint8_t velocity = 0b1111111; // Maximum velocity (0b1111111 = 0x7F = 127)
const uint8_t channel = 1; // MIDI channel 1

// Create an array of 19 new instances of the class 'Digital', called 'buttons',
// on pins 0, 1, ..., 11, 12, 13, A1, A2, A3, A4, A5 that send MIDI messages with notes
// 60, 61, ..., 71, 72, 73, 74, 75, 76, 77, 78 on MIDI channel 1, with maximum velocity (127)
Digital buttons[] = {
  { 0, 60, channel, velocity }, // button connected to pin 0, sends MIDI note 60 (C3) on channel 1 with velocity 127
  { 1, 61, channel, velocity },
  { 2, 62, channel, velocity },
  { 3, 63, channel, velocity },
  { 4, 64, channel, velocity },
  { 5, 65, channel, velocity },
  { 6, 66, channel, velocity },
  { 7, 67, channel, velocity },
  { 8, 68, channel, velocity },
  { 9, 69, channel, velocity },
  {10, 70, channel, velocity },
  {11, 71, channel, velocity },
  {12, 72, channel, velocity },
  {13, 73, channel, velocity },
  {A1, 74, channel, velocity },
  {A2, 75, channel, velocity },
  {A3, 76, channel, velocity },
  {A4, 77, channel, velocity },
  {A5, 78, channel, velocity },
};

// Create an array of 1 new instances of the class 'Analog', called 'potentiometers',
// on pins A0 that send MIDI CC messages with controller numbers 16
// on MIDI channel 1
Analog potentiometers[] = {
  { A0, 16, channel }, // potentiometer connected to pin A0, sends CC (Control Change) #16 on MIDI channel 1

};

void setup() {} // nothing to set up

void loop() { // Refresh the buttons and potentiometers (check whether a button's state or a potentiometer's position has changed since last time, if so, send it over MIDI)
  MIDI_Controller.refresh();

}

Doc for the library states

The MIDI Controller library has been obsoleted by its successor, the Control Surface library.

It has more features, a better architecture, unit tests, continuous integration, clearer documentation, support for more and newer Arduino boards, etc.
It adds support for MIDI input, SysEx, the Mackie Control Universal protocol, VU meters, LEDs, OLED displays, audio over USB, MIDI over Bluetooth, MIDI routing ...

GitHub - tttapa/Control-Surface: Arduino library for creating MIDI controllers and other MIDI devices.

also see this report

➜ the Control Surface library supports callbacks and you can also iterate over the buttons etc

I would suggest you move your code to the better library.

1 Like

I would switch the buttons from 19 individual pins to a 4x5 (20 button) matrix using 9 pins instead of 19. That will free up 10 pins for other uses. If you use addressable LEDs you only need one output pin.

1 Like

thanks my friend for your reply.
i just need to modify the sketch to light a le following each note.
when i press a button for note 60 led 1 light and stay lighted and when i press note 61 led 2 light and led 1 goes off and so on
can you help me to add this command to my sketch
thanks in advance

You need to change your library. Look at the new one

Control Surface's NoteButton class has a getButtonState() method.
Loop over all the buttons, and check if their button state is equal to Button::Falling. If so, turn off the previous LED and turn of the one that corresponds to this button.

1 Like