MIDI: How to intentionally delay notes?

I have a sketch where MIDI notes trigger pulses to analog pins. I need to be able to delay some of the triggers by a few microseconds. I've been trying to cause a delay between when the MIDI note is received and the analogWrite occurs.
Here's part of the sketch:

if (note == 43) {
analogWrite(11, TRIGGERLEVEL);
delayMicroseconds(DELAYTIME);
analogWrite(11, LOW);

(where DELAYTIME= 1000)

I've tried adding both delayMicroseconds() and delay() like so:

if (note == 43) {
delay(DELAYTIME2);
analogWrite(11, TRIGGERLEVEL);
delayMicroseconds(DELAYTIME);
analogWrite(11, LOW);

But it doesn't seem to work, no matter how much I increase the DELAYTIME2. I'm pretty new to Arduino. What do I need to do to get my desired result?

Yes it should work. How are you measuring the fact that you think it dosn't work.

Two ways I know:

  1. the analog circuit that the "delayed" pulse triggers remains in sync with the MIDI sequencers metronome, again no matter how much I increase the DELAYTIME2 value. There should be an audible delay (difficult to discern with a few microseconds, of course, but I've increased the value up to a second and get the same result).

  2. (long explanation) The reason I need some notes delayed is because there is one MIDI note (that triggers the Accent circuit) that I do not want delayed, i.e., I want it to trigger its pulse before all of the others when they are all programmed on the same beat. When this happens, all notes on that same beat are Accented (amplitude is increased). So, I know that the other notes are not delayed, because no matter what value I set as DELAY2, the notes are not accented (meaning that the Accent pulse didn't trigger first). On the music sequencer (on the laptop), if I nudge the Accent note a couple of ticks before all other notes on the same beat, then the Accent circuit triggers as it should.

Just think what you are saying here. No matter what the delay between detecting a note and taking action on it there is no delay.
This simply is not true, it can't be. There is another effect in action here that leads you to that erroneous conclusion. Measure it with an oscilloscope and you will see it is a real delay.

Unfortunately I don't understand your explanation of your methodology to se where it is wrong.

Remember that the delay will affect all the notes that follow it as well because the delay function is blocking, that is the processor does nothing so the sequence of all notes is delayed, not just one.

Grumpy_Mike:
Just think what you are saying here. No matter what the delay between detecting a note and taking action on it there is no delay.

I know. It has me stumped.
Let me try to give a better explanation. I have a software sequencer on my laptop that is sending MIDI notes though a USB-to-MIDI cable into the Arduino (in this case a Teensy 2.0). The Teensy receives MIDI notes and each note within a certain range (notes #35-48) is assigned to an analog pin to send a pulse (as in the sketch). The pulse triggers an analog drum voice (Bass Drum or Snare or Cymbal, etc.). If I program a quantized beat into the software sequencer, so that each drum sound falls on the beat, it is perfectly in time with the sequencer's metronome. By adding that line to the sketch of (DELAY2), then the drum sounds should sound later, or after the beat and be out-of-sync with the sequencer's metronome. The rhythm of each drum sound, however, remains unchanged, exactly on the beat, in spite of the fact that I've written even up to one second of delay in that line of the sketch.

I hope that makes more sense.

Remember that the delay will affect all the notes that follow it as well because the delay function is blocking, that is the processor does nothing so the sequence of all notes is delayed, not just one.

Well, if this is the case, then it won't work for my intended purpose. On the other hand, I can't seem to get it to delay the triggers at all.

Hi,
I wouldn't use delay() functions, because it will stop your listener loop, and it can be bad.
You can do, for example, when detecting the MIDI note :
long start = millis();
And in your main loop :
if(millis()>=start+DELAYTIME) { your code here }

By adding that line to the sketch of (DELAY2), then the drum sounds should sound later, or after the beat and be out-of-sync with the sequencer's metronome.

Not if you are taking as your reference the other non delayed notes. I think this is your problem.

I wouldn't use delay() functions, because it will stop your listener loop,

Well not quite, the interrupts will still occur and the serial buffer will still fill, or in the case of the Teensy the USB buffer so using delay is not much of an issue except in respect that it will delay everything else as well.

I think what you need here is a delayed call back function. When you see the note you want to take a delayed action on you set a timer going. When that timer times out it triggers an interrupt and then the interrupt service routine will fire the pulse to trigger your analogue synth.

That could be messy especially as you need one timer for each simultaneous delay.

The other way to implement this is to do it with the blink without delay technique. When you get the note you want, all you do is to set a variable to the millis() time plus your delay. Then in the loop you check if that variable is not zero, which would mean it is off, AND the current time from millis() is greater than or equal to the variable you set. If it is then go and trigger your pulse and reset the variable to zero. You will need this for each delayed note you want.

Ok, Grumpy, I figured some things out. First, I tried using delay() with a numerical value rather than an assigned variable "DELAYTIME2" and it did in fact delay everything. I'm not sure what I had done originally, but it does operate as expected now.

Of course, that does not solve my problem. I'll read up on the blink without delay and see if I can translate your explanation into some kind of code I can incorporate into my sketch. We'll see...

I'm not sure what I had done originally, but it does operate as expected now.

This is why we ask you to post all your code using the proper code tags.
Please read the how to use the forum sticky post you find at the start of any section.

How instructive and insightful. I'll definitely be using proper code tags in the future.