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.

http://forum.arduino.cc/index.php?topic=223286.0

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
  Timer1.attachInterrupt(over);
  attachInterrupt(echo, pulsein, FALLING);
  pinMode(led, OUTPUT);
  pinMode(trigger, OUTPUT);
}

void loop() {
  if (count == 0)
  {
    count = 1;
    digitalWrite(trigger,HIGH);
    digitalWrite(trigger,LOW);
    Timer1.restart();
  }
  if (counter - counterold > 100)  // 100ms led toggle
  {
    counterold = counter;
    ledstate = !ledstate;
    digitalWrite(led, ledstate);
  }
}


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


void over() {
  Timer1.stop();
  count = 0;  
}