Switching between 2 loops and disabling the other

I am making an Rc car that can be driven manually or be driven autonomously. (RC and AUTO), I have been able to get the RC portion to work perfectly but when I introduced the ability to switch between the two is when issues started. when AUTO is enabled I am able to move the robot but only while turning left (this isnt a big issue but it might be worth noting) the main issue is that the RC loop/code is interfeering with the AUTO loop/code it does this by cuasing stutters in the wheels, the only way i can describe it is that the wheels turn for 999ms then for 1ms they stop cuasing a jerking motion. Im using an L298N, arduino mega, 4 12v motors and so far 1 ultrasonic hc-sr04 sensor. I am pretty confident the RC code is cuasing the issue so i need a way to temporaril disable it for the auto loop, Thanks.

const int rc = A1;
const int rc2 = A0;
const int Forward1 = 10;
const int Backward2 = 9;
const int Forward3 = 8;
const int Backward4 = 7;
int duration;
int duration2;

  double fnc_ultrasonic_distance(int _t, int _e){
  unsigned long dur=0;
  digitalWrite(_t, LOW);
  delayMicroseconds(5);
  digitalWrite(_t, HIGH);
  delayMicroseconds(10);
  digitalWrite(_t, LOW);
  dur = pulseIn(_e, HIGH, 18000);
  if(dur==0)return 999.0;
  return (dur/57);
}




void RC() {
  duration = constrain(pulseIn(rc, HIGH), 1000, 2000);
  duration2 = constrain(pulseIn(rc2, HIGH), 1000, 2000);

  if (duration <= 1600 && duration >= 1400) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, 0);
    
    
  }
    if (duration > 1600) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2, map(duration,1600, 2000, 180, 255));
    analogWrite(Forward3, 0);
    analogWrite(Backward4, map(duration,1600, 2000, 180, 255));
    
    
  }

 if (duration2 > 1600) {
    analogWrite(Forward1, map(duration2,1600, 2000, 180, 255));
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, map(duration2,1600, 2000, 180, 255));
    
     
  }


    if (duration < 1400) {
    analogWrite(Forward1, map(duration,1400, 1000, 180 , 255));
    analogWrite(Backward2,0);
    analogWrite(Forward3, map(duration,1400, 1000, 180 , 255));
    analogWrite(Backward4,0);
    
}
  if (duration2 < 1400) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2,map(duration2,1400, 1000, 180 , 255));
    analogWrite(Forward3, map(duration2,1400, 1000, 180  , 255));
    analogWrite(Backward4,0);
    
     
} 

}

void AUTO() {

  {
    if ((fnc_ultrasonic_distance(24,22) <= 20)) 
    {
      analogWrite(Forward1, 100);
      analogWrite(Backward2,0);
      analogWrite(Forward3, 100);
      analogWrite(Backward4,0);
    } 
    else 
    {
      analogWrite(Forward1, 0);
      analogWrite(Backward2,100);
      analogWrite(Forward3, 0);
      analogWrite(Backward4,100);
    }


  }
}


void setup() {
  pinMode(52, INPUT_PULLUP);
  pinMode(rc, INPUT);
  pinMode(rc2, INPUT);
  pinMode(Forward1, OUTPUT);
  pinMode(Backward2, OUTPUT);
  digitalWrite(Forward1, LOW);
  digitalWrite(Backward2, LOW);  
  pinMode(Forward3, OUTPUT);
  pinMode(Backward4, OUTPUT);
  digitalWrite(Forward3, LOW);
  digitalWrite(Backward4, LOW);
  pinMode(24, OUTPUT);
  pinMode(22, INPUT);
  
}



void loop() {


 if (digitalRead(52) == HIGH) {

    AUTO();

}

 if (digitalRead(52) == LOW) {
  RC();
  

  

}

  }


  

When will this condition ever be satisfied?

Other conditions will execute in sequence, overriding the output of a previous condition.

The use of pulseIn() will block everything until completion or timeout.

I believe that condition is true when the control stick is in the middle which would tell the robot not to move. If I take it out would the robot still not move when no sticks are moved?

You better learn the basics and know what your code does instead of wrong guesses.

I do not understand how my code works, isn’t the point of an online forum to learn from these mistakes? I simply want to know what that pulsein statement does if I take it out.

It's your task to read documentation or other RC car projects, understand and learn. The forum will help on specific questions but won't do all the work for you.

So what's your question?

