Reading the input from proximity sensors in an order

Hello Guys,
I have been trying to make a bidirectional visitor counter, using 2 IR proximity sensors. To do this I want to read the order in which the sensors go HIGH, i.e (if they go high like A then B the increment it and if B then A decrement). Can anyone please give me with an idea how i should go about this. Just an idea would do.

Save the value of millis() when each sensor is activated.

If Btime is greater than Atime then the order was A then B
If Atime is greater than Btime then the order was B then A

I had just thought of something similar. A method where i can see the time at which the sensors were triggered. But Thank You all the same. Highly appreciated.

Basing on your advice I have written this

[code][/begins]
const int proximity1 = 2;       //proximity 1 
const int proximity2 = 3;       //proximity 2
const int led1 = 13;
unsigned long Millis1 = 0;      //stores the time when proximity 1 was triggered
unsigned long Millis2 = 0;      //same as above
void setup()
{
  pinMode(proximity1, INPUT);
  pinMode(proximity2, INPUT);
  pinMode(led1, OUTPUT);
  
}

void loop()
{
  if (( digitalRead (proximity1) == HIGH) && (digitalRead(proximity2) == LOW))
  {
    Millis1 = millis();
  }

  if ((digitalRead(proximity2) == HIGH) && (digitalRead(proximity1) == LOW))
  {
    Millis2 = millis();
  }

  if (Millis1 > Millis2)
  {
    digitalWrite(led1, HIGH);
    Millis1 = 0;
    Millis2 = 0;
  }
  
  if (Millis2 > Millis1)
  {
    digitalWrite(led1, LOW);
    Millis1 = 0;
    Millis2 = 0;
  }
}

[][/ends]
The problem I am facing here is every time the proximity 1 reads HIGH the Led goes HIGH and since the person is also supposed to pass through proximity 2 while coming into the room, the Led goes LOW.
Forgive me for my not so great programming skills. I am a newbie. any help is appreciated. Thank You

The problem I am facing here is every time the proximity 1 reads HIGH the Led goes HIGH and since the person is also supposed to pass through proximity 2 while coming into the room, the Led goes LOW.

What is supposed to happen ?

Hey UKHeliBob,

The system is supposed to detect if the person in coming into the room or going out. This would be achieved by determining the direction in which the sensors are getting excited. For which I followed your advice and wrote the code above.

What is happening now, is every time I move in or out of the room, while I cross proximity-1 to go IN, the led goes HIGH(as expected), but immediately after that since I am supposed to cross both the sensors while on my way IN, the proximity-2 goes HIGH and that instructs the Led to go LOW.
And while on the way out, since I will cross proximity-2 before proximity-1. The Led stays LOW, and when I cross Proximity-1, Led goes HIGH again.

I hope I was clear? and not too verbose
Thank You !

Hi,
You need to detect WHEN the sensor goes form LOW to HIGH, not WHEN it is HIGH.

You need to detect the change in the sensor output, the constant state of the sensor.

Tom.... :slight_smile:

ohh yess!! i did something very similar recently!
I am gonna try it and let you know asap :slight_smile:

TomGeorge thanks a ton! I wrote a preliminary program, doing the job and it works like a charm!!
Thanks for the help once again!.. I will surely let you guys know when the whole project is done and probably post the refined code in some website. :wink:

Hey,
After brainstorming and keeping your ideas in mind, I wrote this program from scratch.

const int proximity_1 = 2;      // IR sensor 1
const int proximity_2 = 3;      // IR sensor 2
unsigned long Millis_1 = 0;     // time when sensor 1 was excited
unsigned long Millis_2 = 0;     // same as above
int currentState_1 = 0;     // current state of sensor 1
int currentState_2 = 0;     // current state of sensor 2
int previousState_1 = 0;
int previousState_2 = 0;
int count = 0;      //number of people in the room
void setup()
{
  pinMode(proximity_1, INPUT);
  pinMode(proximity_2, INPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  IN();
  OUT();
  if (Millis_1 > Millis_2)
  {
    digitalWrite(13, LOW);
  }
  if (Millis_2 > Millis_1)
  {
    digitalWrite(13, HIGH);
  }

  if (count <= 0)
  {
    count = 0;
  }
}
void IN()
{
  currentState_1 = digitalRead(proximity_1);      // checking a change in state of sensor 1
  if (currentState_1 != previousState_1)
  {
    if (currentState_1 == HIGH)
    {
      Millis_1 = millis();
     // Serial.print("the proximity_1 was turned on at ");
      //Serial.println(Millis_1);
      Serial.print("No. of people in the room = ");
      Serial.println(count);
      count++;
    }
    previousState_1 = currentState_1;
  }

}
void OUT()
{
  currentState_2 = digitalRead(proximity_2);      // checking the state change of sensor 2
  if (currentState_2 != previousState_2)
  {
    if (currentState_2 == HIGH)
    {
      Millis_2 = millis();
      //Serial.print("the proximity_2 was turned on at ");
      //Serial.println(Millis_2);
      Serial.print("No. of people in the room = ");
      Serial.println(count); 
      count--;
    }
    previousState_2 = currentState_2;
  }
}

This program is supposed to count the number of people and display that count in the Serial monitor.
But for some unknown reason the count increments by several times in just one pass i.e when I move from sensor 1 to sensor 2 once. and vice versa.
If anyone can point out the error in my logic..
Thank You