Midi Controller

I've built several Arduino-based midi controllers to control my amps and effects. I am making a new one and for the life of me can't seem to get it to work. Here's the code:

#include <Bounce2.h>
#include <MIDI.h>

// Initialize MIDI on Serial (pin 1 for TX)
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);

// Define button pins
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};// Amp models 1,3,7,5 - clean, slight, mod heavy, heavy - 2,3,4,5 Effects Mute, Boost, Phasor, Flang, Tremolo, Harmon - 6,7,8,9,10,11
// Define LED pins
const int leds[] = {12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; // Amp models 1,3,7,5 - clean, slight, mod heavy, heavy - 12,13,14,15 Effects Mute, Boost, Phasor, Flang, Tremolo, Harmon - 16, 17, 18, 19, 20, 21

// Define MIDI messages for each button
const byte midiCC1[] = {1, 3, 7, 5, 7, 25, 19, 19, 19, 19}; // Mute, Boost, Phasor, Flanger, Tremolo, Harmonizer - button 6,7,8,9,10,11
const byte midiCC2[] = {1, 3, 7, 5, 7, 25, 58, 58, 58, 103}; // Mute, Boost, Phasor, Flanger, Tremolo, Harmonizer - button 6,7,8,9,10,11
const byte midiCC3[] = {1, 3, 7, 5, 7, 25, 28, 28, 28, 28}; // Mute, Boost, Phasor, Flanger, Tremolo, Harmonizer - button 6,7,8,9,10,11
const byte midiCC4[] = {1, 3, 7, 5, 7, 25, 34, 34, 34, 34}; // Mute, Boost, Phasor, Flanger, Tremolo, Harmonizer - button 6,7,8,9,10,11

const byte midiNote1[] = {1, 3, 7, 5, 127, 50, 1, 1, 1, 2}; // Amp models 1,3,7,5 - clean, slight, mod heavy, heavy - button 2,3,4,5
const byte midiNote2[] = {1, 3, 7, 5, 127, 50, 2, 3, 1, 2}; // Amp models 1,3,7,5 - clean, slight, mod heavy, heavy - button 2,3,4,5
const byte midiNote3[] = {1, 3, 7, 5, 127, 50, 127, 127, 127, 127}; // Amp models 1,3,7,5 - clean, slight, mod heavy, heavy - button 2,3,4,5
const byte midiNote4[] = {1, 3, 7, 5, 127, 50, 70, 79, 70, 30}; // Amp models 1,3,7,5 - clean, slight, mod heavy, heavy - button 2,3,4,5

byte midiToggle;
// Rename the Bounce array to avoid conflict
Bounce debouncers[10];

void setup() {
  for (int i = 0; i < 10; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    debouncers[i].attach(buttonPins[i]);
    debouncers[i].interval(40);
    pinMode(leds[i], OUTPUT);
    digitalWrite(leds[i], LOW);  // Turn all LEDs off initially
  }
  // Begin MIDI communication on Serial
  Serial.begin(31250); // Set Serial baud rate for MIDI
  MIDI.begin(1);
  
 
}

void loop() {
  for (int i = 0; i < 10; i++) {
    debouncers[i].update();  // Update the debouncer state
    if (debouncers[i].fell()) {  // Button was just pressed
      // If a button in group 1 (2,3,4,5) is pressed, turn off all LEDs in group 2 (6,7,8,9,10,11)
      if (i < 5) {
        for (int j = 5; j < 10; j++) {
          digitalWrite(leds[j], LOW);
        }
      }
      
      // Turn off all LEDs in the group of the pressed button
      if (i < 5) {
        for (int j = 0; j < 5; j++) {
          digitalWrite(leds[j], LOW);
        }
      } else {
        for (int j = 5; j < 10; j++) {
          digitalWrite(leds[j], LOW);
        }
      }
      
      // Turn on the LED for the pressed button
      digitalWrite(leds[i], HIGH);
      // Send MIDI messages
      MIDI.sendProgramChange(midiCC1[i], 1);  

      //MIDI.sendControlChange(midiCC1[i], 127, 1);
      //MIDI.sendControlChange(midiCC2[i], 127, 1);
      //MIDI.sendControlChange(midiCC3[i], 127, 1);
      //MIDI.sendControlChange(midiCC4[i], 127, 1);

      //MIDI.sendNoteOn(midiNote1[i], 127, 1);
      //MIDI.sendNoteOn(midiNote2[i], 127, 1);
      //MIDI.sendNoteOn(midiNote3[i], 127, 1);
      //MIDI.sendNoteOn(midiNote4[i], 127, 1);
      // removed this line with debounce feature added "delay(100);  // Original Debounce delay
    }
  }
}

I have commented out all but the most essential lines that control the midi messages. At this point, the code is super similar to another controller I made using a Mega. Now I'm using a Due.

Serial off pin 1.

I just need to send a midi PC of 1, 3, 5, or 7 (picks the amp model on my guitar ampler). Once that is working, I can focus on the effects information.

Any help would be really appreciated! Thanks!

did you tested example sketch? do you receive the notes?

equal to

        for (int j = 0; j < 10; j++) {
          digitalWrite(leds[j], LOW);
        }
               
      // Turn on the LED for the pressed button
      digitalWrite(leds[i], HIGH);

How have you wired things up ? Unlike the Mega, which uses 5v logic, the Due uses 3.3v logic.

I have two groups of buttons that are treated differently, so I needed two loops.

The sketch works, but I am not receiving notes via the 5-pin din.

Buttons and led logic work perfectly.

Midi out (tx) is connected to pin 1 and ground.

That is still ambiguous. You would have had 220R resistors on your Mega setup.

Post a full schematic please (hand-drawn is fine)
There are versions of MIDI which do use 3.3v, but they are not so safe in case of accidental shorting of wires and pins. Therefore a TTL chip is recommended. The original specification uses Schmitt - trigger gates, but basically any 7400 series gate will do the trick since we start out with a true digital signal. I use 74HCT04 and run it through 2 of the gates, but a 74HCT14 (Schmitt-trigger not-gate is preferred)

There is a +5v pin on the Due.

But the GPIO pins do not accept 5v also not sinking it. The opto-coupler at the other end may have enough voltage drop to get within specs, but somehow i doubt it. You do not want to damage your board.

I’m confused. I’m only sending data. Not receiving.

your sketch uses USB MIDI library, it is not suitable for simple serial MIDI.
suitable is this example:

// connect pin 18 on Arduino Due to 10 Ohm resistor and its other side to pin 5 of 5pin DIN female connector

#define Midi Serial1

void setup() {
  // Set MIDI baud rate:
  Midi.begin(31250);
}

void loop() {
  // play notes from F#-0 (0x1E) to F#-5 (0x5A):
  for (int note = 0x1E; note < 0x5A; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    note(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    note(0x90, note, 0x00);
    delay(100);
  }
}

// plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that
// data values are less than 127:
void note(int cmd, int pitch, int velocity) {
  Midi.write(cmd);
  Midi.write(pitch);
  Midi.write(velocity);
}

for 3.3V board used resistors are 10 and 33 Ohm

show your schematic

Keep in mind that protection against accidental short-circuit is not to good with those values.

So I was able to get it to work perfectly. Had to use to serial commands to send the midi messages. Appreciate everyone's help.