MidiUSB Script not working as intended

hi there, I'm still trying to figure this out:
Arduino Leonardo, connected to Apple / MainStage.
MainStage gets NoteOn/Off-Commands from 0 to 15 from a Footswitch on channel 0.
Commands get routed through to Leonardo.
Works.

Leonardo with MdiUSB should do this:
if you get a noteOn X on channel 1,
wait for the matching NoteOff,
if the Noteoff comes within 600ms: send noteOn X on channel 2
if it is more: send Send NoteOn X+12 on channel 2
then wait for a certain time (wait) and then send the matching NoteOff.

I tried several scripts, with delay or working with Counters based on Millis().
As soon as I try to send the note offs, havoc.
NoteOn only – works.

Any Ideas welcome.
Also on documentation of that MidiUSB-Thing, kinda sparse info...
thanks in advance.

#include "MIDIUSB.h"

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

unsigned long timers[8] {0,0,0,0,0,0,0,0};

unsigned long lang = 600;
unsigned long wait = 50;

void setup() {
  Serial.begin(312500);
}

void loop() {
  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();
    if (rx.header != 0 && rx.byte1 == 0x90 && timers[rx.byte2] == 0) {
        timers[rx.byte2] = millis();
        Serial.println(rx.byte2);
        Serial.println(timers[rx.byte2]);
      }
    if (rx.header != 0 && rx.byte1 == 0x80 && timers[rx.byte2] != 0) {
        if ((timers[rx.byte2]+lang) > millis()){
          noteOn(1, rx.byte2, 127);
          MidiUSB.flush();
          delay(wait);
          noteOff(1, rx.byte2, 127);
          MidiUSB.flush();
          timers[rx.byte2] = 0;
        }
        else {
          noteOn(1, rx.byte2+12, 127);
          MidiUSB.flush();
          delay(wait);
          noteOff(1, rx.byte2+12, 127);
          MidiUSB.flush();
          timers[rx.byte2] = 0;
        }
      }
    } while (rx.header != 0);
}

Define "havoc"

after first press of Switch Arduino sends on / Offs of that note ab lib.
No Pattern I could figure out.

Sorry for the confusion, i figured one Issue out:

Mainstage only sends MidiCommands that are NOT Connected to an internal Control.
But then all of them and : on all channels!

My midifootswitch is doing nothing in MainStage, so its commands get routed through to Arduino on channel 2

As long as my virtual buttons in MainStage the Script is addressing have „nothing to do“, they also echo every command back to the Leonardo on channel 2.
causing all kinds of erratic behaviour

hurray....

Back to Coding.
I'll keep you updated, if there is interest : )

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