Help midi usb leonardo mid deivce need support with code

I'm using a pro micro board to run this sketch.
I am currently utilizing the MIDIUSB library.

Here is a description of my schematic.

all the switches for the main switches are connected to pin's 2,3,5,6,7.
with the ground connected to the centre pin and the main pin connected to one side of the switch,
the bank switch is connected in the same manner to pin 4,

the pots are connected right to left from the back. "I'm not great at remembering which pin numbers which." #
so right to left for the back
left=ground centre=analog pin, right=vcc

the analog pins are A0,A1,A2,A3,A10,

I'll quickly explain its functions.
It has five mechanical switches to send cc messages as momentary commands. The cc messages. need to work on rising and falling edges to trigger a single momentary cc message. which should be at the value of 127 value else 0.
The five switches should work with a bank switch of which, at the moment id like 2 of A and B. Operated with a separate 6ith, banks switch. This is also a mechanical switch, which id like to trigger on edge detection. I would like to have a midi cc code sent on that switch also but Gpt seems to not quite get what I'm after. this is so it could do double duty if needed.
Then there are five potentiometers which are to bank the cc messages also sending different cc codes depending on banks.
I've tried to add an LED on pin 8 to be set to high on bank A, I also set the onboard led to flash on bank A. This doesn't appear to work properly for some reason not sure what to do to fix it.

On to my problems with my code

I have the board flashed with my code but have major issues with the midi device. The device shows up but I can't seem to get anything out of the switches re cc messages.
I tried adding in a midi cc message length to the code, to try to make sure messages would get through.

I have also tried to play around with pot thresholds and debounce delays to fix my issues with this.

The pots only register every so often and the switches don't appear to work at all. Can someone help me fix this code?

I've used chat gpt to write this code thus far.
I understand a small amount about setting up these devices as I have done one before with a lot of help, but couldn't write the code myself.
I know just enough to be dangerous as they say.

Please help here's the code can someone look at it? To see if they can see any problems with the setup of the switches,
and generally help me get the whole thing working.

I know don't know if the code is not set up right to send cc messages in the way I've described on the switches or if it's something with the debounce and other things. Please help me to understand if it's something wrong with the code, and or fix it for me. I don't have a clue at the moment.

#include <MIDIUSB.h>

// MIDIUSB functions

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, static_cast<uint8_t>(0x90 | channel), pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, static_cast<uint8_t>(0x80 | channel), pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

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

// Potentiometer variables
const int numPotentiometers = 5;
const int potentiometerPins[numPotentiometers] = {A0, A1, A2, A3, A10};
const byte potentiometerCCNumbersA[numPotentiometers] = {25, 26, 27, 28, 29};
const byte potentiometerCCNumbersB[numPotentiometers] = {35, 36, 37, 38, 39};
const int potentiometerDebounceDelay = 0;
const float potentiometerMovementThreshold = 6; // Minimum movement threshold as a percentage
const int potentiometerDelayTime = 30; // Delay time in milliseconds for sending CC message

// Switch variables
const int numSwitches = 5;
const int switchPins[numSwitches] = {2, 3, 5, 6, 7};
const byte switchCCNumbersA[numSwitches] = {19, 20, 21, 22, 23};
const byte switchCCNumbersB[numSwitches] = {30, 31, 32, 33, 34};
const int switchDebounceDelay = 0;  // Adjust debounce delay as needed

// Bank switch variables
const int bankSwitchPin = 4;  // Pin for the bank switch
const byte switchCCSignalLength = 500;  // CC signal length (max value for a momentary CC message)
const byte bankLED = 8;  // Pin for the bank LED

byte switchStates[numSwitches] = {0};
byte previousSwitchStates[numSwitches] = {0};
int potentiometerStates[numPotentiometers] = {0};
int previousPotentiometerStates[numPotentiometers] = {0};
byte bankState = LOW;
byte previousBankState = LOW;

