Activating Line Tracking Function Using IR

So I am trying to activate the Line Tracking function using IR. The function works and so does the line tracking code. And when I put the function into the loop, it works fine (the raw code and the function work). But as soon as anything else is in there with the line tracking code or function, it doesn’t work as expected. I have tried to problem solve by adding parts of my code to the working Line tracking and it all works until I put something else into the loop. So this is clearly a coding issue but it exceeds my understanding of Arduino.
So what happens when I press 4 on the remote is that the car just starts moving to the left endlessly.
My guess is that it has something to do with all the IF and ELSE statements, but that is just my 2 cents.

EDIT
So after testing the new code. I’m still having the same problem of getting the code to function as needed. This is the newerish code:
Also while making this edit I found out that the max length is 9000 characters, so I will be getting rid of the older code.
And is there a way to “Bump up” questions?

#include <IRremote.h>

const int receiverpin = 12;
const int in1 = 6;
const int in2 = 7;
const int in3 = 8;
const int in4 = 9;
const int ENA = 5;
const int ENB = 11;
const int ABS = 150;
unsigned long RED;
#define A 16736925

#define B 16754775

#define X 16712445

#define C 16720605

#define D 16761405

#define E 5316027

#define F 2747854299

#define Y 3622325019

#define G 1386468383

#define H 553536955

#define I 16738455

#define J 16750695

#define K 16756815

#define L 16724175

IRrecv irrecv(receiverpin);
decode_results results;



#define LT1 digitalRead(10) \\LT1,2,3 are just the names for the three line following sensors.
#define LT2 digitalRead(4) 
#define LT3 digitalRead(2)

#define ENA 5
#define ENB 11
#define IN1 6
#define IN2 7
#define IN3 8
#define IN4 9
#define ABS 115
void _AForward()
{
  analogWrite(ENA, 85);
  analogWrite(ENB, 85);
  analogWrite(in1, 85); //analog output
  analogWrite(in2, LOW);
  analogWrite(in3, LOW);
  analogWrite(in4, 85);
  Serial.println("Forward");
}
void _DForward()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, HIGH); //digital output
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  Serial.println("Forward");
}
void forward() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go forward!");
}
void _DBack()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.println("Back");
}
void _ABack()
{
  analogWrite(ENA, 130);
  analogWrite(ENB, 130);
  analogWrite(in1, LOW);
  analogWrite(in2, 130);
  analogWrite(in3, 130);
  analogWrite(in4, LOW);
  Serial.println("Back");
}
void back() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go back!");
}

void left() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go left!");
}
void _left()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.println("Left");
}
void _right()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  Serial.println("Right");
}
void right() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go right!");
}
void _DStop()
{
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("STOP!");
}
void stop() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Stop!");
}
void _snakeR()
{
  _right();
  delay(50);
  _left();
  delay(50);
  _DForward();
  delay(50);
  _right();
  delay(50);
  _left();
  delay(50);
  _right();
  delay(50);
  _left();
  delay(50);
}

void LineR() {
  if (LT2) {
    forward();
  }
  else if (LT1) {
    left();
    while (LT1);
  }
  else if (LT3) {
    right();
    while (LT3);
  }
}
void Line() {
  for (int var5 = 0; var5 <= 100; var5++) {
    LineR();
  }
}
void _snake() {
  for (int var2 = 0; var2 <= 20; var2++) {
    _snakeR();
    if (var2 == 20) {
      break;
      Serial.println("Rod 1 is done");
    }
  }
}


void _rodR() {
  _DForward();
  delay(100);
  _DBack();
  delay(100);
}

void _rodR2() {
  _AForward();
  delay(1000);
  _ABack();
  delay(650);
}

void _rod() {
  for (int var1 = 0; var1 <= 5; var1++) {
    _rodR();
  }
}


