-->
Page 1 of 1

ESP8266 PIR state class

PostPosted: Mon Oct 31, 2016 3:03 pm
by C0rrupt3d
Hello everyone,

I`m messing around with a PIR on a small project, and i hit a wall.
The top bit looks like this:

Code: Select allbool pirState = true;
bool pirLowTime;
long unsigned int motionDelay = 10000;
long unsigned int pirTime;


Made a PIR class that looks as follows:

Code: Select allboolean 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);
    }
  }
}


I observerd a wierd behavior and when debuging i get the following:

Code: Select allmotion=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.


What i was expecting was a stable 0 when the sensor is OFF, and a stable 1 when sensor is ON and until after the 10 seconds delay added.
Can anybody spot what i did wrong here cos i cant seem to get my head around this.

Thanks in advance!