2 pir sensors working together

Hello
I have done this before(with help from here) and now I have notice I have to many "False alarm" so I'm trying to rewrite the code
what I have notice is that my sensor is gibing me "0" for ~3 sec when it detect motion which cause me a lot of problems
for now I want to know if someone has move from left to right \right to left
so I have thought to do something like this
when a sensor is going to "0" - it mean he "saw" something , and while he is still a "0" he wait until the other sensor will turn to "0" at well
that why I will know someone moved all over the sensors

but I think I'm missing something

void SensorCount ()
{

  if(digitalRead(LeftPIR)==LOW)//left sensor see something 
  {
    Serial.println("left low 1");
    while (digitalRead(LeftPIR)==LOW)
    {
      Serial.println("**");
      if(digitalRead(RightPIR)==LOW)
      {
        Serial.println("Right1");
        if(Direction==false)
        {
          EnterCount++;
          Serial.println("enter");
          break;
        }
        else if(Direction==true)
        {
          OutCount++;
          Serial.println("exit");
          break;
        }
        break;
      }
    }

    Serial.println("just left");


  } //end if left==low

   if(digitalRead(RightPIR)==LOW)//right sensor see something 
  {
    Serial.println("right low 1");
    while (digitalRead(RightPIR)==LOW)
    {
      Serial.println("**");
      if(digitalRead(LeftPIR)==LOW)
      {
        Serial.println("left");
        if(Direction==false)
        {
          EnterCount++;
          Serial.println("enter");
          break;
        }
        else if(Direction==true)
        {
          OutCount++;
          Serial.println("exit");
          break;
        }
        break;
      }
    }

    Serial.println("just right");


  } //end if left==low
}//end of SensorCount

Thanks ,

Hi,
I have troubles to get some 'feeling' with your code. My general way to deal with this, is to gather the sensor data, keep the 'state' in variables and decide what to do according the 'state' and sensor data. However, you would have to rewrite your sketch for that. Perhaps someone else can make your sketch work.

Did you have a look inside the PIR housing ? perhaps a spider or a bug is walking over the PIR sensor.
There are PIR sensors with two sensors above each other to prevent false alarm. Some sensors combine PIR with radar detection.

Can you tell how your PIR sensors are orientated ? Did you set them at an angle, or do they overlap ?
They could be set straight forward (or a little angle) and use a divider in between, so they can only detect their own part.

every sensor have it's own area
left sensor is 0-80 degrees , and right sensor is 100-180
I'm using 2 separated sensor

what do you mean by "save" the data of each sensor
can you show me how to do this ?
I have try to read and save the sensor output
but maybe I did it wrong , so this is why I went this way(the "while - do").

Thanks ,

I assume the left sensor is pir number 1 ?

Initialize the 'state'

boolean pirOn1 = false;
boolean pirOn2 = false;
boolean pirPrevious1 = false;
boolean pirPrevious2 = false;

Gather the information, detect a change and act upon that

