int pirPin = 2;
int ledPin = 13;
int state = LOW;
unsigned long startTime, responseTime;
int motionCount = 0;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
// Serial.begin(115200);
}
void loop() {
startTime = micros();
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
digitalWrite(ledPin, HIGH);
responseTime = micros() - startTime;
if (state == LOW) {
motionCount++;
Serial.print("Motion #");
Serial.print(motionCount);
Serial.print(" - Response Time: ");
Serial.print(responseTime);
Serial.println(" μs");
state = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
if (state == HIGH) {
Serial.println("Motion stopped");
state = LOW;
}
}
delay(500);
}