PulseIn Multitasking

Is it possible for example to let an LED blink with 5Hz and use the PulseIn function at the same time. The PulseIn function must not slow the blinking LED down.

I don't think so. pulseIn is blocking. You'd have to write non-blocking code to keep the LED flashing and also at the same time watching the input pin for it to change state.


Then I should write a function with the internal Timer1 and an interrupt on the input pin. Could that work?

I came up with this:

What do you think?

#include "TimerOne.h"

#define echo 0
#define trigger 1
#define led 2

boolean count, ledstate;
long distance, counter, counterold;

void setup() {
  Timer1.initialize(1000000);    // 1 sec Overflow
  attachInterrupt(echo, pulsein, FALLING);
  pinMode(led, OUTPUT);
  pinMode(trigger, OUTPUT);

void loop() {
  if (count == 0)
    count = 1;
  if (counter - counterold > 100)  // 100ms led toggle
    counterold = counter;
    ledstate = !ledstate;
    digitalWrite(led, ledstate);

void pulsein() {
  distance = Timer1.read();
  count = 0;

void over() {
  count = 0;