Help on a complex logic(atleast for me)

Greetings everyone!
I am trying to make a thing that checks if a room is occupied or not. For now, just trying to check if 1 person is in the room or not. The room has only 1 entrance and unlike most tutorials online, all I've got are 3 PIR sensors. No option for a laser, sorry people. All of the sensors are very weird by the fact that their jumpers work only in the H position and not the L position. Anyways...

All I want is a boolean variable to invert every time a motion is detected. I just tried:

bool RO = false;// RO stands for Room Occupied?
void Loop(){
bool SensorInput = digitalRead(15);//15 is the pin of the esp32 to which the sensor is connected
if(SensorInput){
RO = !RO;
Serial.println(RO);
}
}

but little did I think about the number of clock cycles, the board goes through while the sensor is in the HIGH state position, and NOW all I get is just the variable just cycling b/n 1 and 0. I just want the variable to be flipped on every motion detection.

Now I think there is something I am missing here and hopefully some can inform me. This is my first post in this community and I have read enough issues of various people on this forums to know that I can expect valid answers unlike yahoo answers(filled with spammers, advertisers and silly people). I am trying not to purchase anything more as this project is already expensive. I am hoping that there is a solution with the motion sensor.

I cannot thank everyone enough for their support here. However, I am running on a deadline. If anyone can help me fast, I would DEFINITELY appreciate it. Thanks people!

Kshitij Kulkarni

You should detect the transition from not active sensor to active sensor, not the state of the sensor.

State Change Detection: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection.

If you want to use the moment that the PIR becomes active (the event), then use the State Change Detection.
If you want to know if the PIR is active (the state), then do a digitalRead().

Complex logic problems are best resolved with a "flow chart".
Paul

First, state in words how you will use three MOTION sensors to determine room occupancy.

What happens if the room is occupied, but no one is moving?

Hey Jremington!
The PIR sensor will not be located in the room.
Instead, it will be located on the doorway that leads to the room.
If someone enters the the room(through its only doorway(the one where the sensor is keeping an eye)), then the sensor will detect the motion of the person entering the room and will know that there is a person in the room.
If the sensor detects the motion again, it must be that of the same person leaving the room, and hence will know that the room is no longer occupied. For now, I am limiting the number of people who could enter the room to 1.
And another thing.
I do not plan on using all three of the sensors in this case. I just mentioned it, because if there be a requirement for more sensors, I had a few more.

Hope this reply helps.

Kshitij Kulkarni

Railroader and Koepel,
Thanks to both of you for helping. I am amazed.
For my 3 years with Arduino, never have I ever come across State change detection. You guys really made my whole life easier. I couldn't thank you enough.

Paul, thank you. I have not been the best one at solving logic issues, and turns out the flowchart really does help.
I don't know WHY I had never thought of it before. Anyways, thanks mate.

kshitij_kulkarni:
If the sensor detects the motion again, it must be that of the same person leaving the room, and hence will know that the room is no longer occupied.

Or a second person entered. Or they turned around and went back in without leaving the detection area. Or a bug crawled across the sensor. Or the sensor failed to see them leave and now the count is wrong. Or... any number of other things.

For now, I am limiting the number of people who could enter the room to 1.

How will you do that?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.