NEED HELP...!!! Two IR Proximity sensors to control two LEDs

Hello everyone! I’m doing a project which uses two IR Proximity sensors (GP2Y0A02YK0F) to control two LEDs simultaneously.
Normally LED_1 output remains HIGH. When an object passes through Sensor_1 and Sensor_2 consecutively, LED_2 output will remain HIGH and LED_1 output will remain LOW at the same time.
Similarly, when the object passes through Sensor_2 and Sensor_1 consecutively, LED_1 output will remain HIGH and LED_2 output will remain LOW at the same time.
I’m gratefully looking forward to your help.
Thanks in advance.

I’ve attached my code here, but it didn’t work as I expected. :frowning:

void setup() 
{
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);

  Serial.begin(9600);
  digitalWrite(11,HIGH);
}

void loop() 
{
int a = analogRead(A0);
int b = analogRead(A1);
  if (b>=1000)
  {
    digitalWrite(11,HIGH);
    digitalWrite(12,LOW);
    delay(5000);
  }
  if (a>=1000)
  {
    digitalWrite(11,LOW);
    digitalWrite(12,HIGH);
    delay(5000);
  }

Serial.print(a);
Serial.print("   ");
Serial.println(b);
Serial.println(   );
}

What do you mean "passes through"? When passing from Sensor1 to Sensor2, is there still a reading on Sensor1 at the point when Sensor2 reaches its trigger threshold? So you could look for both of them being occupied at the same time and just remember which one was the last to trigger.

If the sensors are so far apart that you leave Sensor1 entirely before reaching Sensor2, then you need to record the time using the millis() function.

Thanks for your attention.
The sensors are installed in a fall-ceiling near a door with a distance about 12 inches between each other. Normally LED_1 output remains HIGH. When a man enters into the door going under its sensing range consecutively, then LED_2 output will go HIGH and LED_1 output will go LOW at the same time.
It means, the LED will be ON when it gets two output signals consecutively from the OUT terminal of the sensors.
Its like the occupancy detecting method, but I don't want to use a PIR or an Ultrasonic sensor to do it.
Thanks again.

Hi Arif,

What exactly is going wrong?

Just to be clear: you want LED_1 to be on and LED_2 to be off, until someone walks through the door, triggers sensor_1 and then sensor_2 which makes LED_1 to turn off and LED_2 to turn on.
If someone is leaving you want the opposite to happen. Is that correct?

So when you try it out what happens? Why does it not work?
In your code you have some serial output to check what the values are which are indeed good for debugging. What do these values say?

Also try to write out what your code should do in a diagram for yourself. Also why do you have such a long delay?

Yeah, that's exactly what I want.
In my code, LEDs output gets swapped only when the first sensor gets triggered.
That's why I set a 5 seconds delay for the next sensor. It doesn't affect within 5 seconds.
But when a man moves too slowly that requires more than 5 seconds to cross a distance between two sensors or too fast to go and return through the sensor ranges, my objective fails.
For this reason, I'm in need of a sketch that requires two sensors to be triggered consecutively to swap the outputs of the LEDs.
Thank you.

ah okay,

So to do that you need a code that does the folowing:

  1. constantly check the sensor output (which you already do)

  2. if a value is above 1000 set a flag to 1 for that sensor

  3. if within a certain time (the time it takes to walk from one sensor to the other) the other value is also triggered switch the LED’s

  4. if nothing happens after a while (like the 5 seconds) reset to normal

For time keeping you can look into the micros()/millis(function) time functions

hope that helps you on your way

Thank you so much for the instructions. :slight_smile:
I'd be very grateful if you modify the codes for me please, as I'm not that much expert with those functions. :frowning:

Sorry but i am not going to write your code for you. Try it yourself and ask for help if you get stuck.

Have a look at this post and see how far you can get.
beginner guide for milis()