void loop() {
if (digitalRead(pirPin) == HIGH) {
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("Motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" secs");
delay(50);
}
takeLowTime = true;
}
if (digitalRead(pirPin) == LOW) {
Serial.print("Motion ended...");
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("Motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" secs");
delay(50);
}
}
I notice that when I continue to wave my hand in front of the sensor after activating it, it begins to tell me that motion has ended. It will then go on to say that motion has begun again. Can anybody explain why this is happening?
Hi there. You need to re-evaluate your logic. As you have not included the setup code section, or any initial variable declarations, it is difficult to follow the logic flow. If we assume the sensor read input is low for the no-detect state, the first two lines of code to be executed are in the second half of the code. This will light the LED as you are expecting, but also notify that motion detection has ended. This obviously not correct as an initial detection state has not yet been recorded. And then what of the takeLowTime flag state. Does that start false (LOW) or true (HIGH)?
You need to lay out the logic again - perhaps write it down in words as you would describe it to someone. Also note that at each main loop you appear to be switching the LED on/off regardless of its current state, and outputting messages inthe same manner. Unless the logic does work in some perverse way.
One thing to try may be using a single variable as a state indicator. Not several true/false variables that are inter-dependant, but a single integer value with multiple pre-defined states. This state is updated to reflect the current or next expected state each time around the loop. For a simple example, the states may be defined as:
IDLE = 0
MOTION_DETECTED = 1
MOTION_STOPPED = 2
Your main loop() code then becomes a simple switch() statement or an if-elseif-else construction. Hopefully I have made myself clear!
Start with turning the "time" pot fully counter-clockwise (and leave it there).
Leave the "sensitivity pot in the middle (it does NOT adjust sensitivity).
Now the PIR sensor should be available again after 3 seconds.
Manage any occupancy delay in code (not with the pots).
Leo..