I have two IR LED beams that pulse differently. Both have a 50% duty cycle at 48KHz. one is on for 85usecs and off for 400usecs and the other is on for 170usecs and off for 315usecs. I use CHANGE in the interrupt attachment and then read the pin to see if it’s falling or rising. I never seem to be able to get a consistent value for the pulse.
example code:
#include "Particle.h"
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
void setup() {
Serial.begin();
pinMode(D1, INPUT);
digitalRead(D1);
Particle.process();
attachInterrupt(D1,getpulseWidth,CHANGE,13);
}
void loop() {
if ( (pulse != 0) && (rise != 0) ) {
Serial.printlnf("pulse = %lu) rise = %lu) fall = %lu)", pulse, rise, fall );
pulse = rise = fall = 0;
}
Particle.process();
}
void getpulseWidth() {
if ( pinReadFast(beaconSensePinArray[0]) == LOW ) {
if ( (fall == 0) && (pulse == 0) ) {
fall = SYSTEM_TICK_COUNTER;
}
} else {
if ( (fall != 0) && (pulse == 0) ) {
rise = SYSTEM_TICK_COUNTER;
pulse = rise - fall;
}
}
}