How do I continue a function after and if-statement becomes false

I am currently working on an autonomous robot for my capstone project, I am using obstacle avoidance IR sensors to detect the edge of the table so the robot doesn’t fall off. However, I want the robot to stop, turn 90 degrees, move forwards a little bit, and then turn left another 90 degrees.I already have the timing down so the motors activate long enough to sufficiently turn the robot. The problem I am having is that as soon as the if statement becomes false again, the robot keeps moving forward. I only want my if-statement to trigger functions, instead of running functions while the statement is true. Here is my code.

//LET'S BUILD A COOL ROBOT!

//Right Motor

int in1 = 2;
int in2 = 3;

//Left Motor

int in3 = 4;
int in4 = 5;

//IR Sensor Front
int iRF = 8;

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(iRF, INPUT);
}

void moveForward()    // Move Forward Function for Motor Driver.
{
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
}

void moveBackward()
{
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
}

void moveLeft()
{
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
}

void moveRight()
{
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
}

void brake()
{
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
}

void loop() {
  moveForward();
  
  if(digitalRead(iRF)) // 
  {
    for (int x = 0; x <= 7; x++) {
      delay(200);          // wait for sensors to stabilize
      moveBackward();
      x++;
      }
  }
      else if ((!digitalRead(iRF))) {
        for (int i = 0; i <= 7; i++) {
        delay(200);          // wait for sensors to stabilize
        moveLeft();
        i++;
      }
  }
}

I will also have to do something similar to activate a claw mechanism. I want to call upon a function that closes the claw once a differnt IR sensor detects an object in front of it. The problem is that it reopens and let’s it go as soon as the IR sensor no longer detects the object.

Why the strange double increments of the for loop control variables?

I only want to activate the turn function twice, so I thought I could nest a for-loop. However it didn't work and the motors keep activating and delaying infinitely.

Well, you didn't post your code, so it is hard to give advice.

We cannot assess code based on snippets - your issue also depends on the code surrounding that if statement.

Are you aware that when you do:

for (int x = 0; x <= 7; x++) {
      delay(200);          // wait for sensors to stabilize
      moveLeft();
      x++;
      }

x increases by 2 for each iteration of the loop?

You’ve specified x++ in the for loop - that happens automatically.

DrAzzy:
We cannot assess code based on snippets - your issue also depends on the code surrounding that if statement.

Are you aware that when you do:

for (int x = 0; x <= 7; x++) {

delay(200);          // wait for sensors to stabilize
      moveLeft();
      x++;
      }




x increases by 2 for each iteration of the loop? 

You've specified x++ in the for loop - that happens automatically.

Yeah that nested for-loop was a pretty stupid idea. I just need to edit the code to trigger a function once the initial condition becomes true for a moment.

AWOL:
Well, you didn't post your code, so it is hard to give advice.

I updated the code to include all of it. Unfortunately the else-if statement takes over the move forward command.

The code in your original post doesn't now look like the code you posted originally.

AWOL:
The code in your original post doesn't now look like the code you posted originally.

Yeah those actual code doesn't really matter at this point. I am more interested in what control structure I would need to use to call upon a function after triggering an initial condition.

I just want to call upon a function if an if-statement became true initially. I can code everything up myself after that. Does that make sense?

 if(digitalRead(iRF)) // 

{
   //stuff
 }
     else if ((!digitalRead(iRF))) {

It is possible for neither side to be executed. The value read might change in between the two readings.

This is different to this other common anti-pattern:

if(a==2) {
  } else if(a!=2) {

If a is not 2 the first time you test it then it is still not 2 the second time.

if(a==2) {
  } else { //a is not 2

But you need to totally restructure your program. It should look something like this ...

void loop() {
  readSensors();
  makeDecisions();
  doSerialOutput();
  moveMotors();
}

It is tough to diagnose from afar with no psuedo code or flow diagram, but the 4 item loop structure above will be quite helpful. You likely will also need to introduce some state variables that can let you know what's going on each time you cycle in main loop. For example, a variable to tell you if the claw is moving or not. And if so then which direction, open or closed. Your claw may also need some type of positional sensors to let you know when it becomes fully closed or open so you can shut off the motors. Or you could use a simpler timer variable and some testing to determine open/close speeds.

As to your original post of wanting the robot to do "X" when it detects an edge.... you may want to think of your autonomous system as having a series of goals and subgoals. When the edge is detected a subgoal called Avoid is introduced and executed like a function call away from the main loop with its own timings between commands. When Avoid concludes you return to the main loop but you are now away from danger.

Good luck with it.