How to use 2 IR Proximity Sensor?

Greeting to all,

I am working on automatic stair lighting controlled by using an IR Proximity sensor but I think something wrong in my coding part can anyone help me,,,

int IRSensor1  = 34;// down 
int IRSensor2  = 35;// up   

void setup() {
  pinMode (IRSensor1, INPUT_PULLUP); //down 
  pinMode (IRSensor2, INPUT_PULLUP); //up 

  pinMode(15,OUTPUT); //1st 
  pinMode(2,OUTPUT);  //2nd 
  pinMode(4,OUTPUT);  //3rd 
  pinMode(17,OUTPUT); //4th 
  pinMode(5,OUTPUT);  //5th 
  pinMode(18,OUTPUT); //6th 
  pinMode(19,OUTPUT); //7th 
  pinMode(21,OUTPUT); //8th
  pinMode(1,OUTPUT);  //9th
  pinMode(22,OUTPUT); //10th
  pinMode(23,OUTPUT); //11th
  pinMode(25,OUTPUT); //12th
  pinMode(26,OUTPUT); //13th
  pinMode(27,OUTPUT); //14th
  pinMode(32,OUTPUT); //15th
    
 
}

void loop() {
 int statusSensor1 = digitalRead (IRSensor1);
 int statusSensor2 = digitalRead (IRSensor2);

 if (statusSensor1 ==1 && statusSensor2 ==0)
 {
   digitalWrite(15,LOW);
   delay(500);
   digitalWrite(2,LOW);
   delay(500);
   digitalWrite(4,LOW);
   delay(500);
   digitalWrite(17,LOW);
   delay(500);
   digitalWrite(5,LOW);
   delay(500);
   digitalWrite(18,LOW);
   delay(500);
   digitalWrite(19,LOW);
   delay(500);
   digitalWrite(21,LOW);
   delay(500);
   digitalWrite(1,LOW);
   delay(500);
   digitalWrite(22,LOW);
   delay(500);
   digitalWrite(23,LOW);
   delay(500);
   digitalWrite(25,LOW);
   delay(500);
   digitalWrite(26,LOW);
   delay(500);
   digitalWrite(27,LOW);
   delay(500);
   digitalWrite(32,LOW);
   delay(500);
   digitalWrite(15,HIGH);
   delay(500);
   digitalWrite(2,HIGH);
   delay(500);
   digitalWrite(4,HIGH);
   delay(500);
   digitalWrite(17,HIGH);
   delay(500);
   digitalWrite(5,HIGH);
   delay(500);
   digitalWrite(18,HIGH);
   delay(500);
   digitalWrite(19,HIGH);
   delay(500);
   digitalWrite(21,HIGH);
   delay(500);
   digitalWrite(1,HIGH);
   delay(500);
   digitalWrite(22,HIGH);
   delay(500);
   digitalWrite(23,HIGH);
   delay(500);
   digitalWrite(25,HIGH);
   delay(500);
   digitalWrite(26,HIGH);
   delay(500);
   digitalWrite(27,HIGH);
   delay(500);
   digitalWrite(32,HIGH);
   delay(500);   
}

else if (statusSensor1 ==0 && statusSensor2 ==1)
{
   digitalWrite(32,HIGH);
   delay(500);
   digitalWrite(27,HIGH);
   delay(500);
   digitalWrite(26,HIGH);
   delay(500);
   digitalWrite(25,HIGH);
   delay(500);
   digitalWrite(23,HIGH);
   delay(500);
   digitalWrite(22,HIGH);
   delay(500);
   digitalWrite(1,HIGH);
   delay(500);
   digitalWrite(21,HIGH);
   delay(500);
   digitalWrite(19,HIGH);
   delay(500);
   digitalWrite(18,HIGH);
   delay(500);
   digitalWrite(5,HIGH);
   delay(500);
   digitalWrite(17,HIGH);
   delay(500);
   digitalWrite(4,HIGH);
   delay(500);
   digitalWrite(2,HIGH);
   delay(500);
   digitalWrite(15,HIGH);
   delay(500);
   digitalWrite(32,LOW);
   delay(500);
   digitalWrite(27,LOW);
   delay(500);
   digitalWrite(26,LOW);
   delay(500);
   digitalWrite(25,LOW);
   delay(500);
   digitalWrite(23,LOW);
   delay(500);
   digitalWrite(22,LOW);
   delay(500);
   digitalWrite(1,LOW);
   delay(500);
   digitalWrite(21,LOW);
   delay(500);
   digitalWrite(19,LOW);
   delay(500);
   digitalWrite(18,LOW);
   delay(500);
   digitalWrite(5,LOW);
   delay(500);
   digitalWrite(17,LOW);
   delay(500);
   digitalWrite(4,LOW);
   delay(500);
   digitalWrite(2,LOW);
   delay(500);
   digitalWrite(15,LOW);
   delay(500);
}
}