void readPotentiometerValues() {
  for (int i = 0; i < numPotentiometers; i++) {
    int value = analogRead(potentiometerPins[i]);
    float movementPercentage = abs(value - previousPotentiometerStates[i]) / 1023.0;
    if (movementPercentage >= potentiometerMovementThreshold) {
      delay(potentiometerDebounceDelay); // Debounce the potentiometer reading
      value = analogRead(potentiometerPins[i]); // Read the value again after debounce
      movementPercentage = abs(value - previousPotentiometerStates[i]) / 1023.0;
      if (movementPercentage >= potentiometerMovementThreshold) {
        previousPotentiometerStates[i] = value;
        if (digitalRead(bankSwitchPin) == HIGH) {
          controlChange(0, potentiometerCCNumbersA[i], map(value, 0, 1023, 0, 127), switchCCSignalLength);
        } else {
          controlChange(0, potentiometerCCNumbersB[i], map(value, 0, 1023, 0, 127), switchCCSignalLength);
        }
        delay(potentiometerDelayTime);
      }
    }
  }
}

void readSwitchStates() {
  for (int i = 0; i < numSwitches; i++) {
    byte state = digitalRead(switchPins[i]);
    if (state != previousSwitchStates[i]) {
      delay(switchDebounceDelay);  // Debounce the switch reading
      state = digitalRead(switchPins[i]);  // Read the state again after debounce
      if (state != previousSwitchStates[i]) {
        // High on rising edge
        if (state == HIGH) {
          if (digitalRead(bankSwitchPin) == HIGH) {
            controlChange(0, switchCCNumbersA[i], 127, switchCCSignalLength);
          } else {
            controlChange(0, switchCCNumbersB[i], 127, switchCCSignalLength);
          }
        }
        // Low on falling edge
        else {
          if (digitalRead(bankSwitchPin) == HIGH) {
            controlChange(0, switchCCNumbersA[i], 0, switchCCSignalLength);
          } else {
            controlChange(0, switchCCNumbersB[i], 0, switchCCSignalLength);
          }
        }
        switchStates[i] = state;
      }
    }
    previousSwitchStates[i] = state;
  }
}

void readBankSwitchState() {
  byte state = digitalRead(bankSwitchPin);
  if (state != previousBankState) {
    delay(switchDebounceDelay);  // Debounce the switch reading
    state = digitalRead(bankSwitchPin);  // Read the state again after debounce
    if (state != previousBankState) {
      if (state == HIGH) {
        controlChange(0, 35, 127, switchCCSignalLength);  // Send CC message on bank switch rising edge
      } else {
        controlChange(0, 35, 0, switchCCSignalLength);  // Send CC message on bank switch falling edge
      }
      previousBankState = state;
    }
  }
}

void setup() {
  for (int i = 0; i < numPotentiometers; i++) {
    pinMode(potentiometerPins[i], INPUT);
  }

  for (int i = 0; i < numSwitches; i++) {
    pinMode(switchPins[i], INPUT_PULLUP);
  }

  pinMode(bankSwitchPin, INPUT_PULLUP);
  pinMode(bankLED, OUTPUT);
}

void loop() {
  readPotentiometerValues();
  readSwitchStates();
  readBankSwitchState();

  // Update bank LED
  if (digitalRead(bankSwitchPin) == HIGH) {
    digitalWrite(bankLED, HIGH);
  } else {
    digitalWrite(bankLED, LOW);
  }
}

Could You please learn the use of sentences? A sentence starts with a capital letter and ends with a dot. This maraton text posted exceeds my energies right now at this late hour.

Where is the schematics of all this? Where are the links To none Arduino devices?

Please read and consider this link: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

1 Like

sorry about that i'm dyslexic and struggle with writting. sorry again i'm just needing help on my project.

Okey. What about the rest of reply #2?

I'm sorry, I'm not sure what you mean. I will attempt to clarify my original post with assistance from Grammarly to improve its clarity. Hopefully, this will help. Once again, I apologize for any confusion.
I used it for this never used it before.

Ok @danny1m31 I am dyslexic myself, no worries.

However, what you need to do is to give us some details. We know you are using a Leonardo from the title, but I think it needs mentioning in the body of the reply as well.

The main thing you are missing is a schematic, this is vital in any question involving hardware. It allows us to see what your code is trying to do and how it interacts with the hardware. This is a vital first step.

Not the best idea in the world.
Is the code you posted the one that works or does not work?

We are best seeing the one that doesn't work.

