Midi Footswitch for Guitar

katastrophe

New member
Hi guys! I'm working on a project using a Teensy to send MIDI messages when buttons are pressed and released. Additionally, the system should handle LEDs that indicate the last button pressed. While the LEDs are working correctly, I can't capture the MIDI signals on my computer using MIDI-OX or other MIDI monitoring software. I have followed the documentation to configure MIDI-OX and ensured that my code reads and discards incoming MIDI messages to avoid issues with USB buffers, but I still can't get the MIDI messages to be detected.
The main reason to do this project it's because I want to change presets on a software called Archetype: Nolly, It's a software that has all my presets.
I'm doing this project because this is the only way for me to play live


Code

#include <Bounce.h>
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const int channel = 1;
const int buttonPins[] = {2, 3, 4, 5, 20, 21, 22, 23};
const int ledPins[] = {6, 7, 8, 9, 16, 17, 18, 19};

// Configure buttons with 5 ms debounce time
Bounce buttons[8] = {
Bounce(buttonPins[0], 5),
Bounce(buttonPins[1], 5),
Bounce(buttonPins[2], 5),
Bounce(buttonPins[3], 5),
Bounce(buttonPins[4], 5),
Bounce(buttonPins[5], 5),
Bounce(buttonPins[6], 5),
Bounce(buttonPins[7], 5)
};

int lastButtonPressed = -1; // Variable to track the last button pressed

void setup() {
// Set button pins as input with internal pullup
for (int i = 0; i < 8; i++) {
pinMode(buttonPins, INPUT_PULLUP);
pinMode(ledPins, OUTPUT);
digitalWrite(ledPins, LOW); // Turn off all LEDs initially
}

// Start MIDI
MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
// Update the state of the buttons
for (int i = 0; i < 8; i++) {
buttons.update();
}

// Check if any button was pressed or released
for (int i = 0; i < 8; i++) {
if (buttons.fallingEdge()) {
MIDI.sendNoteOn(60 + i, 99, channel); // Send Note On message (60 + i is the MIDI note)
lastButtonPressed = i; // Update the last button pressed
}
if (buttons.risingEdge()) {
MIDI.sendNoteOff(60 + i, 0, channel); // Send Note Off message
}
}

// Turn on only the LED of the last button pressed and turn off the others
for (int i = 0; i < 8; i++) {
if (i == lastButtonPressed) {
digitalWrite(ledPins, HIGH);
} else {
digitalWrite(ledPins, LOW);
}
}

// Read and discard incoming MIDI messages
while (MIDI.read()) {
// read and discard incoming messages
}
}
```


I can't capture the MIDI signals on my computer. Any ideas on what might be wrong or what else I can try to resolve this issue?

I hope someone can help me please!!

I' using a Teensy 4.0
 

Attachments

  • 76c347a1-4b18-469c-a717-ecadfa676b35.jpeg
    76c347a1-4b18-469c-a717-ecadfa676b35.jpeg
    296.9 KB · Views: 20
When you #include <MIDI.h>, you are referring to the hardware MIDI ports on the Teensy, not the USB MIDI interface.
Check out this page: https://www.pjrc.com/teensy/td_midi.html.
Below is the updated code. You can use the </> button in the top-left menu to insert code like this:
C++:
// select MIDI from the "Tools > USB Type" menu

#include <Bounce.h>

const int channel = 1;
const int buttonPins[] = { 2, 3, 4, 5, 20, 21, 22, 23 };
const int ledPins[] = { 6, 7, 8, 9, 16, 17, 18, 19 };

// Configure buttons with 5 ms debounce time
Bounce buttons[8] = {
  Bounce(buttonPins[0], 5),
  Bounce(buttonPins[1], 5),
  Bounce(buttonPins[2], 5),
  Bounce(buttonPins[3], 5),
  Bounce(buttonPins[4], 5),
  Bounce(buttonPins[5], 5),
  Bounce(buttonPins[6], 5),
  Bounce(buttonPins[7], 5)
};

int lastButtonPressed = -1;  // Variable to track the last button pressed

void setup() {
  // Set button pins as input with internal pullup
  for (int i = 0; i < 8; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);  // Turn off all LEDs initially
  }
}

void loop() {
  // Update the state of the buttons
  for (int i = 0; i < 8; i++) {
    buttons[i].update();
  }

  // Check if any button was pressed or released
  for (int i = 0; i < 8; i++) {
    if (buttons[i].fallingEdge()) {
      usbMIDI.sendNoteOn(60 + i, 99, channel);  // Send Note On message (60 + i is the MIDI note)
      lastButtonPressed = i;                    // Update the last button pressed
    }
    if (buttons[i].risingEdge()) {
      usbMIDI.sendNoteOff(60 + i, 0, channel);  // Send Note Off message
    }
  }

  // Turn on only the LED of the last button pressed and turn off the others
  for (int i = 0; i < 8; i++) {
    if (i == lastButtonPressed) {
      digitalWrite(ledPins[i], HIGH);
    } else {
      digitalWrite(ledPins[i], LOW);
    }
  }

  // Read and discard incoming MIDI messages
  while (usbMIDI.read()) {
    // read and discard incoming messages
  }
}

Also corrected other compilation errors. Added indexing like so:
C++:
for (int i = 0; i < 8; i++) {
  pinMode(buttonPins[i], INPUT_PULLUP);  // add index [i]
  pinMode(ledPins[i], OUTPUT);
  digitalWrite(ledPins[i], LOW);  // Turn off all LEDs initially
}

This compiles without error, don't know ofcourse if it actually works...

Perhaps this tool is helpful to you for testing whether USB MIDI data is received on your PC.

Paul
 
I think your mistake is that you have an array of buttons but when you're going thru it on the for you didn't put the index. For example:


Code:
// Update the state of the buttons
for (int i = 0; i < 8; i++) {
buttons.update();
}

Should be

Code:
// Update the state of the buttons
for (int i = 0; i < 8; i++) {
buttons[i].update();
}

and

Code:
// Check if any button was pressed or released
for (int i = 0; i < 8; i++)
{
if (buttons.fallingEdge()) {
MIDI.sendNoteOn(60 + i, 99, channel); // Send Note On message (60 + i is the MIDI note)
lastButtonPressed = i; // Update the last button pressed
}
if (buttons.risingEdge()) {
MIDI.sendNoteOff(60 + i, 0, channel); // Send Note Off message
}
}

should be

Code:
/ Check if any button was pressed or released
for (int i = 0; i < 8; i++)
{
    if (buttons[i].fallingEdge())
    {
        MIDI.sendNoteOn(60 + i, 99, channel); // Send Note On message (60 + i is the MIDI note)
        lastButtonPressed = i; // Update the last button pressed
    }
    if (buttons[i].risingEdge())
    {
    MIDI.sendNoteOff(60 + i, 0, channel); // Send Note Off message
    }
}

Also you're calling to the MIDI instance instead of the usbMIDI
(Paul S just posted that!)
 
Back
Top