void _rod2() {
  for (int var4 = 0; var4 <= 5; var4++) {
    _rodR2();
  }
}
void _shakeR() {
  _right();
  delay(40);
  _left();
  delay(40);
}
void _shake() {
  for (int var3 = 0; var3 <= 30; var3++) {
    _shakeR();
  }
}
void _Stayng() {
  _left();
  delay(200);
  _DStop();
  delay(100);
  _DForward();
  delay(200);
  _DStop();
  delay(700);
  _right();
  delay(420);
  _DStop();
  delay(100);
  _DForward();
  delay(300);
  _DStop();
  delay(300);
  _left();
  delay(220);
  _DStop();
  delay(1000);
  _right();
  delay(200);
  _left();
  delay(300);
  _DStop();
  delay(1000);
}
void setup() {
  Serial.begin(9600);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(receiverpin, INPUT);
  pinMode(receiverpin, INPUT);
  irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results))
  {
    int RED = results.value;
    Serial.println(RED);
    irrecv.resume();
    delay(150);
    switch (RED) {
      case A:
        _DForward();
        break;
      case B:
        _DBack();
        break;
      case C:
        _left();
        break;
      case D:
        _right();
        break;
      case E:
        _DForward();
        break;
      case F:
        _DBack();
        break;
      case G:
        _left();
        break;
      case H:
        _right();
        break;
      case X:
        _DStop();
        break;
      case Y:
        _DStop();
        break;
      case I:
        _shake();
        _DStop();
        break;
      case J:
        _rod();
        _DStop();
        break;
      case K:
        _Stayng();
        _DStop();
        break;
      case L:
        if (LT2) {                   // this "L" case gets triggered by the ir remote and activates line
          forward();              //tracking, but instead it causes the car to turn to the left.
        }
        else if (LT1) {
          left();
          while (LT1);
        }
        else if (LT3) {
          right();
          while (LT3);
        }
  }
}
}

What do you receive when you press 4 ? It is not obvious from your program.

The code would be easier to read if you used meaningful names and you could eliminate the huge if/else section by using switch/case based on the value received.

#include <IRremote.h>

int receiverpin = 12;
int in1 = 6;
int in2 = 7;
int in3 = 8;
int in4 = 9;
int ENA = 5;
int ENB = 11;
int ABS = 150;
unsigned long RED;
#define A 16736925 //Down to ----->

#define B 16754775

#define X 16712445

#define C 16720605

#define D 16761405

#define E 5316027

#define F 2747854299

#define Y 3622325019

#define G 1386468383

#define H 553536955 /* this is just to make the car go forward, back, left, right and stop. I'm not too sure why double the amount is needed but that is what we were told to use. I will see if I can't make it shorter*/
#define I 16738455  // code # related to number 1 on the remote

#define J 16750695  // code # related to number 2 on the remote

#define K 16756815  //code # related to number 3 on the remote

#define L 16724175  // code # related to number 4 on the remote

IRrecv irrecv(receiverpin);
decode_results results;

// the code up top is related mostly to IR
// the code from here
#define LT1 digitalRead(10)
#define LT2 digitalRead(4)
#define LT3 digitalRead(2)

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

#define ABS 115
// to here is for line tracking.
/* I was experimenting with different ways make the car go forward and ended up with _A and _D 
the difference is _A is analog(car does action slower) and _D is digital(Car does action at max speed)
and the other functions not labeled are for line tracking. Now, thats only for forward, back, and stop. 
the function "left" and "right" are related to IR and the "_left" and "_right" are for line tracking. */

void _AForward()
{
  analogWrite(ENA, 85);
  analogWrite(ENB, 85);
  analogWrite(in1, 85); //analog output
  analogWrite(in2, LOW);
  analogWrite(in3, LOW);
  analogWrite(in4, 85);
  Serial.println("Forward");
}
void _DForward()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, HIGH); //digital output
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  Serial.println("Forward");
}
void forward() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go forward!");
}
void _DBack()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.println("Back");
}
void _ABack()
{
  analogWrite(ENA, 130);
  analogWrite(ENB, 130);
  analogWrite(in1, LOW);
  analogWrite(in2, 130);
  analogWrite(in3, 130);
  analogWrite(in4, LOW);
  Serial.println("Back");
}
void back() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go back!");
}