My question is, if the statement - if (duration <= 1600 && duration >= 1400) { is removed, won’t the robot not be able to stop for it does not have a piece of code telling it to do so?

As the condition can never be true the statement does not have any impact on your project. So yes, you can remove that entire statement.

I have also looked at many rc car projects but I was unable to find any that use auto and rc control along with the pulse in function.

Thank you for your help, I will try to understand my code a bit better next time, programming isn’t one of my strongest skills.

The motors continue running until you change the Forward or Backward outputs. So far your problem is not pulseIn() but the confuse interpretation of the control signals.

You can start by computing the new values for the motor outputs first, in the RC() and auto() functions, and only output the new values in loop(). If you track the previous values you can update the outputs only if the values have changed. This will remove any motor stutter from your project.

It's written poorly, but duration==1500 would be true for both cases.

I would code it like this to make it more readable.
if ( duration >= 1400 && duration <= 1600 ){

Sorry, I misread 1600 as 1000 :frowning:

I have begun making a few changes, I tried changing the ultrasonic sensor code thinking the delay was messing with the wheels but I was wrong. I know that when I do not run the RC loop everything works fine. Would there be a way to disable the RC loop while the AUTO loop is ON? I have read about break; but that wouldnt work as im not using for loops, I also tried to use exit(0); but that disables the board so that wouldnt work. Is there another alternative?

const int rc = A1;
const int rc2 = A0;
const int Forward1 = 10;
const int Backward2 = 9;
const int Forward3 = 8;
const int Backward4 = 7;
int duration;
int duration2;

  double fnc_ultrasonic_distance(int _t, int _e){
  unsigned long dur=0;
  digitalWrite(_t, LOW);
  delayMicroseconds(5);
  digitalWrite(_t, HIGH);
  delayMicroseconds(10);
  digitalWrite(_t, LOW);
  dur = pulseIn(_e, HIGH, 18000);
  if(dur==0)return 999.0;
  return (dur/57);
}




void RC() {
  duration = constrain(pulseIn(rc, HIGH), 1000, 2000);
  duration2 = constrain(pulseIn(rc2, HIGH), 1000, 2000);

  if ( duration >= 1400 && duration <= 1600 ){
    analogWrite(Forward1, 0);
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, 0);
    
    
  }

  if ( duration2 >= 1400 && duration2 <= 1600 ){
    analogWrite(Forward1, 0);
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, 0);
    
    
  }

  
    if (duration > 1600) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2, map(duration,1600, 2000, 180, 255));
    analogWrite(Forward3, 0);
    analogWrite(Backward4, map(duration,1600, 2000, 180, 255));
    
    
  }

 if (duration2 > 1600) {
    analogWrite(Forward1, map(duration2,1600, 2000, 180, 255));
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, map(duration2,1600, 2000, 180, 255));
    
     
  }


    if (duration < 1400) {
    analogWrite(Forward1, map(duration,1400, 1000, 180 , 255));
    analogWrite(Backward2,0);
    analogWrite(Forward3, map(duration,1400, 1000, 180 , 255));
    analogWrite(Backward4,0);
    
}
  if (duration2 < 1400) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2,map(duration2,1400, 1000, 180 , 255));
    analogWrite(Forward3, map(duration2,1400, 1000, 180  , 255));
    analogWrite(Backward4,0);
    
     
} 

}

void AUTO() {

     {
    if ((fnc_ultrasonic_distance(24,22) <= 20)) 
    {
      analogWrite(Forward1, 100);
      analogWrite(Backward2,0);
      analogWrite(Forward3, 100);
      analogWrite(Backward4,0);
    } 
    else 
    {
      analogWrite(Forward1, 0);
      analogWrite(Backward2,100);
      analogWrite(Forward3, 0);
      analogWrite(Backward4,100);
    }


  }
 
}


void setup() {
  pinMode(52, INPUT_PULLUP);
  pinMode(rc, INPUT);
  pinMode(rc2, INPUT);
  pinMode(Forward1, OUTPUT);
  pinMode(Backward2, OUTPUT);
  digitalWrite(Forward1, LOW);
  digitalWrite(Backward2, LOW);  
  pinMode(Forward3, OUTPUT);
  pinMode(Backward4, OUTPUT);
  digitalWrite(Forward3, LOW);
  digitalWrite(Backward4, LOW);
  pinMode(24, OUTPUT);
  pinMode(22, INPUT);
  
}



void loop() {


 if (digitalRead(52) == HIGH) {

    AUTO();

}

 if (digitalRead(52) == LOW) {
  RC();
  

  

}

  }



  

   

  

Please use CTRL-T in the IDE to format the code, then repost it.
And please stop calling them loops. They are functions. Your sketch may contain only one loop function.
Finally, COMMENTS. Code without comments is almost unreadable. I have ABSOLUTELY no clue what the function "fnc_ultrasonic_distance()" does.

While I am nitpicking on style, using a conventional coding style also makes it easier for experienced users to read your code.

