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");
}
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.
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.
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.