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:
Use analogWrite(pin, pulseWidth) followed by an analogWrite(pin, 0) to cut it off before it finishes a cycle
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.
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..)