Need help in coding?

I am having a project in which my line following robot follows a circle and I have single ir sensor at the starting point of the line which can detect the passing robot, at first if I switch on my line following robot, while my robot starts to move ir sensor detects the robot which is passing the starting point, and if ir sensor detects I am switching on the relay for turning on the table fan for 10 seconds and it goes off, robot comes back to the starting point for the second lap and the ir sensor placed at the starting point detects again, I will have to switch other relay for turning on light for 10 seconds and go off. That's it only two laps. again if I start the line following robot it has to start over again, first fan then light. I am not sure how to code it, could you please guide me through this . for one lap, robot might take around some 45 to 48 seconds(approx). I am not getting the required output with this code, it gives me a random behaviour

If I haven't provided enough details please ask

int sensor2 = 2;
int motor1 = 3;
int motor2 = 4;
int motor3 = 5;
int motor4 = 6;

void setup()
{
  pinMode(sensor2, INPUT);
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
  pinMode(motor4, OUTPUT);
}

void loop()
{
  mainfn();
}

void mainfn()
{
  
  int a  = digitalRead(sensor2);
  if( a == 1)
  {
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, HIGH);
    delay(10000);
    digitalWrite(motor1,LOW);
    digitalWrite(motor2, LOW);
    while(digitalRead(sensor2) == LOW)
    {
      int b = digitalRead(sensor2);
      if (b ==1)
      {
        digitalWrite(motor3, HIGH);
        digitalWrite(motor4, HIGH);
        delay(10000);
        digitalWrite(motor3, LOW);
        digitalWrite(motor4, LOW);
        mainfn();
      }
    }
  }
}

Thanks

digitalWrite(motor4, LOW);
        mainfn();

Recursion is a Bad Ideatm on a very small microcontroller.

fine, I am not sure how to accomplish the required task, can you please help

Well, when you've got rid of the recursion, start at looking getting rid of the delay () calls.

You don't drive a car or even walk down the street by opening your eyes, then setting off walking with your eyes shut.

Try googling "state machine".

Your program is structured in an odd way. Why have the mainfn() function ?

The nastiest thing about what you are doing is to call mainfn() from within mainfn(). This is known as recursion and unless you know what you are doing can cause problems as memory can rapidly be used up and not released.

The second problem area is the use of the delay() function. It may not matter in this instance that during the 10 second delay nothing else can happen but if you were you using the same Arduino to control the line following robot it would be a problem. During that 10 seconds it could not sense the line and correct the direction so it would be a good habit to get into to use millis() for timing instead of delay().

You have given some pins names but not made them meaningful. I assume you know what motor1, motor2 etc are but we don't. Which pins are the relays connected to, for example ?

Recursion is a Bad Ideatm on a very small microcontroller.

Actually it’s a bad idea on any architecture if there’s no exit clause to unwind the recursion.

Your program is structured in an odd way. Why have the mainfn() function ?

The nastiest thing about what you are doing is to call mainfn() from within mainfn(). This is known as recursion and unless you know what you are doing can cause problems as memory can rapidly be used up and not released.

The second problem area is the use of the delay() function. It may not matter in this instance that during the 10 second delay nothing else can happen but if you were you using the same Arduino to control the line following robot it would be a problem. During that 10 seconds it could not sense the line and correct the direction so it would be a good habit to get into to use millis() for timing instead of delay().

You have given some pins names but not made them meaningful. I assume you know what motor1, motor2 etc are but we don’t. Which pins are the relays connected to, for example ?

I used mainfn within itself because, first time I tried without calling the mainfn, what happened was " at first ir sensor detected and “motor1//fanrelay” switched on for 10 seconds and went off and again when ir sensor detected the presence “motor2//lightrelay” switched on, fine, again if ir sensor detected it doesn’t goes back to fanrelay and after this every time the ir sensor detected it switched only the light relay every time, so I tried calling the mainfn again, which didn’t work.

Coming to your second question, my line following robot is independent of this code, I have prepared a separate line following robot without microcontroller.

Third question, Motor1 is fanrelay1, motor2 is fanrelay2, motor3 is lightrelay1 and motor4 is lightrelay2.

sorry last post i made a mistake, there are two fans and two lights in total,

Well, when you’ve got rid of the recursion, start at looking getting rid of the delay () calls.

You don’t drive a car or even walk down the street by opening your eyes, then setting off walking with your eyes shut.

Try googling “state machine”.

can you please provide me pseudo code, let me try it please

Is Google broken?

If you want something to happen when something is detected isn't it as simple as this pseudo code

if (somethingIsDetected == true) {
    doSomething();
}

If that doesn't meet the requirement then you need to describe what you want more clearly.

...R