How to use pulseIn to read both HIGH an LOW pulse width

Hi All.

I can successfully read a string of HIGH going pulses that I generate with a Nano.

With this code.

void readPulses()
{
  Serial.print("Start");

  for (unsigned int i = 0; i < numReadings; i++  )//numReading = 100
  {
    duration = pulseIn (readPin, HIGH);
    myArray[i] = duration ;
  }
}

But when I read both HIGH and LOW the values does not match up with what I generate.
This does not give the correct values seem like it miss read or skip readings.

void readPulses()
{
  Serial.print("Start");

  for (unsigned int i = 0; i < numReadings;  )//numReading = 100
  {
    duration = pulseIn (readPin, HIGH);
    myArray[i] = duration ;
    i++;
    duration = pulseIn (readPin, LOW);
    myArray[i] = duration ;
    i++;
  }

  Serial.print("Done");
}

Thanks for any help.

Have your read the reference: pulseIn() - Arduino Reference.

Your code in words:
Do hundred times: wait until the signal is low, then wait for the rising edge and start the timer. Then wait for it to go low then stop the timing. Then wait for it to get high, then wait for the falling edge and start the timer. Then wait for it to go high and stop the timer.

The signal has to give about three pulses instead of one.

Can you tell what your project is ?

Yes .. I did also read the reference info to your link.

It seems that when a pulse goes LOW the pulsIn HIGH stops counting which works fine if you only read HIGH pulse.

But when I want to read pulseIn LOW immediately after pulseIn HIGH detects the LOW the LOW sometimes skips to the next LOW.

The code is for reading(playing around)with 433Mhz encoders and decoders.

Libraries for 433MHz use a interrupt and micros() to measure the pulses.

I played with that libraries and they work.

Lets say I want to read the pulse width of a HIGH and LOW pulse then pulseIn HIGH and pulsIn LOW wont work.

The delay of setting up the interrupt from reading HIGH to reading LOW misses the pulse going LOW and only detects the next LOW pulse and if there is no next LOW going pulse a "0" is returned after 1 sec.

Back to my friend Google.

Maybe the writing to the Array causes the delay.

I found this for a period counter.

ontime = pulseIn(pulse_ip,HIGH);
   offtime = pulseIn(pulse_ip,LOW);
   period = ontime+offtime;

mikedb:
Maybe the writing to the Array causes the delay.

I found this for a period counter.

ontime = pulseIn(pulse_ip,HIGH);

offtime = pulseIn(pulse_ip,LOW);
  period = ontime+offtime;

That works for a steady PWM signal but it skips two out of three HIGH pulses and two out of three LOW pulses. When the first pulseIn() finishes measuring a HIGH pulse it is because the pin went LOW at the end of the pulse. The second pulseIn() is too late to see the start of that LOW pulse so it has to wait until the pin goes HIGH and LOW again to catch the beginning of a LOW pulse. It then finishes when the pin goes HIGH again. That causes the first pulseIn() to miss the start of that HIGH pulse so it has to wait for the pin to go LOW and HIGH again.

To measure consecutive pulses you have to use something other than pulseIn(). The IRremote library uses a timer interrupt at about 50 microsecond intervals. It just counts consecutive HIGH or LOW samples.

Thanks johnwasser this is exactly what I found.

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