'ping' was not declared in this scope

Hello, I’m fairly new to Arduino and coding in general, and I have a problem that needs to be fixed. So I’m building a robot which will have two “modes”, one where you can control it via Bluetooth and another where it is autonomous. This is the code for the Bluetooth control

#include <Servo.h> //include Servo library
#include <HC_SR04.h>

#define TRIG_PIN 3   // HC_SR04 trig pin to Arduino pin 3
#define ECHO_PIN 2   // HC_SR04 echo pin to Arduino pin 2 (this pin is Arduino's interrupt pin 0)
#define ECHO_INT 0   // The HC_SR04 echo pin is connected to Arduino pin 2 which is interrupt id 0)  

HC_SR04 sensor(TRIG_PIN, ECHO_PIN, ECHO_INT);  // Create the sensor object

const int RForward          = 110;  //the speed of the servo, maximum speed is 180
const int RForwardHalf      = 100;
const int RBackward         = 60;   
const int LForward          = 70; 
const int LBackward         = 120; 
const int LBackwardHalf     = 80;
const int RNeutral          = 90;  //centered position
const int LNeutral          = 90;
const int RightLightSensor  = 0;  // This is analog pin 0 for the right light sensor
const int LeftLightSensor   = 2;  // This is analog pin 2 for the left light sensor
const int collisionThresh   = 15; //threshold for obstacles (in cm)
const int threshold_pot     = 4;  // Use this potentiometer to calibrate the threshold of
                                  // the sensors to the light source. NOTE: this is ANALOG pin 4
const int closeness         = 60; // This value defines when the left and right light
                                  // sensors detect light at a similar enough intensity
                                  // to indicate that the light source is straight ahead. 
const int RightLEDIndicator = 5;  // This is digital pin
const int LeftLEDIndicator  = 4;  // This is digital pin                                                                   

int SensorLeft;                  // Stores the value from the left light sensor
int SensorRight;                 // Stores the value from the right light sensor
int SensorDifference;            // Stores absolute difference between the values from the two light sensors
int leftDistance, rightDistance; //distances on either side


Servo panMotor;   //The micro servo with the distance sensor on it
Servo leftMotor;  //the Left motor
Servo rightMotor; //the Right motor

void setup()
{
  rightMotor.attach(11);  //attach the right motor
  leftMotor.attach(10);   //attach the left motors
  panMotor.attach(6);     // attach the micro servo
  panMotor.write(90);     //center the micro servo

  sensor.begin();
  
  pinMode(TRIG_PIN,          OUTPUT);
  pinMode(ECHO_PIN,          INPUT);
  pinMode(LeftLightSensor,   INPUT);
  pinMode(RightLightSensor,  INPUT);
  pinMode(threshold_pot,     INPUT);  
  pinMode(RightLEDIndicator, OUTPUT);
  pinMode(LeftLEDIndicator,  OUTPUT);

  Serial.begin(9600);
}

