How to use sensing without a delay

Hello, I am new to Arduino. I have an Arduino Uno R3. I wrote a code to control a robot that has 4 wheels (2 wheels/motors on each side) and 3 sensors (left, forward, and right). When the robot sensor sees something in the way, the robot backs up and turns a different direction.

When the motor goes into backup mode, the sensors stop sensing because of the delays. I am trying to figure out how to keep sensing while the motor is doing its backup mode. Then I could add a sensor in the back so the robot does not run into something while backing up.

I tried searching on the forums and found a thread about changing from delay() to millis(), but I could not make that work. Is there a better way? Here is the loop part of the code I am using.

void loop()                                  // Main loop auto-repeats
{     
    int irLeft = irDetect(11, 13, 38000);    // Check for object on left
    int irEdge = irDetect(10, 12, 38000);  // Check for an edge
    int irRight = irDetect(9, 8, 38000);     // Check for object on right
  
    Serial.print("  leftEye = ");
    Serial.print(irLeft); 
    Serial.print("-irEdge = "); 
    Serial.print(irEdge); 
    Serial.print("-rightEye = "); 
    Serial.print(irRight); 
    Serial.println(" "); 
    delay(100);   

  if ((irLeft == 1) && (irEdge == 1) && (irRight == 1))
      // Robot Forwards mode
    {
        motorA(1, 100);
        motorB(1, 100);
    }
  
  else      // if ((irLeft == 0) || (irEdge == 0) || (irRight == 0))
     // Robot Backup mode 
   {
    
      // stop the robot 
      motorA(0, 100);
      motorB(0, 100);
      delay(1000);
    
      // go backwards
      motorA(2, 100);
      motorB(2, 100);
      delay(500);
    
      // turn right
      motorA(0, 100);
      motorB(2, 100);
      delay(1000); 
    }
}

Is there a better way?

The blink without delay example and a state machine.

The robot is in some state, always - going forward, stopped, backing up, turning left, etc.

There are events that dictate a change in state - something appeared in front while going forward, been backing up long enough, something appeared behind while backing up, etc.

The state you are in will dictate what events to check for, and what state to transition to if the event occurs. There may, or may not, be things to do when the transition is to occur - put on the brakes, shift into reverse and hit the gas, shift into forward and easy on the gas, record the time, etc.

Draw a diagram with the states as circles. Along the lines connecting the circles, list the actions that need to happen below the line and the events that trigger the change above the line.

Once you have a pretty picture, the code part is easy.

The demo several things at a time is an extended example of the BWoD technique for managing time using millis() rather than delay(). You may also find planning and implementing a program useful.

...R