Elegoo smart car Obstacle avoidance code question / feedback

New to coding here and looking for advise and feedback. I have modified the base code from Elegoo for Obstacle avoidance by adding a “check if stuck – move back and right” block to the code. Its working OK but not perfect. The delay between forward distance measurements seems to be the issue (see code below). If the delay is longer than 500ms the vehicle will sometimes hit a wall dead on. If the delay is too short the vehicle will detect a false stuck situation and do its evasive maneuver. I also increased the detect distance to 40cm from the original 20cm to compensate. I’m thinking there must be a more efficient (elegant) way of doing this with code. Any input would be greatly appreciated.

Thanks

CC

void loop() { 
    myservo.write(90);  //setservo position according to scaled value
    delay(500); 
   forward();

    checkifstuckDistance = Distance_test();
 
    delay (300);   //delay between forward movement Distance tests          
    
    middleDistance = Distance_test();

//checking if stuck. will compare two forward Distance_test(); at an interval 
//do evasive move if within +/- 1cm of both measurment

    if(checkifstuckDistance <= middleDistance + 1 && checkifstuckDistance >= middleDistance - 1 ) {

      stop();
      delay (500);
      back();
      delay(180);
      right();
      delay(90);
}
      else {
    

    if(middleDistance <= 40) {     
      stop();
      delay(500);                         
      myservo.write(23);          
      delay(500);      
      rightDistance = Distance_test();
      
      delay(500);
      myservo.write(90);              
      delay(500);                                                  
      myservo.write(165);              
      delay(500); 
      leftDistance = Distance_test();
      
      delay(500);
      myservo.write(90);              
      delay(500);
      if(rightDistance > leftDistance) {
        right();
        delay(260);
      }
      else if(rightDistance < leftDistance) {
        left();
        delay(260);
      }
      else if((rightDistance <= 40) || (leftDistance <= 40)) {
        back();
        delay(180);
      }
      else {
        forward();
      }
    }  
    else {
        forward();
    }  
    }                   
}

You jump on an airplane, and fly to a strange city. You get off the plane, and need to find baggage claim. Do you walk around with your eyes closed most of the time, only opening them periodically to see if there is anything in front of you?

Why are you making your robot do that? GET RID OF EVERY DELAY IN YOUR CODE! Keep your robots eyes open ALL THE TIME.

Just like you walk forward, looking for signs to baggage claim, only changing direction when there are obstacles (or signs) in the direction that you are traveling, your robot MUST do the same thing. When you tell it to go forward, you must assume that it can travel indefinitely in that direction UNTIL YOU SPOT AN OBSTACLE.

Thanks for the comment and the challenge PaulS. What you are saying is what good looks like for me. But as a Newbie to this world, the path is far from being clear.

cubiccarbon:
But as a Newbie to this world, the path is far from being clear.

Who gets it right the first time? Much of the entertainment of this hobby comes from learning as you go.
Good that you recognize the good advice being given.
Don't fret over the challenge. We can help get you there.

The loop() function is called over and over again, possibly 1000's of times per second. Each time through loop(), get the distance in front of the robot. Make a decision about what to do based on that distance.

Telling the robot to go forward when it is already going forward is a waste of effort, but it won't hurt anything. You could determine whether that was necessary, or not, before telling it to go forward. You need to keep track of what the robot is currently doing. Google "state machine Arduino". Your robot is movingForward, movingBackwards, turningLeft, turningRight, stopping, stopped, doingTheHokeyPokey, etc. In any given state, there are things it can do, and things it can't. For instance, if it is doing the hokey pokey, it can't turn left. If it is going forward, it can turn left.

There are reasons why it should change from one state to another. For instance, going forward when the distance in front is 3 cm is not a good idea. Turning left when there is an object 2 cm away on the left isn't a good idea. Going forward when the distance in front is > 100 cm is a good idea.

So, on each pass through loop(), determine whether it is necessary to change state, based on sensor input and the current state.

When changing from one state to another, there are things that have to happen. Changing from going forward to going backwards requires stopping first. Changing from turning right to doing the hokey pokey requires liberal applications of alcohol.

You need to make those things that need to happen happen, in the proper order.

The hard part, usually, is knowing what needs to happen to effect each transition, and what order those things need to happen in.