Have you tried printing values?

(Waaaay too much code, BTW)

Why do you think that?

no, i don't try yet

The problem is my all the LEDs keep blinking

"My car makes a funny noise" is just as descriptive.

You need to know what the IRSensors are reading in the loop, but I suspect that the LEDS are doing exactly what the code says.

There are a lot of delays in here which make it hard to troubleshoot. Consider commenting out what you have now inside the if statements and replace them with Serial prints. Doing it this way makes it much easier to figure out how the code will react without it interacting with your existing circuitry. I am also not familiar with the sensor you are using, but i assume it changes from high to low and whatnot? If so, you likely do not need to have the INPUT_PULLUP in there and can just use INPUT since most sensors will control the line. By this i mean when the sensor is low, it actually grounds the pin, and when it is high, it will hold it high, so having the pin pulled up is unnecessary and it may have conflict with the sensor (again, depends on the sensor since some will leave the pin floating, but i haven't encountered many of these). Also a good practice is to use more brackets in if statements that have multiple arguments such as if((StatusSensor1 == True) &&(StatusSensor2 == False)) ... This prevents the code from confusing things in the future

You didn't tell us which Arduino.
Pin1 on a Mega (educated guess) is for USB serial communication, so can't be used.

Which IR sensor (web link please).
Did you use current limiting resistors for your LEDs, and what value.

Study arrays.
A pin array (Google it) could make your code a lot shorter.
Leo..

  1. I am using esp32, which have gpio1(tx0)
  2. e18-d80nk

yes man, but i want when I cross the first sensor my all LEDs on after delay it will go off, permanently,
same with the second sensor

my circuit diagram but i am using esp32

So you need to print out what the sensors are reading, as pointed out in the first post by @anon73444976 .

It is likely that one of them is not adjusted correctly (positioned), and is showing a permanently triggered state.

e18-d80nk seems to be an NPN type sensor, in which case you can connect sensor output directly to an ESP pin with internal pull up (or external pull up to 3.3volt).

I assume you have current limiting resistors for the unspecified LEDs.
And that the LEDs are drawing no more than 50mA each, because of the 1k resistors and the 100mA signal transistors.

The 7805 must be used with decoupling caps.
Leo..

1 Like

OK, I made it my project successfully,
Actually, a little problem was in my circuit and program to I solved it

here is a program you can also try it,

int IRSensor1 = 34; // 1st sensor
int IRSensor2 = 39; // 2nd sensor 
 
int LED1 = 15;  //1st  
int LED2 = 2;   //2nd 
int LED3 = 4;   //3rd 
int LED4 = 17;  //4th 
int LED5 = 5;   //5th
int LED6 = 18;  //6th
int LED7 = 19;  //7th
int LED8 = 21;  //8th
int LED9 = 1;   //9th 
int LED10 = 22; //10th
int LED11 = 23; //11th
int LED12 = 25; //12th
int LED13 = 26; //13th
int LED14 = 27; //14th
int LED15 = 32; //15th

void setup()
{
  pinMode (IRSensor1, INPUT); // sensor pin INPUT
  pinMode (IRSensor2, INPUT);
  pinMode (LED1, OUTPUT); // Led pin OUTPUT
  pinMode (LED2,OUTPUT);
  pinMode (LED3, OUTPUT);
  pinMode (LED4, OUTPUT);
  pinMode (LED5, OUTPUT);
  pinMode (LED6, OUTPUT);
  pinMode (LED7, OUTPUT);
  pinMode (LED8, OUTPUT);
  pinMode (LED9, OUTPUT);
  pinMode (LED10, OUTPUT);
  pinMode (LED11, OUTPUT);
  pinMode (LED12, OUTPUT);
  pinMode (LED13, OUTPUT);
  pinMode (LED14, OUTPUT);
  pinMode (LED15, OUTPUT); 
}

void downTOup()
{
int statusSensor1 = digitalRead (IRSensor1);

if (statusSensor1 == 1)
{
digitalWrite(LED1, LOW); // LED LOW
delay(500);
digitalWrite(LED2, LOW); // LED LOW
delay(500);
digitalWrite(LED3, LOW); // LED LOW
delay(500);
digitalWrite(LED4, LOW); // LED LOW
delay(500);
digitalWrite(LED5, LOW); // LED LOW
delay(500);
digitalWrite(LED6, LOW); // LED LOW
delay(500);
digitalWrite(LED7, LOW); // LED LOW
delay(500);
digitalWrite(LED8, LOW); // LED LOW
delay(500);
digitalWrite(LED9, LOW); // LED LOW
delay(500);
digitalWrite(LED10, LOW); // LED LOW
delay(500);
digitalWrite(LED11, LOW); // LED LOW
delay(500);
digitalWrite(LED12, LOW); // LED LOW
delay(500);
digitalWrite(LED13, LOW); // LED LOW
delay(500);
digitalWrite(LED14, LOW); // LED LOW
delay(500);
digitalWrite(LED15, LOW); // LED LOW
//delay(500);
}

else
{
digitalWrite(LED1, HIGH); // LED LOW
delay(500);
digitalWrite(LED2, HIGH); // LED LOW
delay(500);
digitalWrite(LED3, HIGH); // LED LOW
delay(500);
digitalWrite(LED4, HIGH); // LED LOW
delay(500);
digitalWrite(LED5, HIGH); // LED LOW
delay(500);
digitalWrite(LED6, HIGH); // LED LOW
delay(500);
digitalWrite(LED7, HIGH); // LED LOW
delay(500);
digitalWrite(LED8, HIGH); // LED LOW
delay(500);
digitalWrite(LED9, HIGH); // LED LOW
delay(500);
digitalWrite(LED10, HIGH); // LED LOW
delay(500);
digitalWrite(LED11, HIGH); // LED LOW
delay(500);
digitalWrite(LED12, HIGH); // LED LOW
delay(500);
digitalWrite(LED13, HIGH); // LED LOW
delay(500);
digitalWrite(LED14, HIGH); // LED LOW
delay(500);
digitalWrite(LED15, HIGH); // LED LOW
//delay(500);
}
}

void upTOdown()
{
int statusSensor2 = digitalRead (IRSensor2);

if (statusSensor2 == 1)
{
digitalWrite(LED15, LOW); // LED LOW
delay(500);
digitalWrite(LED14, LOW); // LED LOW
delay(500);
digitalWrite(LED13, LOW); // LED LOW
delay(500);
digitalWrite(LED12, LOW); // LED LOW
delay(500);
digitalWrite(LED11, LOW); // LED LOW
delay(500);
digitalWrite(LED10, LOW); // LED LOW
delay(500);
digitalWrite(LED9, LOW); // LED LOW
delay(500);
digitalWrite(LED8, LOW); // LED LOW
delay(500);
digitalWrite(LED7, LOW); // LED LOW
delay(500);
digitalWrite(LED6, LOW); // LED LOW
delay(500);
digitalWrite(LED5, LOW); // LED LOW
delay(500);
digitalWrite(LED4, LOW); // LED LOW
delay(500);
digitalWrite(LED3, LOW); // LED LOW
delay(500);
digitalWrite(LED2, LOW); // LED LOW
delay(500);
digitalWrite(LED1, LOW); // LED LOW
//delay(500);
}

else
{
digitalWrite(LED15, HIGH); // LED LOW
delay(500);
digitalWrite(LED14, HIGH); // LED LOW
delay(500);
digitalWrite(LED13, HIGH); // LED LOW
delay(500);
digitalWrite(LED12, HIGH); // LED LOW
delay(500);
digitalWrite(LED11, HIGH); // LED LOW
delay(500);
digitalWrite(LED10, HIGH); // LED LOW
delay(500);
digitalWrite(LED9, HIGH); // LED LOW
delay(500);
digitalWrite(LED8, HIGH); // LED LOW
delay(500);
digitalWrite(LED7, HIGH); // LED LOW
delay(500);
digitalWrite(LED6, HIGH); // LED LOW
delay(500);
digitalWrite(LED5, HIGH); // LED LOW
delay(500);
digitalWrite(LED4, HIGH); // LED LOW
delay(500);
digitalWrite(LED3, HIGH); // LED LOW
delay(500);
digitalWrite(LED2, HIGH); // LED LOW
delay(500);
digitalWrite(LED1, HIGH); // LED LOW
//delay(500);
}
}


void loop (){
  downTOup();
  upTOdown();
}

Now is a really good time to learn about arrays.
And the const keyword.
And maybe fix the cut-and-paste comments

1 Like

sure!

What about reading this, it has actual examples
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

1 Like

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