Measuring LPO (Low-Pulse Occupancy) I'm using an Arduino clone (Seeeduino Lotus) together with a Shinyei PPD42NS sensor to measure air quality. The sensor reports its measurement as Low-Pulse occupancy. What would be the best way how to collect the value with Arduino? Currently I'm simply looping for some predefined duration using pulseIn:



unsigned long starttime;
register unsigned long sample_time; // ms
register unsigned long lowpulseoccupancy = 0; // us

// Wait for the signal to return to HIGH
pulseIn(DUSTPIN, LOW);
// Now start measuring
starttime = millis();
while ((sample_time = (millis() - starttime)) &lt; DUST_SAMPLE_TIME_MS) {
    lowpulseoccupancy += pulseIn(DUSTPIN, LOW);
}

ratio_pct = lowpulseoccupancy / (sample_time * 10.0);


Is there a better way? In particular, this tight loop prevents Arduino from doing anything else for 30s (or a similar period).

I was thinking about using interrupts, either triggering on CHANGE, or (if that's possible) having two functions, one triggering on RISING and another on FALLING. Are there any caveats when doing so?
You can trigger on CHANGE, and check in your handler which case it is:

   attachInterrupt(digitalPinToInterrupt(my_pin), my_handler, CHANGE);
    (...)
    void my_handler(void) {
      if(LOW==digitalRead(my_pin)) {
        //do_falling();
      }
      else {
        //do_rising();
      }
    }


Another option would be to use a timer to poll your input, and implement a logic on top of this.