Only constants should be upper case, like HIGH and LOW.
Variables and function names should be in camelCase. For example, "Forward" should be "forward".

As the name of a pin I'd prefer Forward, leaving forward for some action or value variable and FORWARD for a direction constant..

What's "disable"? A function is disabled if it is not called, as demonstrated in your loop() code.

You also can move the test to the begin of each function like

void RC() {
  if (digitalRead(52) == HIGH) return; //not exit()
  ...
}

For I/O pins, I prefer to use
const int XYZ_PIN 5;
because XYZ_PIN is a constant.

Here is my revised code, using (return;) did not work and did not allow me to switch between modes. I am still experiencing jittering when the sensor does not see anything, when it finds something within 20cm and it is backing up the motion is smooth, what can i do to supress this?


const int rc = A1;
const int rc2 = A0;
const int Forward1 = 10;
const int Backward2 = 9;
const int Forward3 = 8;
const int Backward4 = 7;
int duration;
int duration2;

double fnc_ultrasonic_distance(int _t, int _e) { //Ultrasonic sensor code used to space out the timing for ping/echo
  unsigned long dur = 0;
  digitalWrite(_t, LOW);
  delayMicroseconds(5);
  digitalWrite(_t, HIGH);
  delayMicroseconds(10);
  digitalWrite(_t, LOW);
  dur = pulseIn(_e, HIGH, 18000);
  if (dur == 0)return 999.0;
  return (dur / 57);
}




void RC() {
  duration = constrain(pulseIn(rc, HIGH), 1000, 2000); //L298N A output, pulseIn is used to encode the values coming from the RC reciever from 1000-2000
  duration2 = constrain(pulseIn(rc2, HIGH), 1000, 2000); //L298N B output

  if ( duration >= 1400 && duration <= 1600 ) { //No (x) stick moved
    analogWrite(Forward1, 0);
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, 0);


  }

  if ( duration2 >= 1400 && duration2 <= 1600 ) { //No (y) stick moved
    analogWrite(Forward1, 0);
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, 0);


  }


  if (duration > 1600) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2, map(duration, 1600, 2000, 180, 255)); //robot moves back
    analogWrite(Forward3, 0);
    analogWrite(Backward4, map(duration, 1600, 2000, 180, 255));


  }

  if (duration2 > 1600) {
    analogWrite(Forward1, map(duration2, 1600, 2000, 180, 255)); //Robot turns
    analogWrite(Backward2, 0);
    analogWrite(Forward3, 0);
    analogWrite(Backward4, map(duration2, 1600, 2000, 180, 255));


  }


  if (duration < 1400) {
    analogWrite(Forward1, map(duration, 1400, 1000, 180 , 255)); //Robot moves forward
    analogWrite(Backward2, 0);
    analogWrite(Forward3, map(duration, 1400, 1000, 180 , 255));
    analogWrite(Backward4, 0);

  }
  if (duration2 < 1400) {
    analogWrite(Forward1, 0);
    analogWrite(Backward2, map(duration2, 1400, 1000, 180 , 255));  //Robot turns
    analogWrite(Forward3, map(duration2, 1400, 1000, 180  , 255));
    analogWrite(Backward4, 0);


  }

}

void AUTO() {

  {
    if ((fnc_ultrasonic_distance(24, 22) <= 20)) //If sensor finds object less than or equal 20cm away
    {
      analogWrite(Forward1, 0);
      analogWrite(Backward2, 100); //It will back away
      analogWrite(Forward3, 0);
      analogWrite(Backward4, 100);
    }
    else
    {
      analogWrite(Forward1, 100);
      analogWrite(Backward2, 0); //else, it will continue moving forward
      analogWrite(Forward3, 100);
      analogWrite(Backward4, 0);
    }


  }

}


void setup() {
  pinMode(52, INPUT_PULLUP); //pin for switching between AUTO and RC modes (HIGH/LOW)
  pinMode(rc, INPUT);
  pinMode(rc2, INPUT);
  pinMode(Forward1, OUTPUT);
  pinMode(Backward2, OUTPUT);
  digitalWrite(Forward1, LOW);
  digitalWrite(Backward2, LOW);
  pinMode(Forward3, OUTPUT);
  pinMode(Backward4, OUTPUT);
  digitalWrite(Forward3, LOW);
  digitalWrite(Backward4, LOW);
  pinMode(24, OUTPUT); //TRIG
  pinMode(22, INPUT); //ECHO

}



void loop() {


   if (digitalRead(52) == HIGH) { // if reciever pin is high switch to AUTO

    AUTO();

  }

  if (digitalRead(52) == LOW)  { // if reciever pin is low switch to RC

    
    RC();




  }

}

Still duration and duration2 have different effects at the same time.