pirOn1 = digitalRead(...
pirOn2 = digitalRead(...

if (!pirPrevious1 && pirOn1 && pirOn2 && pirPrevious2)
{
  // pir1 just became active, while pir2 was already active
  motion (DETECTED_LEFT);
}
else if (!pirPrevious2 && pirOn2 && pirOn1 && pirPrevious1)
{
  // pir2 just became active, while pir1 was already active
  motion (DETECTED_RIGHT);
}

pirPrevious1 = pirOn1;
pirPrevious2 = pirOn2;

A seperate function that does show the alarm.

#define DETECTED_LEFT 100
#define DETECTED_RIGHT 101

void motion( int detect)
{
  if (detect == DETECTED_LEFT)
  {
    ....
  }
  else if (detect == DETECTED_RIGHT)
  {
    ....
  }
  else
  {
    // This should not happen, you could print "error" or so.
  }
}

You might have to slow the sketch down. Detect the PIR for example 5 times a second.

void loop()
{
  ...
  delay(200);
}

I hope this makes sense.
The sketch can be extended by storing the time (using the millis() function) for the PIR to become active. That way you could tell how fast someone is moving :stuck_out_tongue:
And that could be stored into a class. A class for each PIR sensor.
Then you could have more PIR sensors, for example to detect at different heights, so you can tell if it is a dog or a person.

this is what I have done
my full code

#include <SPI.h>
#include <Ethernet.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

#define DETECTED_LEFT 100
#define DETECTED_RIGHT 101

byte mac[] = {
  0xD8, 0xBB, 0xB1, 0xE1, 0xFA, 0xE5 };
IPAddress ip(10,0,0,15);
EthernetServer server(80);

int EnPIR=3;//auto-manual switch
int ManualGreen=4;//Green button
int ManualRed=5;//Red button
int PIRDirection=6;//Direction button
int RightPIR=7;//right sensor
int LeftPIR=8;//left sensor

int MoveLED=9;//Output LED

int Battary_value=1;//analog battery sensor

boolean Direction=false;
boolean Manual=true;
int EnterCount=0;
int EnterOldCount=0;

int OutCount=0;
int OutOldCount=0;

int SensorDelay=950;
int RightSensorDelay=1050;

boolean PirRightOn = false;
boolean PirLeftOn = false;
boolean PirRightPrevioes = false;
boolean PirLeftPrevioes = false;



int x,i=0;
void setup() {
  pinMode(EnPIR,INPUT_PULLUP);
  pinMode(ManualGreen,INPUT_PULLUP);
  pinMode(ManualRed,INPUT_PULLUP);
  pinMode(PIRDirection,INPUT_PULLUP);
  pinMode(RightPIR,INPUT_PULLUP);
  pinMode(LeftPIR,INPUT_PULLUP);
  pinMode(MoveLED,OUTPUT);

  digitalWrite(MoveLED,HIGH);

  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Wire.begin();
  for (int y=0;y<20;y++)
  {
    Serial.print("**");
    Serial.print("wait");
    delay(1000);
  }
  Serial.println(" ");

  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println(" done");
  Serial.print("Upload file is - ");
  Serial.println(F(__FILE__));

  digitalWrite(MoveLED,LOW);
  Serial.println("Sensor now  working :-) " );

} 


void loop() {

  OperationCheck ();
  if (Manual==false)// work automatic
  {
    //Serial.println("Auto");
    DirectionCheck ();
    SensorStat ();
  }
  else if(Manual==true)//work manual 
  {
     Button ();
  }
  EthernetClient client = server.available();
  if (client) {
   boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (c == '\n' && currentLineIsBlank) {
          
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 1");  
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("<span style=color:red;font-weight:bold>Enter - </span>");
          if (EnterOldCount==EnterCount)
          {
           client.print(EnterCount);
            client.println("
");
          }
          else 
          {
            x=EnterCount-EnterOldCount;
           client.print(EnterCount);
            EnterOldCount=EnterCount;
            client.println("
");            
          }
          client.print("<span style=color:green;font-weight:bold>Exit - </span>");
          if (OutOldCount==OutCount)
          {
           
            client.print(OutCount);
           }
          else 
          {
            i=OutCount-OutOldCount;
            client.print(OutCount);
            OutOldCount=OutCount;
              
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
   
  }
}



void DirectionCheck ()
{
  if (PIRDirection==HIGH)
  {
    Direction=false;//right to left
  }
  else
  {
    Direction=true;//left to right
  } 

}

void SensorStat ()
{
  PirRightOn=digitalRead(RightPIR);
  PirLeftOn=digitalRead(LeftPIR);
  if (!PirRightPrevioes && PirRightOn && PirLeftOn  && PirLeftPrevioes)
  {
    motion (DETECTED_LEFT);
  }
  else if  (!PirLeftPrevioes && PirLeftOn && PirRightOn  && PirRightPrevioes)
  {
    motion (DETECTED_RIGHT);
  }
 PirRightPrevioes=PirRightOn;
 PirLeftPrevioes=PirLeftOn;

}//end of SensorStat


void OperationCheck ()
{
  if (digitalRead(EnPIR)==HIGH)
  {
    Manual=true;
  }
  else if (digitalRead(EnPIR)==LOW)
  {
    Manual=false;
  }

}

void Button ()
{
  if (digitalRead(ManualRed)==LOW)
  {
 
    EnterCount++;
    Serial.println("RED ON");
  }
  if (digitalRead(ManualGreen)==LOW)
  {
    OutCount++;
    Serial.println("GREEN ON");
  }


}

void motion (int detected)
{
  if (detected == DETECTED_LEFT)
  {
    Serial.println("left move");
    EnterCount++;
    }
    else if (detected == DETECTED_RIGHT)
   {
    Serial.println("right move");
   } 
  
  
}

not working as you said it will
when I just move my hand in front of one sensor - I get count of 73
I want it only to count when I move from 1 sensor to the other
the sensor is on "1" when no movement
so what I want it to do is this :
only when he see that sensor1 was "1" then "0" then sensor 2 was "1" then "0"
he will know there was a movement
do you understand what I'm trying to explain ?
now I get only when one sensor is down - doesn't help me with what I need

Thanks ,

Could you try a part and add a new part.

Is the PIR output high or low, when a motion is detected ?
I'm not sure what you mean, is the digitalRead HIGH when there is no motion ?
In that case use this:

// PIR is low active
if (digitalRead(RightPIR) == LOW)
  PirRightOn = true;
else
  PirRightOn = false;

if( digitalRead(LeftPIR) == LOW)
  PirLeftOn = true;
else
  PirLeftOn = false;

The code to detect left- and right-motion could be tested all the time. You only test it if "Manual" is false. If both PIR sensors are 'on' at that point, it doesn't detect a left-motion or right-motion. The "Manual" is set false when EnPIR signal is LOW. I don't know what that means.

The program flow is hard to understand, and the sketch could use more comment. Perhaps a schematic is helpful to understand it.

I also helps to have good names. For example 'PIRDirection' is a pin number of a button. You could call it 'pinButtonDirection'.
There is already a mistake with it when you have: if (PIRDirection==HIGH)
That is wrong, since it is the number of a pin.

maybe this will help understand