As for not knowing about punctuation, here is a tip. Break up any long passage of words longer than three or four lines with a double press of the enter key. This breaks up the text and makes it much easer to read.

I was, for quite some time, a University Lecturer (Called Professor in some parts of the world, but in the UK it means something else). I was often sent students from other departments to advise them about coping with Dyslexia. My best advice is :- Never play Scrabble for money.

for now it appears as if all your debounce is done with delay() and this may work if you are dealing with a single button, but not for multiple inputs. You will have to use millis() to debounce every input individually.

Hello, I have revised my original post to provide more clarity. I am currently using a Pro Micro board, although I mistakenly referred to it as a Leonardo, which is the official version displayed in the Arduino software upon connection.

If needed, I can provide a schematic graphic, but for now, here is a text description of my layout.

all the switches for the main switches are connected to pin's 2,3,5,6,7.
with the ground connected to the centre pin and the main pin connected to one side of the switch,
the bank switch is connected in the same manner to pin 4, edit "forgot the pots."
the pots are connected right to left from the back. "I'm not great at remembering which pin numbers which." #
so right to left for the back
left=ground centre=analog pin, right=vcc

the analog pins are A0,A1,A2,A3,A10,

We only can possibly know what you tell us.
As for a description it is virtually imposable to follow.
For example

Are these throw switches? That is not a push button switch?
If so these are the wrong sort to use, and given the time delay between reading them debouncing is not needed.

There is a lot of delay between how often you read any one set of inputs.
Reading the pots is also a little Baroque, that is unnecessarily complex. Take this bit:-

There is no need to calculate a movement percentage just use the numbers returned themselves.

movement = abs(value - previousPotentiometerStates[i]) ;
      if (movement >= potentiometerMovementThreshold) {

With potentiometerMovementThreshold being something like 6.
There is no need to have any potentiometerDebounceDelay, or to re-read the pots after this delay because pots do not bounce. Also there is no need for a delay at the end of the function.

The same goes for your other functions that read inputs. Do not re-read the state after a debounce delay.

So try re-writing the readPotentiometerValues function with those points in mind, and post your attempt and we can see if you have done it correctly.

Using chat gpt and then not being able to understand the results, is a danger. Especially as chat gpt appearers not to be very intelligent in the first place. We used to say, back at the University, that artificial intelligence is what students displayed in their exam scripts.

A schematic would really help but a poos second best is to post some good quality pictures.

Hi, thanks for the reply. just to clarify, they are latching footswitches DPDT.

I realise these are not topical but I have quite a lot of them. as got into building effects pedals in lockdown.

Do I need to remove the debounce for this switch?

I assumed that this type of switch could still detect a change in the state through edge detection and trigger the necessary response.

Just to confirm, would I also take out all debounce and turn delays?

Then just rely on the turn threshold which should be set to read the numbers outputted by the cc and use that as the read for threshold?

Would the bank switch be set without any debounce? I would want edge detection for a cc code on press.
Hello, can you provide information on how to set up the on-board LED to flash for approximately 300ms? I would like to use it as an additional indicator for the bank I am currently on. Thank you for your assistance.

Let's see later, for the moment just have the loop function as

and just concentrate on the pots and getting them right and working.

The instruction to what I have just done would be :- "comment out everything but the readPotentiometerValues call". Would you have been able to do that if I just gave the comment?

I have created a circuit design using TinkerCAD. Unfortunately, the platform does not have a pro micro board, so I suggest using the UNO instead. The pin numbers should be the same as the pro micro board, and if there are any discrepancies, please note that A10 corresponds to pin 10 on the pro micro.

Thanks, for your replies got to go out tonight but will look into anything tomorrow hope the schematic helps sorry if it's a bit messy.

That is not a schematic, although it does clarify things.
I used to just use windows paint and created a lot of the symbols. Now many times i use Kicad which i anyway use for designing PCB's, but a quick google showed me circuit-diagram which has a web editor. and will make things so much easier to read for all of us. Unfortunately no 'micro' available, but a leonardo or even an UNO would work just as well. Main thing is this will show us an actual circuit schematic rather than a drawing of wires and parts.
So please use circuit diagram or a method / program which will result into something similar.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.