void left() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go left!");
}
void _left()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.println("Left");
}
void _right()
{
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  Serial.println("Right");
}
void right() {
  analogWrite(ENA, ABS);
  analogWrite(ENB, ABS);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go right!");
}
void _DStop()
{
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("STOP!");
}
void stop() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Stop!");
}
// Now i have made some amusing functions
void _snakeR()
{
  _right();
  delay(50);
  _left();
  delay(50);
  _DForward();
  delay(50);
  _right();
  delay(50);
  _left();
  delay(50);
  _right();
  delay(50);
  _left();
  delay(50);
}

void LineR() {
  if (LT2) {
    forward();
  }
  else if (LT1) {
    left();
    while (LT1);
  }
  else if (LT3) {
    right();
    while (LT3);
  }
}
void Line() {
  for (int var5 = 0; var5 <= 100; var5++) {
    LineR();
  }
}
void _snake() {
  for (int var2 = 0; var2 <= 20; var2++) {
    _snakeR();
    if (var2 == 20) {
      break;
      Serial.println("Rod 1 is done");
    }
  }
}


void _rodR() {
  _DForward();
  delay(100);
  _DBack();
  delay(100);
}

void _rodR2() {
  _AForward();
  delay(1000);
  _ABack();
  delay(650);
}

void _rod() {
  for (int var1 = 0; var1 <= 5; var1++) {
    _rodR();
  }
}


void _rod2() {
  for (int var4 = 0; var4 <= 5; var4++) {
    _rodR2();
  }
}
void _shakeR() {
  _right();
  delay(40);
  _left();
  delay(40);
}
void _shake() {
  for (int var3 = 0; var3 <= 30; var3++) {
    _shakeR();
  }
}
void _Stayng() {
  _left();
  delay(200);
  _DStop();
  delay(100);
  _DForward();
  delay(200);
  _DStop();
  delay(700);
  _right();
  delay(420);
  _DStop();
  delay(100);
  _DForward();
  delay(300);
  _DStop();
  delay(300);
  _left();
  delay(220);
  _DStop();
  delay(1000);
  _right();
  delay(200);
  _left();
  delay(300);
  _DStop();
  delay(1000);
}
void setup() {
  Serial.begin(9600);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(receiverpin, INPUT);
  pinMode(receiverpin, INPUT);
  irrecv.enableIRIn();
}

void loop() {

  if (irrecv.decode(&results))
  {
    int RED = results.value;
    Serial.println(RED);
    irrecv.resume();
    delay(150);
    switch (RED) {
      case A:
        _DForward();
        break;
      case B:
        _DBack();
        break;
      case C:
        _left();
        break;
      case D:
        _right();
        break;
      case E:
        _DForward();
        break;
      case F:
        _DBack();
        break;
      case G:
        _left();
        break;
      case H:
        _right();
        break;
      case X:
        _DStop();
        break:
      case Y:
        _DStop();
        break
      case I:
        _shake();
        _DStop();
        break;
      case J:
        _rod();
        _DStop();
        break;
      case K:
        _Stayng();
        _DStop();
        break;
      case L:  // now with this new code This is what will be excuted when 4 on the remote is pressed.
        Line();
        break;
    }
  }
}

Here you go. The new code with switch and case, and more of a description.
But I will not be able to test it until Monday. But none the less thank you for reminding me of the switch cases.

If anything can be improved or is incorrect, don’t hesitate.

break:

You will need to fix this

You should also consider changing the data type of your variables to the most appropriate size. Pin numbers could, for instance, be byte instead of int and you could make them const whilst you are about it as they are never going to change in your program.

The #defined single letters are still anonymous but I assume that they mean something to you.

Doing this

#define LT1 digitalRead(10)
#define LT2 digitalRead(4)
#define LT3 digitalRead(2)

really obscures what the code does due to the need to refer back to the #define when seeing Lt1, LT2 and LT3 in the code. The oin numbers too are anonymois and could be given names.