Help with incrementing midi program change message

Hi.

Super short patch i promise :slight_smile:

I am making a small arduino nano MIDI project.
The Arduino has midi in and out wired up as well as a button connected to digital pin 2.

My goal is to:

  1. Receive midi program change messages on the midi in port (DIN, not usb)
  2. Increment the received program change message by 1.
  3. Send the incremented program change message when the button is pressed via the midi out (DIN, not usb).

My problem is how to increment the received program change message. I cant seem to get the ++ to work (which is why it is not in my code below.

I would love a suggestion on how to write up the increment part of the code. So far I can send out the same value as the one received:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


void setup() {
pinMode (2, INPUT_PULLUP);
MIDI.begin(MIDI_CHANNEL_OMNI);
}


void loop() {
if (digitalRead(2) == LOW)
  if(MIDI.read())
   {
        switch(MIDI.getType())
        {
            case midi::ProgramChange:       
                MIDI.sendProgramChange(1, MIDI.getData1());
               break;
            default:
                break;
}  
}
}

Hope to hear back.

Best

Lameller

Hello

Not sure to understand your question.

MIDI.sendProgramChange(1, MIDI.getData1() + 1);

?

The post increment operator '++' adds one to a variable and then stores the result back into the variable. getData1() is a function that returns a constant value so you can not increment it and then store the new value back. Just add 1 to what is returned.

Also, your code does not really match your description. You won't read any midi messages unless your button is pressed. Is that what you want? Your description sounded more like you would read any midi message as it arrived, but only send it out (+1) when the button is pressed. If that is correct, you will have to refactor your code so you always read input but only send output on button press and only do it once.

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


void setup() {
  pinMode (2, INPUT_PULLUP);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

int value = 0;
bool wasSent = false;

void loop() {
  if (MIDI.read())
  {
    switch (MIDI.getType())
    {
      case midi::ProgramChange:
        value = MIDI.getData1() + 1;
        break;
      default:
        break;
    }

    if (digitalRead(2) == LOW && wasSent == false)
    {
      MIDI.sendProgramChange(1, value);
      wasSent = true;
    } else
    {
      wasSent = false;
    }
  }

You are absolutly right. I will try you suggestion.

Thankyou very much for taking the time to help me out

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