elegoo smart robot car v3.0 modes setting

Hi, I have a Elegoo smart robot car V3.0. I have made obstacle avoidance and speed control . Now i want to do more coding on it and make it 1 single code. So i need a detailed and clear explanation of what it is so i can get a better idea of it.

Yes, I think you’re right.

Let's see. You're the one with the car and so you know what it's doing. You're the one who wants to do some coding and has the code to look at. So I guess you're the only one who can possibly explain "it", whatever "it" is.

Steve

Actually i have coded both as 2 but when i try to run it when different commands hit, it works except obstacle avoidance.

#include <IRremote.h>
#include <Servo.h>
int servoPin = 3;
int servoPos = 130;
Servo myServo;

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11

float t;
float distanceV = 1000;
float speedV = 100;

int trig = A5;
int echo = A4;
int pingtime;
int pingTime;
int pingTime2;
int pingTime3;
int pingread;
int pingRead;
int pingRead2;
int pingRead3;
int j;





int irSensor = 12;
IRrecv  myIR(irSensor);
decode_results cmd;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  myIR.enableIRIn();
  myIR.blink13(true);
  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  myServo.attach(servoPin);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

}


void servoMeasure(){
  for(j=95;j<=180;j=j+1){
    myServo.write(j);
    delay(9);
  }
  digitalWrite(trig,LOW);
  delayMicroseconds(10);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  pingTime=pulseIn(echo,HIGH);
  delay(25);
  pingRead=pingTime/15;
  for(j=180;j>=95;j=j-1){
    myServo.write(j);
    delay(9);
  }
  digitalWrite(trig,LOW);
  delayMicroseconds(10);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  pingTime2=pulseIn(echo,HIGH);
  delay(25);
  pingRead2=pingTime2/15;
  for(j=95;j>=1;j=j-1){
    myServo.write(j);
    delay(9);
  }
  digitalWrite(trig,LOW);
  delayMicroseconds(10);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  pingTime3=pulseIn(echo,HIGH);
  delay(25);
  pingRead3=pingTime3/15;
  for(j=1;j<=95;j=j+1){
    myServo.write(j);
    delay(9);
  }
  
  
}


void forward(float speedV){
  analogWrite(ENA,speedV);
  analogWrite(ENB,speedV);
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,HIGH);
  
 
  
}


void stopped(){
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}


void backward(float speedV){
  analogWrite(ENA,speedV);
  analogWrite(ENB,speedV);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  
  
  
}

void left(float speedV){
  analogWrite(ENA,speedV);
  analogWrite(ENB,speedV);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);

 
  
}

void right(float speedV){
  analogWrite(ENA,speedV);
  analogWrite(ENB,speedV);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
 
  
  
}


void distance(){
  digitalWrite(trig,LOW);
  delayMicroseconds(10);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  pingtime=pulseIn(echo,HIGH);
  delayMicroseconds(5);
  pingread=pingtime/15;
  
}



void setspeed(){
  Serial.println("Set Speed");
  delay(500);
  while(myIR.decode(&cmd) == 0){
      
  }
 
  if(cmd.value == 0xFF6897){
    Serial.println("speed=1");
    speedV=50;
  }
  if(cmd.value == 0xFF9867){
    Serial.println("speed=2");
    speedV=75;
  }
  if(cmd.value == 0xFFB04F){
    Serial.println("speed=3");
    speedV=100;
  }
  if(cmd.value == 0xFF30CF){
    Serial.println("speed=4");
    speedV=150;
  }
  if(cmd.value == 0xFF18E7){
    Serial.println("speed=5");
    speedV=200;
  }
  if(cmd.value == 0xFF7A85){
    Serial.println("speed=6");
    speedV=255;
  }
  
}


void setdistance(){
  Serial.println("Set distance mode");
  delay(500);
  myIR.resume();
  while(myIR.decode(&cmd) == 0){
      
  }
  
  if(cmd.value == 0xFF6897){
    Serial.println("Distance=1");
    distanceV=250;
  }
  if(cmd.value == 0xFF9867){
    Serial.println("Distance=2");
    distanceV=500;
  }
  if(cmd.value == 0xFFB04F){
    Serial.println("Distance=3");
    distanceV=1000;
  }
  if(cmd.value == 0xFF30CF){
    Serial.println("Distance=4");
    distanceV=1500;
  }
  if(cmd.value == 0xFF18E7){
    Serial.println("Distance=5");
    distanceV=2000;
  }
  if(cmd.value == 0xFF7A85){
    Serial.println("Distance=6");
    distanceV=3000;
  }
 
}



void distance2(){
  forward(255);
  distance();
  Serial.println(pingread);
  
}

void obstacle(){
  distance();
  Serial.println(pingread);
  if(pingread > 50){
    distance2();
    
  }

  if(pingread < 40){
    stopped();
    servoMeasure();
  }

  if(pingRead < pingRead3){
    right(255);
    distance();
    if(pingread > 40){
      distance2();
    }
    
    
  }
  

  if(pingRead > pingRead3){
    left(255);
    distance();
    if(pingread > 40){
      distance2();
    }
    
    
  }
}


void drive(){
  if (cmd.value == 0xFF629D){
    forward(speedV);
    delay(distanceV);
    stopped();
    
  }



  if (cmd.value == 0xFFA857){
    backward(speedV);
    delay(distanceV);
    stopped();
    
  }

  if (cmd.value == 0xFFC23D){
    right(speedV);
    delay(distanceV);
    stopped();
    
  }

  if (cmd.value == 0xFF22DD){
    left(speedV);
    delay(distanceV);
    stopped();
    
  }

  if (cmd.value == 0xFF02FD){
    stopped();
    delay(250);
    
  }



  if (cmd.value == 0xFF52AD){
    setspeed();
    
  }

  
  
  if (cmd.value == 0xFF42BD){
    setdistance();
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  
  Serial.println(cmd.value);
  distance();
  delay(100);
 

  Serial.println(cmd.value,HEX);

  while(myIR.decode(&cmd) == 0){
    
  }
  delay(100);

  myIR.resume();
  if (cmd.value == 0xFF629D){
    forward(speedV);
    delay(distanceV);
    stopped();
    
  }



  if (cmd.value == 0xFFA857){
    backward(speedV);
    delay(distanceV);
    stopped();
    
  }

  if (cmd.value == 0xFFC23D){
    right(speedV);
    delay(distanceV);
    stopped();
    
  }

  if (cmd.value == 0xFF22DD){
    left(speedV);
    delay(distanceV);
    stopped();
    
  }

  if (cmd.value == 0xFF02FD){
    stopped();
    delay(250);
    
  }



  if (cmd.value == 0xFF52AD){
    setspeed();
    
  }

  
  
  if (cmd.value == 0xFF42BD){
    setdistance();
  }


  if (cmd.value == 0xFF10EF){
    distance();

    if(pingread > 50){
      distance2();
    
    }

    if(pingread < 40){
      stopped();
      servoMeasure();
    }

    if(pingRead < pingRead3){
      right(255);
      distance();
      if(pingread > 40){
        distance2();
      }
    
    
    }
  

    if(pingRead > pingRead3){
      left(255);
      distance();
      if(pingread > 40){
        distance2();
      }
    
    
    }
  }
 

  
 

  
  
    

}