MIDIUSB - How to change the note pitch with midiread?

Hello !
I'm using an Arduino micro with 4 buttons to send midi notes to control my software (Ableton Live).
I would like to send a midi message from Ableton Live to the Arduino to change the midi notes sent but i don't know if the MIDIUSB library allows it since it's declared as constants...

const byte TOTAL_BUTTONS = 4;
const byte BUTTONS_PIN[TOTAL_BUTTONS] = {2,3,4,5};
const byte BUTTONS_PITCH[TOTAL_BUTTONS] = {36,37,38,39};

I'm quite new with Arduino programing so i hope you will be able to understand my question and help me :slight_smile: Thanks !

You need to post the COMPLETE code that you're using. There's nothing in that snippet that has anything to do with reading a MIDI message. The library certainly allows reading messages from external sources.

Steve

Here is the complete code:

const byte TOTAL_BUTTONS = 4;
// All the Arduino pins used for buttons, in order.
const byte BUTTONS_PIN[TOTAL_BUTTONS] = {2,3,4,5};
// Every pitch corresponding to every Arduino pin. Each note has an associated numeric pitch (frequency scale).
// See https://github.com/arduino/tutorials/blob/master/ArduinoZeroMidi/PitchToNote.h
const byte BUTTONS_PITCH[TOTAL_BUTTONS] = {36,37,38,39};
// Current state of the pressed buttons.
byte currentRead[TOTAL_BUTTONS];
// Temporary input reads to check against current state.
byte tempRead;


void setup() {
// Initialize all the pins as a pull-up input.
  for (byte i = 0; i < TOTAL_BUTTONS; i++) {
    pinMode(BUTTONS_PIN[i], INPUT_PULLUP);
  }
  

  // initialize both leds to be off
  ledTx( false);
  ledRx( false);
}


void loop() {
for (byte i = 0; i < TOTAL_BUTTONS; i++) {
    // Get the digital state from the button pin.
    // In pull-up inputs the button logic is inverted (HIGH is not pressed, LOW is pressed).
    byte buttonState = digitalRead(BUTTONS_PIN[i]);
    // Temporarily store the digital state.
    tempRead = buttonState;
    // Continue only if the last state is different to the current state.
    if (currentRead[i] != tempRead) {
      // See https://www.arduino.cc/en/pmwiki.php?n=Tutorial/Debounce
      delay(2);
      // Get the pitch mapped to the pressed button.
      byte pitch = BUTTONS_PITCH[i];
      // Save the new input state.
      currentRead[i] = tempRead;
      // Execute note on or noted off depending on the button state.
      if (buttonState == LOW) {
        noteOn(pitch);
      } else {
        noteOff(pitch);
      }
    }
  }
}

void noteOn(byte pitch) {
  MidiUSB.sendMIDI({0x09, 0x90 | 2, pitch, 127}); //envoie la note sur le channel MIDI 3
  MidiUSB.flush();
}

void noteOff(byte pitch) {
  MidiUSB.sendMIDI({0x08, 0x80 | 2, pitch, 0});
  MidiUSB.flush();
}

No, that is not the complete code. It doesn't compile because it doesn't include the MidiUSB library you are using and is missing other definitions.

But I'll guess which library it is and If you look in the IDE examples for MIDIUSB you'll find MIDIUSB_read and MIDIUSB_buzzer both of which give examples of reading data and doing something with it. See if they give you any ideas.

Steve

Sorry, it was a copy and paste mistake. The button box is actually working as it is. My question is how to change the notes that the arduino send when Ableton Live is sending a Program Change.

Here is the full code

#include "MIDIUSB.h"

const byte TOTAL_BUTTONS = 4;
// All the Arduino pins used for buttons, in order.
const byte BUTTONS_PIN[TOTAL_BUTTONS] = {2,3,4,5};
// Every pitch corresponding to every Arduino pin. Each note has an associated numeric pitch (frequency scale).
// See https://github.com/arduino/tutorials/blob/master/ArduinoZeroMidi/PitchToNote.h
const byte BUTTONS_PITCH[TOTAL_BUTTONS] = {36,37,38,39};
// Current state of the pressed buttons.
byte currentRead[TOTAL_BUTTONS];
// Temporary input reads to check against current state.
byte tempRead;


void setup() {
// Initialize all the pins as a pull-up input.
  for (byte i = 0; i < TOTAL_BUTTONS; i++) {
    pinMode(BUTTONS_PIN[i], INPUT_PULLUP);
  }
  

  // initialize both leds to be off
  ledTx( false);
  ledRx( false);
}


void loop() {
for (byte i = 0; i < TOTAL_BUTTONS; i++) {
    // Get the digital state from the button pin.
    // In pull-up inputs the button logic is inverted (HIGH is not pressed, LOW is pressed).
    byte buttonState = digitalRead(BUTTONS_PIN[i]);
    // Temporarily store the digital state.
    tempRead = buttonState;
    // Continue only if the last state is different to the current state.
    if (currentRead[i] != tempRead) {
      // See https://www.arduino.cc/en/pmwiki.php?n=Tutorial/Debounce
      delay(2);
      // Get the pitch mapped to the pressed button.
      byte pitch = BUTTONS_PITCH[i];
      // Save the new input state.
      currentRead[i] = tempRead;
      // Execute note on or noted off depending on the button state.
      if (buttonState == LOW) {
        noteOn(pitch);
      } else {
        noteOff(pitch);
      }
    }
  }
}

void noteOn(byte pitch) {
  MidiUSB.sendMIDI({0x09, 0x90 | 2, pitch, 127}); //envoie la note sur le channel MIDI 3
  MidiUSB.flush();
}

void noteOff(byte pitch) {
  MidiUSB.sendMIDI({0x08, 0x80 | 2, pitch, 0});
  MidiUSB.flush();
}

O.k. Read the incoming PC using code from the examples I gave you in my last post. Remember it. Then apply whatever changes you want depending on the PC value. You haven't said what these changes are so I can't help there.

BTW that code STILL doesn't compile. ledTx and ledRx which are apparently functions are undefined. If you have working code just use Edit/Copy for Forum in the IDE and paste the results into post here.

Steve

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