#define PIR_PIN 2
#define LED_PIN 8
volatile unsigned long tRise = 0, tCmd = 0, dt = 0;
volatile bool ready = false;
void isrChange() {
if (digitalRead(PIR_PIN) == HIGH) {
tRise = micros();
digitalWrite(LED_PIN, HIGH);
tCmd = micros();
dt = tCmd - tRise;
ready = true;
} else {
digitalWrite(LED_PIN, LOW);
}
}
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(PIR_PIN), isrChange, CHANGE);
}
void loop() {
if (ready) {
noInterrupts();
unsigned long x = dt;
ready = false;
interrupts();
Serial.print("RESPONSE_MICROSECONDS=");
Serial.println(x);
}
}