ESP8266 reading PIR sensor

7 years 5 months ago #3556 by C0rrupt3d
Hello everyone,

Really sorry in advance if i get anoying, but still learning :)
I made a class for reading the pir state but its behaving a bit strange at the moment, maybe somebody already stumbled on this problem or could help me out.

The class is as follows:
boolean checkPir() {
  int pir = digitalRead(PirPin);
  if (pir == HIGH) {
    if (pirState) {
      pirState = false;
      Serial.println("Motion Detected!");
      return true;
      delay(50);
    }
    pirLowTime = true;
  }
  if (pir == LOW) {
    if (pirLowTime) {
      pirState = false;
      Serial.print("Motion Ended! Delaying for ");
      Serial.print(motionDelay/1000);
      Serial.println(" seconds.");
      pirTime = millis();
      pirLowTime = false;
    }
    if (!pirState && millis() - pirTime > motionDelay) {
      pirState = true;
      Serial.println("Delay Ended!");
      return false;
      delay(50);
    }
  }
}

But whenever i am trying to read the class i get wierd numbers back:
motion=0.
motion=0.
motion=0.
motion=0.
Motion Detected!
motion=1.
motion=1073670060.
motion=1073670060.
motion=1073670060.
motion=1073670060.
motion=1073670060.
Motion Ended! Delaying for 10 seconds.
motion=0.
motion=681.
motion=1182.
motion=1682.
motion=2183.
motion=2683.
motion=3184.
motion=3685.
motion=4185.
motion=4686.
motion=5186.
motion=5687.
motion=6187.
motion=6688.
motion=7189.
motion=7689.
motion=8189.
motion=8714.
motion=9215.
motion=9715.
Delay Ended!
motion=0.
motion=0.
motion=0.
motion=0.

Can anybody spot what i did wrong? I can't get my head around the problem.
I was expecting to get a stable 0 if no motion and a stable 1 untill the 10 seconds after motion end event.

Thanks in advance!

Please Log in or Create an account to join the conversation.

Time to create page: 0.193 seconds

Forum latest

  • No posts to display.