Difficulty in programming 3 IR sensors simultaneouly to Arduino UNO

I am building a home made egg incubator. For this purpose I need to move the egg trays up and down, so i have arranged 3 IR sensors successively inline on the inside vertical face of the incubator box.As the platform moves it should remain for 1 hour in the middle when sensed by the middle sensor.After 1 hour it should go up and remain there for 4 minutes and again come down to middle and stay for 1 hour and then go down and remain for 4 minutes.It should stop whenever it senses any of the sensors with the programmed delay no matter what the order in which it moves.I have designed the structure according.I am using LM298 motor driver module and a DC motor for the movement.The circuit is fine according to my knowledge.Working on this code for many days but still not able to program it for doing this task.

Using this IR sensor:-

https://robu.in/product/ir-infrared-obstacle-avoidance-sensor-module/?gclid=CjwKCAiAgqDxBRBTEiwA59eEN3p2QA2zcP4V-guuNHVETGvnaRbTfHRki7iIrCq_1S3XUG9k-i6I2xoCsc0QAvD_BwE

The code I tried but does not work:-

int In3=3;
int In4=4;
int ENA2=9;
int SPEED=210;
const int sensorPin1=2;
const int sensorPin2=13;
const int sensorPin3=7;
boolean flag1=false;
boolean flag2=false;
boolean flag3=false;


void setup() {
 pinMode(2,INPUT);
 pinMode(13,INPUT);
  pinMode(7,INPUT);
 pinMode(In3,OUTPUT);
 pinMode(In4,OUTPUT);
 pinMode(ENA2,OUTPUT);
 analogWrite(ENA2,150);
 
 
}


void loop()
{
   digitalWrite(In4,HIGH);
   digitalWrite(In3,LOW);
 


if((digitalRead(13)==LOW && flag1==false))                 

   {
   digitalWrite(In4,LOW);
   digitalWrite(In3,LOW);
   delay(36,00,000);
   flag1 = true;
   }


else   if((digitalRead(13)==LOW && flag1==true))

   {
   digitalWrite(In4,HIGH);
   digitalWrite(In3,LOW);
   flag1=false; 
   }


if((digitalRead(2)==LOW && flag2==false))                 

   {
   digitalWrite(In4,LOW);
   digitalWrite(In3,LOW);
   delay(2,40,000);
   flag2 = true;
   }

else if((digitalRead(2)==LOW && flag2==true))

   {
   digitalWrite(In4,HIGH);
   digitalWrite(In3,LOW);
   flag2=false; 
   }

   
if((digitalRead(7)==LOW && flag3==false))                 

   {
   digitalWrite(In4,LOW);
   digitalWrite(In3,LOW);
   delay(2,40,000);
   flag3 = true;
   }


else  if((digitalRead(7)==LOW &&flag3==true))

   {
   digitalWrite(In4,HIGH);
   digitalWrite(In3,LOW);
   flag3=false; 
   }
 
}

Welcome to the forum. Congratulations for using code tags with your code!

As for your code, there is not 1 single comment. Comments help humans understand what the intent is.

You go to the trouble of defining your sensor pins, but then never use them through the code

const int sensorPin1=2;
//...
void setup {
  pinMode(sensorPin1, INPUT);
//...

Your variable names should be much more descriptive. If you have 3 sensors, may they are the upperSensor, middleSensor and lowerSensor. Those make the code much more readable as well. Same for your flag variables.

You can not use commas in large numbers like you did in delay().

It might be better to structure your code like a state machine and know which state you are in (moving, resting at the top,middle, bottom) and then react accordingly.

Look at the example BlinkWithoutDelay (File->Examples->02.Digital->BlinkWithoutDelay) to learn how to measure elapsed time without using delay().

@blh64

Thanks for the help,I'll for sure try to understand the concept the way you told. I am just a tyro to arduino as well as programming so learning it by looking the videos and going through the net.

It's more to ask but if possible can you help me with the code of atleast one sensor.

Have you gotten the sample code in the user manual to work properly? If so it is the basic code to put in your program.

Paul

Hey Paul thanks for the guidance. The code that I could figure out from the example blinkwithoutdelay is as follow. I was able to write this much only.

const int In3=3;
const int In4=4;
const int ENA2=9;
const int SPEED=210;
const int sensorPin1=2;  // For stopping at the middle (centre sensor,completely horizontal)
const int sensorPin2=7;  // For stopping at the upper position (top sensor,inclined position)
const int sensorPin3=13;  // For stopping at the lower position (lower sensor,inclined position)

int sensorPin1State= HIGH;
const long interval1 = 2000;  // Done according to the LED blinkwithoutdelay example
int sensorPin2State= HIGH;
const long interval2 = 2000;
int sensorPin3State= HIGH;
const long interval3 = 2000;



void setup() {
  pinMode(sensorPin1,INPUT);
  pinMode(sensorPin2,INPUT);
  pinMode(sensorPin3,INPUT);
  pinMode(In3,OUTPUT);
  pinMode(In4,OUTPUT);
  pinMode(ENA2,OUTPUT);
  analogWrite(ENA2,150);
  
  
}


void loop()
{
  
 

if((digitalRead(sensorPin1)==LOW))

    {
    digitalWrite(sensorPin1,sensorPin1State=HIGH);  // stops at the centre position
    digitalWrite(In4,LOW);
    digitalWrite(In3,LOW);
    
    }


if((digitalRead(sensorPin2)==LOW))

    {
    digitalWrite(sensorPin2,sensorPin2State=HIGH);  // stops at the upper position
    digitalWrite(In4,LOW);
    digitalWrite(In3,LOW);
    
    }

if((digitalRead(sensorPin3)==LOW))

    {
    digitalWrite(sensorPin3,sensorPin3State=HIGH);   // stops at the lower position
    digitalWrite(In4,LOW);
    digitalWrite(In3,LOW);
    
    }
else 
    {
    digitalWrite(In4,HIGH);
    digitalWrite(In3,LOW);
    }

But the code when tested again does not work, only worked the first time.All the code did is that the motor runs continuously when the sensor does not senses anything . When the position comes in middle and gets sensed by the sensor, the motor stops(which did not happen earlier,which is a good thing) but it stops for infinite period ,the delay given does not matter it continues to stop.Now all I have to do is that get the motor moving in this stopped condition and go to the upper or lower position as the motor moves. Stop there again for the prescribed delay and then again get moving . This cycle should continuously go on. The sequence of the sensors in the code shouls not matter ,the motor should stop when the mechanism gets by any of the three sensors. If possible please help with the relevant code, could not decipher it though trying all the options within limit !

That is not a complete sketch. Please post the complete sketch.

Based on your inexperience, it would really recommend you follow @Paul's advice and get the simple example code working first. It appears you may be biting off more than you can chew.

What do you expect this code to be doing?

  if ((digitalRead(sensorPin2) == LOW))
  {
    digitalWrite(sensorPin2, sensorPin2State = HIGH); // stops at the upper position
    //                                 ^^^^^^^^^^^^^^^

As written, you are assigning to sensorPin2State and then making that pin HIGH, but I'm guessing that is by accident. A much more typical way would be

  if ((digitalRead(sensorPin2) == LOW))
  {
    sensorPin2State = HIGH;
    digitalWrite(sensorPin2, sensorPin2State); // stops at the upper position
    //...