void loop(){
char c = Serial.read();

if(c == '1'){
  leftMotor.write(70); //go forward
  rightMotor.write(110);
  delay(300);
}
else if (c == '2') {
  leftMotor.write(120); //go backward
  rightMotor.write(60);
  delay(300);
}
else if (c == '3') {
  leftMotor.write(120); //go left
  rightMotor.write(110);
  delay(80);
  
}
else if (c == '4') {
  leftMotor.write(70); //go right
  rightMotor.write(60);
  delay(80);
}
else if (c == 'y') {
  leftMotor.write(90); //nuetral position
  rightMotor.write(90);
}
else if (c =='x') {
//run autonomouse code
}

And this is the code for the autonomous

// the void setup and all declarations are the same as in the bluetooth code
void loop(){int threshold = analogRead(threshold_pot);
  int distance  = ping();  //call the ping function to get the distance in front of the robot
  SensorLeft = analogRead(LeftLightSensor);  //read the left light sensor

  SensorRight = analogRead(RightLightSensor); //read the right light sensor
 

  SensorDifference = abs(SensorLeft - SensorRight);  //Calcuate the difference between
                                                     // left and right light sensor

  if (distance > collisionThresh) //if path is clear, the robot can use the light sensors to navigate towards the light
  {
  // Check the conditions for moving forward
  if ( SensorLeft       <= threshold     && 
       SensorRight      <= threshold     && 
       SensorLeft       >= threshold/2   && 
       SensorRight      >= threshold/2   && 
       SensorDifference <= closeness) 
       {  
          leftMotor.write(LForward); 
          rightMotor.write(RForward);
          digitalWrite(LeftLEDIndicator, HIGH);
          digitalWrite(RightLEDIndicator, HIGH);
          delay(300);  
        }

  //This condition indicates that the robot has reached the light source,
  //so it will stop
  else if ( SensorLeft       >= threshold && 
            SensorRight      >= threshold && 
            SensorDifference <= closeness) 
       {  
          leftMotor.write(LNeutral);
          rightMotor.write(RNeutral); 
          digitalWrite(RightLEDIndicator, LOW);
          digitalWrite(LeftLEDIndicator, LOW);
          delay(500);
        }
        
  // If the left light sensor value is smaller than that of the right sensor, 
  // and the difference between the values is large, then
  // the light source is towards the left, so robot should turn left. 
  else if ( SensorLeft       <  SensorRight &&       
            SensorRight       >= threshold/2 ) 
            { 
              leftMotor.write(LForward); 
              rightMotor.write(RBackward);
              digitalWrite(RightLEDIndicator, LOW);
              digitalWrite(LeftLEDIndicator, HIGH);
              delay(80);
            }

  // If the right light sensor value is smaller than that of the left sensor, 
  // and the difference between the values is large, then
  // the light source is towards the right, so robot should turn right. 
  else if ( SensorLeft       >  SensorRight  &&        
            SensorLeft       >= threshold/2 ) 
            { 
                leftMotor.write(LBackward); 
                rightMotor.write(RForward); 
                digitalWrite(RightLEDIndicator, HIGH);
                digitalWrite(LeftLEDIndicator, LOW);
                delay(80);
            }

  //This condition indicates that both sensor of the robot are heading away from,
  //the light, so it should turn around
  else if ( SensorLeft  < threshold/2 && 
            SensorRight < threshold/2) 
            {                
                leftMotor.write(LBackwardHalf); 
                rightMotor.write(RForwardHalf); 
                delay(300);
                digitalWrite(RightLEDIndicator, LOW);
                digitalWrite(LeftLEDIndicator, LOW);
            }
  }
  else     {
      leftMotor.write(LNeutral);   // Stop the left motor
      rightMotor.write(RNeutral);  // Stop the right motor
      panMotor.write(0);           // Turn the distance sensor to the left
      delay(400);                  // Wait for the sensor to move into position
      leftDistance = ping();       // scan to the right
      delay(400);                  // Wait for the sensor to move into position
      panMotor.write(180);         // Turn the distance sensor to the right
      delay(600);                  // Wait for the sensor to move into position
      rightDistance = ping();      // Scan to the left
      delay(400);                  // Wait for the sensor to move into position
      panMotor.write(90);          // Return to center
      delay(400);                  // Wait for the sensor to move into position
      compareDistance();
  }
  
}

void compareDistance()
{
  if (leftDistance > rightDistance)
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); 
    delay(400); 
    leftMotor.write(LForward);  
    rightMotor.write(RForward); 
    delay(600);
  }
  else if (rightDistance > leftDistance) 
  {   
    leftMotor.write(LForward);
    rightMotor.write(RBackward); 
    delay(400);
    leftMotor.write(LForward);  // Then move forward just a bit
    rightMotor.write(RForward); // to try and clear the obstacle
    delay(600);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn 180 degrees
    delay(400);
  }
}

long ping()
{
  sensor.start();                         // Start the sensor to take a distance measurement
  while(!sensor.isFinished()) continue;   // Wait until the sensor returns a reading. We need a valid value before we can continue.
  return sensor.getRange();               // The sensor has returned a reading, get it and return it to the caller.
}
}

I'm not real familiar with the HC-SR04N but have you tried sensor.ping()?

long ping()
{
  sensor.start();                         // Start the sensor to take a distance measurement
  while(!sensor.isFinished()) continue;   // Wait until the sensor returns a reading. We need a valid value before we can continue.
  return sensor.getRange();               // The sensor has returned a reading, get it and return it to the caller.
}
} <<< Oops