Code stuck in loop

Below is some simple code for a little autonomous robot I am working on. The robot is to go forward until an obstacle and then look left and right for the best path. Everything works great until the robot stops. The servo moves left then right and records, instead of 2 values with the ping sensor, 3. Not sure why because it only pings 2 times unless it is exiting the function. After that, it gets stuck in a loop where the robot just keeps looking left and right and never turns out of the way of the obstacle. If anyone could help me spot my problem I would really appreciate it, I wanted to finish this today.

#include <TimedAction.h>
#include <Servo.h>
 
 int enable = 53;
 int dir1 = 49;
 int pwm1 = 2;
 int dir2 = 51;
 int pwm2 = 3;
 int servo = 4;
 int pos = 45;
 Servo myservo;
 
 const int pingPin = 5;
 
 long Ranging[3];
 //int x = 0;
 
 TimedAction repeatScan = TimedAction(1000, Scan);
 
void setup()
{
 Serial.begin(9600);
 pinMode(enable, OUTPUT);
 pinMode(dir1, OUTPUT);
 pinMode(pwm1, OUTPUT);
 pinMode(dir2, OUTPUT);
 pinMode(pwm2, OUTPUT);
 pinMode(servo, OUTPUT);
 
 myservo.attach(servo);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
 repeatScan.check();
}

long duration;
long cm;

long ping(){ // ping ultrasonic sensor
 
 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
  delay(100);
  return cm;
 
}
void Scan(){
 
  myservo.write(90);
   Ranging[2] = ping();//Front
   Serial.println(Ranging[2]);
   if(Ranging[2] >= 40){
    digitalWrite(enable, HIGH);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, LOW);
    forward();
    Serial.println("Forward");
   }
   else{
     mstop();
     checkdir();
   }
 
  }

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void forward(){
 // pwm1 is left
 analogWrite(pwm1, 20); // PWM at 20% is 51
 analogWrite(pwm2, 20); // PWM at 20% is 51

}

void checkdir(){
 repeatScan.disable();

 myservo.write(45);
 delay(500);
 Ranging[0] = ping();//Left
 Serial.println(Ranging[0]);
 myservo.write(135);
 delay(500);
 Ranging[1] = ping();//Right
 Serial.println(Ranging[1]);
 delay(200);
 myservo.write(90);
 //delay(200);
 
  turn();

}

void mstop(){
 analogWrite(pwm1, 0); // PWM at 20% is 51
 analogWrite(pwm2, 0); // PWM at 20% is 51
 digitalWrite(enable, LOW);
 }

void turn(){
 
  if (Ranging[0] > Ranging[1]) {
   digitalWrite(dir1, HIGH);
   digitalWrite(dir2, LOW);
   forward();
   Serial.println("Left");
   delay(250);
  }
  else if (Ranging[1] > Ranging[0]) {
 digitalWrite(dir1, LOW);
 digitalWrite(dir2, HIGH);
 forward();
 Serial.println("Right");
 delay(250);
  }
 
  repeatScan.enable();
}

You've got debug prints there, what do they tell you?

Why do you have a delay in "ping" just before you return the (global) value?

I think it is looping because it still meets the condition to start the checkdir() function. Any idea how to fix this?

Also, heres some of what is being printed since you wanted to see it: 84 Forward 83 Forward 83 Forward 83 Forward 24 18 0 Left 0 20 5 Left 0 21 50 Right 0 83 50 Left 0 83 50 Left

Useless:

 Serial.println(Ranging[0]);

Not-so-Useless:

Serial.print("Ranging[0] = [");
Serial.print(Ranging[0]);
Serial.println("]");

Anyone know the code that I could use to fix this issue?

Anyone know the code that I could use to fix this issue?

I don't think you've provided enough information. The three values that are printed between Forward and Left correspond to what?

When Left is printed, does the robot turn left?

the robot never turns, it gets stuck in a loop. It just sits there.

it gets stuck in a loop.

In what loop?

   digitalWrite(dir1, HIGH);
   digitalWrite(dir2, LOW);
   forward();
   Serial.println("Left");

If the robot doesn't move when "Left" is printed, the code isn't "stuck in a loop". It's wrong.