Need more pins with Leonardo Midi controller

Help, I need more pins from my Arduino Leonardo board.

I use the software provided in this tutorial https://www.arduino.cc/en/Tutorial/MidiDevice.
Everything works perfect, but for 7 notes only.
I would like to extend the controller to 12 keys, but I do not know how to access the other digital pins.

I am musician and I use my Leonardo at a very intuitive level.

Any help will be very much appreciated!

have a great day!

Arduino MIDI Controller Library

For example (12 buttons and 6 potentiometers):

#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 12 new instances of the class 'Digital', called 'buttons',
// on pins 0, 1, ..., 11 that send MIDI messages with notes
// 60, 61, ..., 71 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 },
};

// Create an array of 6 new instances of the class 'Analog', called 'potentiometers',
// on pins A0, A1, .., A5 that send MIDI CC messages with controller numbers 16, 17, ... 21
// on MIDI channel 1
Analog potentiometers[] = {
  { A0, 16, channel }, // potentiometer connected to pin A0, sends CC (Control Change) #16 on MIDI channel 1
  { A1, 17, channel },
  { A2, 18, channel },
  { A3, 19, channel },
  { A4, 20, channel },
  { A5, 21, channel },
};

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();
}

You can just delete the potentiometers if you don’t need them.

Pieter

1 Like

Fantastic Pieter, it works perfect, thank you very much!
Amazing job!

Helo Pieter

I wonder if there is a way to include a hold button (sustain or sustenuto), let say a switch button attached to an analog port; if pressed, the sketch above should ignore to "midi note Off".
It is possible?

Have a great day!

No, that's not possible with the sketch above. Sustain should be handled by the MIDI synthesizer/sampler, not by the MIDI controller.
It is part of the MIDI standard as CC controller 0x40 (Damper Pedal on/off (Sustain): ≤0x3F = off, ≥0x40 = on).

You could add a potentiometer (to pin A0, for example), and add the following line of code:

Analog sustain(A0, MIDI_CC::Damper_Pedal, channel);

Most synthesizers should support it. What software (or hardware?) are you using to generate the sounds?

I use Reaper, or similar.

It works very well, thank you.

I tasted also for Sostenuto, and it works also.

Many thanks PieterP, you are very generous.

There is a way to include an octave button, up or down, testing an analog pin: if pressed, to provide a different midi notes?

testing an analog pin

Why an analogue pin? Why not a digital one, or even using one of the analogue pins in digital mode?

It is very simple to do, in the main loop do:-

octave = 0;
if(digitalRead(octavePin) ) octave = 12;

Then when you send a MIDI note, just send the note plus the octave variable.

The MIDI controller library has a feature to switch between banks. (This is mainly meant for actual control surfaces that can control more tracks than physically available. That’s why the API is a little strange for changing octaves, but it’s the same principle.)

Connect a pushbutton to pin 12. By default, the normal notes will play (C3, C#3, … B3), if you press it once, the LED on pin 13 will light up, and all notes are shifted an octave higher (C4, C#4, … B4). Press it again, the LED dims, and the normal notes play again.

Code:






```
// A bank that shifts each button one octave (i.e. twelve semitones)

Bank octavecolor=#000000[/color];

// A bank selector with a momentary push button on pin 12 and an LED on pin 13
BankSelector bs(octave, 12, 13, BankSelector::MOMENTARY);

void setupcolor=#000000[/color] {
 // Add the note buttons to the bank, changing the address (i.e. note number)
 octave.add(buttons, Bank::CHANGE_ADDRESS);
}
```

|

For more options, check out the library documentation.

Pieter

Thank you very much Pieter, it works wonderful.
For playing purposes I changed “Momentary” to “Toggle”, and the octave button to provide low-octave.

The actually code looks like this:

#include <MIDI_Controller.h> // Include the library
const uint8_t velocity = 70; // Maximum velocity (0b1111111 = 0x7F = 70)
const uint8_t channel = 1; // MIDI channel 1


// Create an array of 12 new instances of the class 'Digital', called 'buttons',
// on pins 0, 1, ..., 11 that send MIDI messages with notes
// 60, 61, ..., 71 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 },
};

// Analog sustain(A0, MIDI_CC:: Damper_Pedal , channel);

// A bank that shifts each button one octave (i.e. twelve semitones)
Bank octave(-12); 

// A bank selector with a momentary push button on pin 12 and an LED on pin 13
BankSelector bs(octave, 12, 13, BankSelector::TOGGLE); 

void setup() {
  // Add the note buttons to the bank, changing the address (i.e. note number)
  octave.add(buttons, Bank::CHANGE_ADDRESS);
}
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();
}

Grumpy_Mike, many thanks to you too.

Hi Pieter

There is a problem with the octave button, some notes remains playing forever.
Could be a sketch issue?

Suppose I play a note in the low register (with the octave pedal pressed); holding that note I play other different notes in the high register, releasing the octave pedal; the low note will play forever.

I suppose, because in this case the "low bank" register will do not receive the "note off" information?
This manner of playing is very useful, playing a chord or a bass note in the low register and than the melody in the high one.

I didn't anticipate pressing buttons while switching banks. I won't be able to solve it this week.

Finnnaly.... my midi controller:

https://www.facebook.com/marianvernescu/videos/1886442331429955/

Many thanks Pieter!