Arduino midi porblem with francis best not being recognised leonardo usb midi skript

hi here's a script i've been working on using chat gpt keep getting an midi library error even though the library is installed and have tried all versions of the library this script uses MIDI.h by frances best.

i'm wanting a native usb midi device that will use latching machanical switches as i have quite a few of them knocking around so asked gpt to do edge detection i also wanted the abilty to use combinations of two switches at once to have more cc messages posible from the 6 switches i'm using so thats in there to with some delay for the dual presses to allow for timing issues with dual presses not being exactly at the same time i add slao debounce and pots with a change state to store current state and check against that before sending any cc changes to avoid any constant cc codes being sent any way here is the code and error if anyone can help.

the board is a pro micro which comes up as a leonardo not sure if this is an issue or not

#include <MIDI.h>

const int numPotentiometers = 5;
const int numSwitches = 6;
const int numCombinations = 2;

const int potentiometerPins[numPotentiometers] = {A0, A1, A2, A3, A4};  // Analog input pins for potentiometers
const int switchPins[numSwitches] = {2, 3, 4, 5, 6, 7};  // Digital input pins for switches

const int combinations[numCombinations][2] = {{2, 3}, {4, 5}};  // Switch combinations
const int ccCombination[numCombinations] = {10, 11};  // CC numbers for each combination

const int debounceDelay = 50;  // Debounce delay in milliseconds

int potentiometerValues[numPotentiometers];
int switchStates[numSwitches];
int lastSwitchStates[numSwitches];

