usbMIDI Continual send on Teensy 3.2 with MIDI OX

Status
Not open for further replies.

Iain

Member
Hi All
New to this forum.
I am just starting out have moved from Arduino to Teensy for my second MIDI Vst controller. When I used a 5 pin port on arduino and monitored with MIDI OX I would only get CC messages when there was a control change. I wrote this little sketch below(I know its crude) and it does everything I expect except when monitoring in MIDIOX I get a continual stream of updates (the values remain the same until I press the button as expected) and then the new value is continually sent. Is this just the way usbMIDI works or is there something else going on? I'm concerned that this would be a huge data hog if midi data continually coming into my DAW ?
Thanks
Iain




const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin_BLUE = 8; // the number of the LED pin
const int ledPin_YELLOW = 9;
const int ledPin_GREEN = 10;

// Variables will change:
int state = 1; // interger to hold current state
int old = 0; // interger to hold last state
int buttonPoll = 0; // interger to hold button state




void setup() {

pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin_BLUE, OUTPUT);
pinMode(ledPin_YELLOW, OUTPUT);
pinMode(ledPin_GREEN, OUTPUT);


// set initial LED state
digitalWrite(ledPin_BLUE, HIGH);
digitalWrite(ledPin_YELLOW, LOW);
digitalWrite(ledPin_GREEN, LOW);
}

void loop() {

buttonPoll = digitalRead(buttonPin); //read button pin
if (buttonPoll == 1){
delay(5);
buttonPoll = digitalRead(buttonPin);
if (buttonPoll == 0) {
state = old + 1;
}}
else{
delay(50);
}

switch (state) {
case 1:
digitalWrite(ledPin_BLUE, HIGH);
digitalWrite(ledPin_YELLOW, LOW);
digitalWrite(ledPin_GREEN, LOW);
usbMIDI.sendControlChange( 1, 0, 1);
usbMIDI.send_now();

old = state;

break;
case 2:
digitalWrite(ledPin_BLUE, LOW);
digitalWrite(ledPin_YELLOW, HIGH);
digitalWrite(ledPin_GREEN, LOW);
usbMIDI.sendControlChange( 1, 55, 1);
usbMIDI.send_now();
old = state;

break;
case 3:
digitalWrite(ledPin_BLUE, LOW);
digitalWrite(ledPin_YELLOW, LOW);
digitalWrite(ledPin_GREEN, HIGH);
usbMIDI.sendControlChange( 1, 127, 1);
usbMIDI.send_now();
old = 0;
}


// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) {
}
}
 
Hi All
New to this forum.
I am just starting out have moved from Arduino to Teensy for my second MIDI Vst controller. When I used a 5 pin port on arduino and monitored with MIDI OX I would only get CC messages when there was a control change. I wrote this little sketch below(I know its crude) and it does everything I expect except when monitoring in MIDIOX I get a continual stream of updates (the values remain the same until I press the button as expected) and then the new value is continually sent. Is this just the way usbMIDI works or is there something else going on? I'm concerned that this would be a huge data hog if midi data continually coming into my DAW ?
Thanks
Iain

Iain:

Welcome to the forum !! One quick way to make it so that your sketch only sends the CC message when a button press is detected would be to add the following lines (highlighted in RED):

Code:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin_BLUE = 8; // the number of the LED pin
const int ledPin_YELLOW = 9;
const int ledPin_GREEN = 10;

// Variables will change:
int state = 1; // interger to hold current state
int old = 0; // interger to hold last state
int buttonPoll = 0; // interger to hold button state




void setup() {

   pinMode(buttonPin, INPUT_PULLUP);
   pinMode(ledPin_BLUE, OUTPUT);
   pinMode(ledPin_YELLOW, OUTPUT);
   pinMode(ledPin_GREEN, OUTPUT);


   // set initial LED state
   digitalWrite(ledPin_BLUE, HIGH);
   digitalWrite(ledPin_YELLOW, LOW);
   digitalWrite(ledPin_GREEN, LOW);
}

void loop() {

   buttonPoll = digitalRead(buttonPin); //read button pin
   if (buttonPoll == 1) {
      delay(5);
      buttonPoll = digitalRead(buttonPin);
      if (buttonPoll == 0) {
         state = old + 1;
      }
   }
   else {
      delay(50);
   }

   switch (state) {
      case 1:
         digitalWrite(ledPin_BLUE, HIGH);
         digitalWrite(ledPin_YELLOW, LOW);
         digitalWrite(ledPin_GREEN, LOW);
         usbMIDI.sendControlChange( 1, 0, 1);
         usbMIDI.send_now();

         old = state;
[COLOR="#FF0000"]         state = 0;
[/COLOR]
         break;
      case 2:
         digitalWrite(ledPin_BLUE, LOW);
         digitalWrite(ledPin_YELLOW, HIGH);
         digitalWrite(ledPin_GREEN, LOW);
         usbMIDI.sendControlChange( 1, 55, 1);
         usbMIDI.send_now();

         old = state;
[COLOR="#FF0000"]         state = 0;
[/COLOR]
         break;
      case 3:
         digitalWrite(ledPin_BLUE, LOW);
         digitalWrite(ledPin_YELLOW, LOW);
         digitalWrite(ledPin_GREEN, HIGH);
         usbMIDI.sendControlChange( 1, 127, 1);
         usbMIDI.send_now();

         old = 0;
[COLOR="#FF0000"]         state = 0;
[/COLOR]   }


   // MIDI Controllers should discard incoming MIDI messages.
   while (usbMIDI.read()) {
   }
}

As a suggestion, it makes it easier to view & copy your code if you enclose your code in "CODE tags" (which are automatically generated by using the "#" icon right above where you entered the text of your message). Also, in the Arduino IDE, you can use "CTRL-T" to automatically format your code before you paste it between the CODE tags. You might find this useful as well.

Please feel free to ask any other questions . . . there are a lot of good folks willing to help on this forum !!

Good luck & have fun !!

Mark J Culross
KD5RXT
 
Thanks Mark
Ill give that a try and thanks for the posting code tip as well
I've also tried some other Teensy USB midi controller code from other users and they all do the same thing when monitored with MIDIOX so maybe its just the way Teensy deals with usbMIDI. It sends every time around the loop regardless of change or not?
Have to admit it wasn't what I was expecting
Ian
 
@Iain:

The change that I suggested is not specific either to Teensy, nor to USB MIDI. The switch statement (as originally written) uses the current value of the "state" variable to decide what to send (NOTE that there is not a case statement for "state" equal to "0", so nothing will be sent for state "0"). State "1" sends the CC w/ a value of "0", state "2" sends the CC w/ a value of "55", & state "3" sends the CC w/ a value of "127". Each time the button is pressed & released, the "old" variable will be incremented & "state" will be assigned the updated value of "old". Likewise, each time through the loop, the value of "state" will be used by the switch statement to decide what value to send in the CC. Without resetting the "state" to "0" after sending a CC, because state is always not "0", another CC gets sent each time thru the loop.

By (re)setting the "state" to "0" after sending the CC (with the appropriate value based upon the state), the CC will only be sent once each time the button is pressed & released. Because "state" is (re)set to "0", the next time thru the loop, nothing will get sent.

Once you thoroughly understand how & why this works, you should certainly be able to use this same approach, no matter how complex your code.

Good luck & have fun !!

Mark J Culross
KD5RXT
 
Status
Not open for further replies.
Back
Top