MIDI signal not starting or stopping and outputting other midi commands

I have created a simple push button that should send out a simple SysEx command that I know is right but when it sends it, it also sends a load of other nonsense with it not just the SysEx I'm trying to send.

So basically I'm just trying to clear up my output so its only the SysEx signal being sent.

This is my code.

#include <MIDI_Controller.h>
#include <MIDI.h>

#define BUTTON 2
byte MIDI_CHANNEL = 1;

int currentstatus = LOW;  //Variable. Is the key currently pressed?
int noteisplaying = 0;    //Variable. Is the Note currently playing?
MIDI_CREATE_DEFAULT_INSTANCE();

void setup()  //The Setup Loop
{
  Serial.begin(31250);         //Buad rate of midi
  MIDI.begin(MIDI_CHANNEL);    //initialise midi library
  digitalWrite(BUTTON, HIGH);  //Light up led when button pressed
}
//---------------------------------------------
void loop()  //the main loop
{
  int keyispressed = digitalRead(BUTTON);  //read pin 2

  if (keyispressed != currentstatus) {  //state has changed

    currentstatus = keyispressed;

    if (currentstatus == LOW) {  // Button is now pressed

      // Sub 90 @ 100
      byte message[] = { 0xf0, 0x7F, 0x01, 0x02, 0x01, 0x06, 0x5A, 0x00, 0x64, 0x00, 0xf7 };
      MIDI.sendSysEx(message, sizeof(message));

    } else {  // Button is now released

      //Sub 90 @ 0
      byte message[] = { 0xF0, 0x7F, 0x01, 0x02, 0x01, 0x06, 0x5A, 0x00, 0x00, 0x00, 0xF7 };
      MIDI.sendSysEx(message, sizeof(message));
    }
  }
}

And this produces this output.

As you can see it does produce the right line of code but it looks like it goes through every other command first which can't happen. I need it to be a single clean line outputted.

Any assistance would be great, thank you!

Why are you sending the same SYSex message when you press the button and when you release it?

Also I think you will find that the MIDI.h library limits SYSex messages to a maximum length of 4 bytes, so you have to split it up into chunks of four bytes or less and send them one after the other. in your case two messages of 4 bytes and one message of three bytes.

I am not, the value at the end is changing from 100 when pressed to 0 when depressed.

I didn't see that anywhere so will try that next cheers.

I don't understand I see the comments

and

But I see no sign of any subtraction taking place

When pushed down it send this.

byte message[] = { 0xf0, 0x7F, 0x01, 0x02, 0x01, 0x06, 0x5A, 0x00, 0x64, 0x00, 0xf7 };

Note the 0x64 after the 0x00.

Whereas when de pressed it sends this.

byte message[] = { 0xF0, 0x7F, 0x01, 0x02, 0x01, 0x06, 0x5A, 0x00, 0x00, 0x00, 0xF7 };

Note no 0x64 but a 0x00 instead.

OK I could not spot that but what is the point of this second message?

Also your comment said "sub 90 @ 0".
Two things were confusing

  1. I read sub as subtract, I assume now you meant substitute.
  2. The number mentioned in the comment was in a different number base to the one used in the message.

Am I right in thinking that this code is just a part of a much bigger piece of code? Because you said in the first post

Does this full code use the MIDIUSB library? Because if it does after you send a MIDI message you are supposed to use a

MidiUSB.flush();

Call. Maybe this is not happening?

That is all the code, I just need it to do this one thing and nothing else.

This is to control a lighting desk, but the system I previously made has no redundancy so I'm remaking it which is why I need the SysEx command.

The sub stands for submaster which is just a fader on the lighting desk, thats my bad for not explaining correctly.

I don’t use the MIDIUSB library just the MIDI library but I will look to see if there is a flush I can add.

I am away for a couple days now so will make these changes when I get back and dig in a bit deeper then.

OK I did try your code and found it did exactly what you said, by sending a load of junk messages. After an hour or so of trying to see what was going wrong I decided to take the advice of a man in the countryside who was asked "how do you get to London"? He replied, "well ....... if I was going to London, ...... I wouldn't start from here"

So I decided to do it in a way I would call "properly" and ditch the MIDI library.

This code uses pin 2 as the button, with the button just connected between the input pin and ground. When the button is pressed the onboard LED is illuminated. I have tested this code on an Arduino Uno and it works.

//Push button between input and ground
#define BUTTON 2

bool lastButtonStatus = false;  //Variable. was the key pressed last time
bool isKeyPressed = false;  //Variable. Is the key currently pressed?
byte messageKeyDown[] = { 0xf0, 0x7F, 0x01, 0x02, 0x01, 0x06, 0x5A, 0x00, 0x64, 0x00, 0xf7 };
byte messageKeyUp[] = { 0xF0, 0x7F, 0x01, 0x02, 0x01, 0x06, 0x5A, 0x00, 0x00, 0x00, 0xF7 };

void setup()  //The Setup Loop
{
  Serial.begin(31250); //Buad rate of midi
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(BUTTON, INPUT_PULLUP); 
}

//---------------------------------------------

void loop(){
  isKeyPressed = digitalRead(BUTTON);  //read pin 2
  if (isKeyPressed != lastButtonStatus) {  //state has changed
    digitalWrite(LED_BUILTIN, !isKeyPressed);

    if (isKeyPressed) {  // Button is now pressed      
      midiSysEx(messageKeyDown, sizeof(messageKeyDown));      
    } 
    else {  // Button is now released
      midiSysEx(messageKeyUp, sizeof(messageKeyUp));
    }
    delay(200); // debounce delay
    lastButtonStatus = isKeyPressed; // for next key press
  }
}

void midiSysEx(byte *messageArray, int arrayLength){
  for(int i = 0; i< arrayLength; i++) {
    Serial.write(messageArray[i]);
  }
}

This is a screen dump from the MidiMonitor app.

Thank you Mike. I did some digging and the midi library is extremely sensitive to any voltage change or any input whereas the midi controller library has some code where if the state changes for more than a certain time then it proceeds with the change making it way more stable just as an FYI.

Cheers for all your help tho, this is great.

Great you liked my solution.

I am not too sure about that. I can't see any mechanism that would do that. I have used the MIDI.h in dozens of projects and never came across what I was seeing when I tried your code. Thing is this is the digital world and any voltage variation would make no difference to any code you try and run. So there is nothing special about a library it is just code like anything else.

I might just investigate further and raise a Git Hub issue if I can't sober out the exact reason. I tend to use this library when I want to use the input parser.

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