void setup() {
  MIDI.begin();

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

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

void loop() {
  // Read potentiometer values
  for (int i = 0; i < numPotentiometers; i++) {
    int value = analogRead(potentiometerPins[i]) / 8;
    if (value != potentiometerValues[i]) {
      potentiometerValues[i] = value;
      MIDI.sendControlChange(i, value, 1);
    }
  }

  // Read switch states with edge detection and debounce
  for (int i = 0; i < numSwitches; i++) {
    int state = digitalRead(switchPins[i]);
    if (state != lastSwitchStates[i]) {
      delay(debounceDelay);
      state = digitalRead(switchPins[i]);
      if (state != lastSwitchStates[i]) {
        switchStates[i] = state;
        MIDI.sendControlChange(i + numPotentiometers, state, 1);
        handleSwitchStateChange(i);
      }
    }
    lastSwitchStates[i] = state;
  }
}

void handleSwitchStateChange(int switchIndex) {
  int switchState = switchStates[switchIndex];
  for (int i = 0; i < numCombinations; i++) {
    if (switchPins[switchIndex] == combinations[i][0] || switchPins[switchIndex] == combinations[i][1]) {
      MIDI.sendControlChange(ccCombination[i], switchState, 1);
      break;
    }
  }
}


and the error in this code box

C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino: In function 'void setup()':
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:20:3: error: 'MIDI' was not declared in this scope
   MIDI.begin();
   ^~~~
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:20:3: note: suggested alternative: 'MISO'
   MIDI.begin();
   ^~~~
   MISO
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino: In function 'void loop()':
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:38:7: error: 'MIDI' was not declared in this scope
       MIDI.sendControlChange(i, value, 1);
       ^~~~
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:38:7: note: suggested alternative: 'MISO'
       MIDI.sendControlChange(i, value, 1);
       ^~~~
       MISO
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:50:9: error: 'MIDI' was not declared in this scope
         MIDI.sendControlChange(i + numPotentiometers, state, 1);
         ^~~~
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:50:9: note: suggested alternative: 'MISO'
         MIDI.sendControlChange(i + numPotentiometers, state, 1);
         ^~~~
         MISO
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino: In function 'void handleSwitchStateChange(int)':
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:62:7: error: 'MIDI' was not declared in this scope
       MIDI.sendControlChange(ccCombination[i], switchState, 1);
       ^~~~
C:\Users\pc\Documents\Arduino\midipedal\midipedal.ino:62:7: note: suggested alternative: 'MISO'
       MIDI.sendControlChange(ccCombination[i], switchState, 1);
       ^~~~
       MISO

exit status 1

Compilation error: 'MIDI' was not declared in this scope

any help welcome i'm not a coder but ahve made a few of these midi controller using other peoples scripts so understand what i need in one just can't seem to get this one working

tryed againg chat gpt reprdouced the code which works now but get this on upload any help no idea

onnecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44


hi guys resolved thias using chat gpt on my own pretty prud of myself the gpt code was addressing the wrong libray altogether for the leonardo board it should be midiusb and is adrressed with totally different functions thaks if you were reading this heres the code i ended up with if your trying to do somthing suimilar using latching switches haven't tested it as i bricked my pro micro board and waiting on a new one hopfully this will have the functions i need added a cc length as the switch are on so short they dont register a cc code on most devices and sorted the pots to debounce aswell as save and work on state change to avoid jitter the cc length can be set along with the deounce for pots and switches separately as the pots need a differnt delay time to to be set to get a smooth oparation so that you don't end up moving the pot too far before registering a move cos of the debounce. heres the code.

#include <MIDIUSB.h>

// MIDIUSB functions

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

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

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

// Potentiometer variables
const int numPotentiometers = 5;
const int potentiometerPins[numPotentiometers] = {A0, A1, A2, A3, A10};
const byte potentiometerCCNumbers[numPotentiometers] = {25, 26, 27, 28, 29};
const int potentiometerDebounceDelay = 10;

// Switch variables
const int numSwitches = 6;
const int switchPins[numSwitches] = {2, 3, 4, 5, 6, 7};
const byte switchCCNumbers[numSwitches] = {19, 20, 21, 22, 23, 24};
const int switchDebounceDelay = 5;             // Adjust debounce delay as needed
const int switchCCSignalLength = 100;           // Length of CC signal in milliseconds

// Combination variables
const int numCombinations = 5;
const int combinationPins[numCombinations][2] = {{2, 3}, {4, 5}, {6, 7}, {2, 4}, {3, 5}};
const byte combinationCCNumbers[numCombinations] = {14, 15, 16, 17, 18};
const int combinationDebounceDelay = 0;        // Adjust debounce delay as needed
const int combinationPressDelay = 100;          // Delay to check for combination press
const int combinationCCSignalLength = 150;      // Length of CC signal in milliseconds

byte switchStates[numSwitches] = {0};
byte previousSwitchStates[numSwitches] = {0};
byte combinationStates[numCombinations] = {0};
byte previousCombinationStates[numCombinations] = {0};
byte potentiometerStates[numPotentiometers] = {0};
byte previousPotentiometerStates[numPotentiometers] = {0};
unsigned long combinationPressTime[numCombinations] = {0};

void readPotentiometerValues() {
  for (int i = 0; i < numPotentiometers; i++) {
    int value = analogRead(potentiometerPins[i]);
    if (abs(value - potentiometerStates[i]) > 1) {
      delay(potentiometerDebounceDelay); // Debounce the potentiometer reading
      value = analogRead(potentiometerPins[i]); // Read the value again after debounce
      if (abs(value - potentiometerStates[i]) > 1) {
        potentiometerStates[i] = value;
        controlChange(0, potentiometerCCNumbers[i], map(value, 0, 1023, 0, 127));
      }
    }
  }
}

void readSwitchStates() {
  for (int i = 0; i < numSwitches; i++) {
    byte state = digitalRead(switchPins[i]);
    if (state != previousSwitchStates[i]) {
      delayMicroseconds(switchDebounceDelay); // Debounce the switch state
      state = digitalRead(switchPins[i]); // Read the state again after debounce
      if (state != previousSwitchStates[i]) {
        switchStates[i] = state;
        if (state == LOW) {
          controlChange(0, switchCCNumbers[i], 127); // Momentary high on noteOn
          delay(switchCCSignalLength); // CC signal length delay
          controlChange(0, switchCCNumbers[i], 0); // Low on noteOff
        }
      }
    }
    previousSwitchStates[i] = state;
  }
}

void readCombinationStates() {
  for (int i = 0; i < numCombinations; i++) {
    byte switch1 = digitalRead(combinationPins[i][0]);
    byte switch2 = digitalRead(combinationPins[i][1]);
    byte combinationState = (switch1 == LOW && switch2 == LOW) ? 1 : 0;

    if (combinationState != previousCombinationStates[i]) {
      delayMicroseconds(combinationDebounceDelay); // Debounce the combination state
      switch1 = digitalRead(combinationPins[i][0]); // Read the states again after debounce
      switch2 = digitalRead(combinationPins[i][1]);
      combinationState = (switch1 == LOW && switch2 == LOW) ? 1 : 0;

      if (combinationState != previousCombinationStates[i]) {
        combinationStates[i] = combinationState;
        if (combinationState == 1) {
          combinationPressTime[i] = millis();
          controlChange(0, combinationCCNumbers[i], 127); // Momentary high on noteOn
          delay(combinationCCSignalLength); // CC signal length delay
          if (millis() - combinationPressTime[i] <= combinationPressDelay) {
            controlChange(0, combinationCCNumbers[i], 127); // Momentary high on noteOn
          } else {
            controlChange(0, combinationCCNumbers[i], 0); // Low on noteOff
          }
        }
      }
    }
    previousCombinationStates[i] = combinationState;
  }
}

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

  for (int i = 0; i < numCombinations; i++) {
    pinMode(combinationPins[i][0], INPUT_PULLUP);
    pinMode(combinationPins[i][1], INPUT_PULLUP);
  }
}

void loop() {
  readPotentiometerValues();
  readSwitchStates();
  readCombinationStates();
}

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