# Avoid Pulsein?

hi all.

on my project my slave needs to read pulses and if they have the correct width (program compare them with some variables) it needs to send them to an output pin.

The problem is that when i am using pulsein for example to see the width when the state is rising it waits from low to become high and continue when it goes back to low, so it can’t send these pulses to the output pin, is there any way to solve this problem?

You have the source for pulseIn. You could write your own, with the bits that you do want, and missing the bits you don't.

acmiilangr: The problem is that when i am using pulsein for example to see the width when the state is rising it waits from low to become high and continue when it goes back to low, so it can't send these pulses to the output pin, is there any way to solve this problem?

Sometimes there's a way to do it, if you're able to fit the output pulse into the gap between pulses and you don't care that the input and output pulses aren't in phase. If those conditions are met, you can use the function pulseIn().

If not, you're better off rewriting it so it meets your needs. Depending on what you really need you can do it either with or without interrupts - both solutions have their advantages and problems. If you really just to replicate exactly the same pulse, you could also consider just running a wire from the input to the destination of the output and not go through the Arduino or running it to a AND gate with the output just as an output enable signal.

Korman

I have an application that watches several digital pins and sets the durations of the pulses. This is watching the output of a Radio Control receiver.

I use code something like this for each pin.

``````int Chan1Interrupt = 5; // pin 18
int Chan2Interrupt = 4; // pin 19
int Chan3Interrupt = 3; // pin 20
int Chan4Interrupt = 2; // pin 21
int Chan5Interrupt = 1; // pin 3
unsigned long Chan1_startPulse, Chan2_startPulse, Chan3_startPulse, Chan4_startPulse, Chan5_startPulse;
volatile double Chan1_val, Chan2_val, Chan3_val, Chan4_val, Chan5_val;

void Chan1_begin()	     // enter Chan1_begin when interrupt pin goes HIGH.
{
Chan1_startPulse = micros();     // record microseconds() value as Chan1_startPulse
detachInterrupt(Chan1Interrupt);  // after recording the value, detach the interrupt from Chan1_begin
attachInterrupt(Chan1Interrupt, Chan1_end, FALLING); // re-attach the interrupt as Chan1_end, so we can record the value when it goes low
}

void Chan1_end()
{
Chan1_val = micros() - Chan1_startPulse;  // when interrupt pin goes LOW, record the total pulse length by subtracting previous start value from current micros() vlaue.
detachInterrupt(Chan1Interrupt);  // detach and get ready to go HIGH again
attachInterrupt(Chan1Interrupt, Chan1_begin, RISING);
}
``````

Hope I posted the relevant sections of my code.