Can`t stop car automatically

Your program needs state - as it is when the condition for stopping happens it will stop only as long as that condition holds, whereas I think you want it to stay stopped.

Perhaps a variable:

bool stopped = false ;


void loop ()
{
  int msensor =analogRead(sensor);                                            
  if(msensor>90 || msensor<200){
    Serial.println("car want self-stop automatically "); 
    stopped = true ;
    go_Right();
    stopCar();
  }
  if (!stopped)
    auto_avoidance();
}

Thus auto_avoidance should take note of whether your in the stop state.

1 Like