Best method to generate a single pulse?

For a project I'm working on, I need to generate single pulses of a certain width (shortest would be 10us) at various times. The two options I've come up with so far are:

  1. Use analogWrite(pin, pulseWidth) followed by an analogWrite(pin, 0) to cut it off before it finishes a cycle

  2. Use delayMicroseconds(pulseWidth) with digitalWrite() to manually toggle an output.

The catch here is that while this pulse happens, I need to detect a different pulse coming in from a mosfet driver which verifies that the mosfet switched. Right now I'm planning on doing that with an interrupt but I'm not sure how it'll affect the above functions.

Any ideas?

Here's a timing diagram:

The incoming pulse is on a different pin, right?

If you're doing timing-critical stuff like this, I would recommend going with direct port manipulation instead of digitalWrite / digitalRead (they're rather "slow").

So:

#define dwon(port, pin) (port |= _BV(pin))
#define dwoff(port, pin) (port &= ~(_BV(pin)))

const int outputPin = 3;


void setup()
{
  pinMode(inputPin, INPUT);
  pinMode(outputPin, OUTPUT);
}

// PORTD, pin 3 is digital I/O pin 3
// lookup the schematic for these values
void loop()
{
  // pulse pin 3
  dwon(PORTD, 3);
  delayMicroseconds(10);
  dwoff(PORTD, 3);
}

And then the best way to capture I think would be an interrupt. So long as you keep your interrupt as short as possible (ex: set a "volatile" variable to 1, and that's it), I think it should be fine.

Otherwise you can bring the output pin high, then sit and wait for the input pin to go high, then bring the output pin low again (after waiting the appropriate time..)

They are on different pins. Here's the gut of my code:

const int pulseDetect = 3;
attachInterrupt(pulseDetect, storePulse, RISING);

void storePulse()
{
  
  boolPulseDetect = true;
  
}

void pulse() 
{

  boolPulseDetect = false;  
  if ( strobeTime > 1000 ) //Use delay() for ms delays
  {
    
    digitalWrite(strobe, HIGH);
    delay(strobeTime/1000);
    digitalWrite(strobe, LOW);
    
  }
  else //Use delayMicroseconds() for us delays
  {
    
    digitalWrite(strobe, HIGH);
    delayMicroseconds(strobeTime);
    digitalWrite(strobe, LOW);
    
  }
  
  if ( !boolPulseDetect )
  {
   
  }
        
}

Aside from changing the port manipulations, this should work eh?

Seems good. Best way to see if it works or not is to test it :stuck_out_tongue: