Midi messages not sending correctly.

So after a frustrating day i feel i have made some headway on my project.

I have a button triggering a pin on my arduino board along with code to blink the on board led when it is pressed and released. this is working i see the led blinking.. on and off.

this is the pin out for my style of midi.

Here is the if statement that initiates the button press.

if (debouncedState == HIGH) {
        digitalWrite(13, 1 );

        CMDToChange = 128;
        PadToChange = pn;
        StateToChange = 1;
buttonClicked('22');
        Serial.println("pressed");
      } else {

        digitalWrite(13, 0 );
CMDToChange = 144;
          PadToChange = pn;
          StateToChange = 1;
          buttonClicked('22');
        Serial.println("released");
      }

Here are more variables that i would like to include to later on effect aftertouch and things of that nature within the midi message. right now Im trying to focus on getting a single note through first. The values are generic as they are always changing as the midi interface is used.

int PinToChange;
int StateToChange;
int PadToChange;
char PadValue;
long CMDToChange = 144;
int CMDValue = 1;
int NoteToChange = 50;
int NoteValue = 1;
int VelToChange;
int VelValue = 1;
char TrackToChange;
int TrackValue = 1;
char BarsToChange;
int BarsValue = 1;
char LengthToChange;
int LengthValue = 1;
char ActBarToChange;
int ActBarValue = 1;
char BankToChange;
int BankValue = 1;

basically when it comes down to these lines something is amiss.

void changeAllStates() {

    noteOn( CMDToChange, NoteToChange, VelToChange);

}

void noteOn(int cmd, int pitch, int velocity) {

  //Serial.print(cmd);
  mySerial.write(cmd);
  mySerial.write(pitch);
  mySerial.write(velocity);
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

As you can see I have a few variables that define which type of is to be sent.

CMDToChange will be 144 - 159 to equal a NOTE ON message
or 128-143 for a NOT OFF message

While researching midi messages I came across something that looks as thought it may be useful. Pretty much explains what the numbers mean and a bit more.

http://computermusicresource.com/MIDI.Commands.html

I am having trouble sending the correct midi message to my synth.
can anyone help me out with the correct way to send midi messages?

One Last thing when i play a note on my synth and then while it is still going i would then press the momentary button that is pinned to my arduino and it will Stop whatever is ringing through the synth.

you need to share complete code first .then only we can tell the solution. If you devide code in parts we cant track.

Not a problem…

here is the full sketch be aware most of the variables are not being used at the moment.
I was getting a bit ahead of my self. :-\

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
int btnnum = 22;
int buttonPin;
int x;
boolean currentStates[16];//stroage for current measured button state
boolean lastStates[16] ;//storage for last measured button state
boolean debouncedStates[16] ;//debounced button state
int debounceInterval[16];//wait 20 ms for button pin to settle
unsigned long timeOfLastButtonEvents[16];//store the last time the button state changed
int PinToChange;
int StateToChange;
int nextbyte = 0;

String fromsoftware;
char firstchar;
int StateValue = 0;
int PadToChange;
char PadValue;
long CMDToChange = 144;
int CMDValue = 1;
int NoteToChange = 1;
int NoteValue = 1;
int VelToChange;
int VelValue = 1;
char TrackToChange;
int TrackValue = 1;
char BarsToChange;
int BarsValue = 1;
char LengthToChange;
int LengthValue = 1;
char ActBarToChange;
int ActBarValue = 1;
char BankToChange;
int BankValue = 1;
int PinState22 = LOW;
int PinState23 = LOW;
int PinState24 = LOW;
int PinState25 = LOW;
int PinState26 = LOW;
int PinState27 = LOW;
int PinState28 = LOW;
int PinState29 = LOW;
int PinState30 = LOW;
int PinState31 = LOW;
int PinState32 = LOW;
int PinState33 = LOW;
int PinState34 = LOW;
int PinState35 = LOW;
int PinState36 = LOW;
int PinState37 = LOW;

int stillHIGH = 0;


void setup() {
  pinMode(13, OUTPUT);
  pinMode(31, INPUT);//this time we will set the pin as INPUT

  int pn = 1;
  int interval = 0;
  int padpin = 31;
  digitalWrite(13, LOW);
  while (pn < 16) {
    debounceInterval[interval] = 40;
    digitalWrite(padpin, 0);
    padpin++;
    interval++;
    pn++;
  }
  Serial.begin(9600);//initialize Serial connection


}

void loop() {



  int pn = 1;
  int padpin = 22;
  while (pn < 16) {

    currentStates[pn] = digitalRead(padpin);
    unsigned long currentTime = millis();

    if (currentStates[pn] != lastStates[pn]) {
      timeOfLastButtonEvents[pn] = currentTime;
    }

    if (currentTime - timeOfLastButtonEvents[pn] > debounceInterval[pn]) { //if enough time has passed
      if (currentStates[pn] != debouncedStates[pn]) { //if the current state is still different than our last stored debounced state
        debouncedStates[pn] = currentStates[pn];//update the debounced state
        //trigger an event
        
        if (debouncedStates[pn] == HIGH) {
          digitalWrite(13, 1 );
stillHIGH = 1;
          CMDToChange = 144;
          PadToChange = pn;
          StateToChange = 1;

          buttonClicked('22');

          Serial.println("pressed");
        } else {

          digitalWrite(13, 0 );
          CMDToChange = 128;
          PadToChange = pn;
          StateToChange = 1;
          stillHIGH = 0;
          buttonClicked('22');
          Serial.println("released");
        }
        while (stillHIGH) {
      if (debouncedStates[pn] == HIGH) {
        digitalWrite(13, 1 );
          CMDToChange = 144;
          PadToChange = pn;
          StateToChange = 1;
          stillHIGH = 0;
          buttonClicked('22');
          Serial.println("HELD...");
        }
        }
      }
      
      // }

      padpin++;
      pn++;
    }
    lastStates[pn] = currentStates[pn];


  }
}
void buttonClicked(int btn) {
  //Serial.println(index);
  btn = btn;
  if (btn == 22 && debouncedStates[PadToChange] == HIGH) {
    changeAllStates();
  }
  //digitalWrite(pin, LOW);
}
void changeAllStates() {
  VelToChange = 127;
  int chcmd;
  chcmd = CMDToChange;
  noteOn(CMDToChange, NoteToChange, VelToChange);

  //Serial.print(CMDToChange);
  //digitalWrite(13, LOW);
}

void noteOn(int cmd, int pitch, int velocity) {

  //Serial.print(cmd);
  mySerial.write(cmd);
  mySerial.write(pitch);
  mySerial.write(velocity);
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

Just had a thought... do all synths have pretty much the same midi messages? in general i understand this to be yes. but would a more robust synthesiser like the novation ultranova have them mapped out differently? now that i have a solid button press i can see buttons lighting up and changing back and for as if 144 and 128 are not the note on and off messages. im a bit confused. :sob:

long CMDToChange = 144

Why long? even an int is too big.

Serial.begin(9600);//initialize Serial connection

Does MIDI run at 9600 baud - answer No.

Grumpy genius should be your name sir..

changed to char and all is good.
and the baudrate to 31250

Lol I have it working.
smh
Thank you!!