error: 'ping' was not declared in this scope

Hello all. I have a hopefully easy problem I have not been able to solve. I am using 0023 and have been able to make a few changes to the attached code but am getting:

VarSpeedServoPingneedsWork.cpp: In function 'void loop()':
VarSpeedServoPingneedsWork:41: error: 'ping' was not declared in this scope

I have been searching the internet and this forum for a couple hours and have tried the solutions from other posts but non seem to work here. I left in the code attempts but commented them out. The overall goal is to adapt this code to use a Ping sonar sensor to scan for and detect an object, then move to the object using continuous motion servos and stop a pre-determined distance away. Moving forward, back, right, left, and stop all need to function based on the Ping return distance. The code is hanging up at the very beginning of the loop. I am new to programming so explanations may need to be a little verbose. Thanks in advance!!

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

void setup();
void loop();
//void ping();
long microsecondsToInches(long microseconds);
long microsecondsToCentimeters(long microseconds);

//#define ping 1
//#include <Servo.h> //include Servo library

const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
const int RNeutral = 95; //value 90 for center, my servo is a little off
const int LNeutral = 95; //value 90 for center, my servo is a little off
const int pingPin = 8;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int irPin = 0;  //Sharp infrared sensor connected to this arduino pin "not currently used"
const int dangerThresh = 20; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance; //distances on either side
//int distanceFwd = 0;
//int Scan;
//int ping;
VarSpeedServo panMotor; //servo that pans the ping sensor 
VarSpeedServo leftMotor;  //continuous rotation servo that drives the left wheel
VarSpeedServo rightMotor; //continuous rotation servo that drives the right wheel
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  leftMotor.attach(10);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  panMotor.attach(6); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(90); //set PING))) pan to center "90 by default"

}

void loop()
{
//  Scan = distanceFwd(ping()); //ping stright ahead
  int distanceFwd = ping(); //ping stright ahead  
  //int distanceFwd = ping(); //ping stright ahead
  if (distanceFwd>dangerThresh) //if the path is clear

  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral); //stop left drive motor
    rightMotor.write(RNeutral); //stop right drive motor
    panMotor.write(0); //turn ping sensor right, 0 for full right
    delay(500);        //wait this many milliseconds
    rightDistance = ping(); //ping
    delay(500);  //wait this many milliseconds
    panMotor.write(170); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
    delay(500);  //wait this many milliseconds
    leftDistance = ping(); //ping
    delay(500);  //wait this many milliseconds
    panMotor.write(83); //return to center, "90 by default" my servo is off center
    delay(100);  //wait this many milliseconds
    compareDistance(); //compare the two distances measured
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //rotate left drive servo bacward "skid steer"
    rightMotor.write(RForward); //rotate right drive servo forward "skid steer"
    delay(1000); //run motors this many milliseconds
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward "skid steer"
    rightMotor.write(RBackward); //rotate right drive servo backward "skid steer"
    delay(1000);  //run motors this many milliseconds
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward
    rightMotor.write(RBackward); //rotate right drive servo backward
    delay(2000); //run motors this many milliseconds
  }
}

The code is hanging up at the very beginning of the loop

How can it, when it won't even compile?

  int distanceFwd = ping(); //ping stright ahead

So, ping() needs to be a function.

//void ping();

Not needed. The IDE creates function prototypes for you. It doesn't comment them out, either.

//int ping;

You can't have variables and functions with the same name.

Where is your implementation of the ping() function? You need to do more than say you are going to have one. You actually have to provide one.

Thanks for your quick reply PaulS.
I apologize for mis-stating the issue. should have said it got hung up during the compile at the beginning of the loop.
As for the missing function, that is what I did not understand. I did a quick search and found a description of a function and I will work on writing a 'ping()' function and post back when done or another problem arises.

Ok. Made some changes and now a different error.

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

long microsecondsToInches(long microseconds);
long microsecondsToCentimeters(long microseconds);
long duration; //time it takes to recieve PING))) signal
long distanceInches;
long distanceCm;

const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
const int RNeutral = 95; //value 90 for center, my servo is a little off
const int LNeutral = 95; //value 90 for center, my servo is a little off
const int pingPin = 8;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
//const int irPin = 0;  //Sharp infrared sensor connected to this arduino pin "not currently used"
const int dangerThresh = 20; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance; //distances on either side
int delayTime = 100;

VarSpeedServo pingServo;
VarSpeedServo panMotor; //servo that pans the ping sensor 
VarSpeedServo leftMotor;  //continuous rotation servo that drives the left wheel
VarSpeedServo rightMotor; //continuous rotation servo that drives the right wheel

int pingScan()
{
  
  pingServo.write(90);
  delay(delayTime);
  
  pingServo.write(180);
  delay(delayTime);
  
  pingServo.write(0);
  delay(delayTime);
   
  pingServo.write(90);
  delay(delayTime);
  
  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  distanceInches = microsecondsToInches(duration);
  distanceCm = microsecondsToCentimeters(duration);
  Serial.print(distanceInches);
  Serial.print("in, ");
  Serial.print(distanceCm);
  Serial.print("cm");
  Serial.println();
  delay(100); 
  
}

void setup()
{
  rightMotor.attach(11); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  leftMotor.attach(10);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  panMotor.attach(6); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(90); //set PING))) pan to center "90 by default"

}

void loop()
{
  int distanceFwd = pingScan(); //ping stright ahead  
  if (distanceFwd>dangerThresh) //if the path is clear

  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral); //stop left drive motor
    rightMotor.write(RNeutral); //stop right drive motor
    panMotor.write(0); //turn ping sensor right, 0 for full right
    delay(500);        //wait this many milliseconds
    rightDistance = pingScan(); //ping
    delay(500);  //wait this many milliseconds
    panMotor.write(170); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
    delay(500);  //wait this many milliseconds
    leftDistance = pingScan(); //ping
    delay(500);  //wait this many milliseconds
    panMotor.write(83); //return to center, "90 by default" my servo is off center
    delay(100);  //wait this many milliseconds
    compareDistance(); //compare the two distances measured
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //rotate left drive servo bacward "skid steer"
    rightMotor.write(RForward); //rotate right drive servo forward "skid steer"
    delay(1000); //run motors this many milliseconds
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward "skid steer"
    rightMotor.write(RBackward); //rotate right drive servo backward "skid steer"
    delay(1000);  //run motors this many milliseconds
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward
    rightMotor.write(RBackward); //rotate right drive servo backward
    delay(2000); //run motors this many milliseconds
  }
}

The compile provides the following errors but the references are at the top of the code so not sure what exactly is the problem:

VarSpeedServoPingneedsWork2.cpp.o: In function `pingScan()':
C:\Users\Me\AppData\Local\Temp\build8523946266365575811.tmp/VarSpeedServoPingneedsWork2.cpp:58: undefined reference to `microsecondsToInches(long)'
C:\Users\Me\AppData\Local\Temp\build8523946266365575811.tmp/VarSpeedServoPingneedsWork2.cpp:59: undefined reference to `microsecondsToCentimeters(long)'

not sure what exactly is the problem:

Exactly the same as with the ping() function. You have to do more than provide a function prototype. You have to actually implement the function.

Ok, here is the latest code. I added the distance function and added code to output the Ping readings to the Serial Monitor so I can verify operation. The Ping sensor is providing correct distances but when the 10 cm threshold is crossed, the drive servos do not change direction but keep on driving in the same direction. It would be nice to be able to step through the code as it runs to help see the problems, but hopefully your experience can see why the Ping return values are not affecting the drive servos. Thanks!!

const int TxPin = 7;
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

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

long duration,distanceInches,distanceCm; //time it takes to recieve PING))) signal

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

const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
const int RNeutral = 90; //value 90 for center, my servo is a little off
const int LNeutral = 90; //value 90 for center, my servo is a little off
const int pingPin = 8;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
//const int irPin = 0;  //Sharp infrared sensor connected to this arduino pin "not currently used"
const int dangerThresh = 10; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance; //distances on either side
int delayTime = 2;

VarSpeedServo pingServo; //pings the servo when called
VarSpeedServo panMotor; //servo that pans the ping sensor 
VarSpeedServo leftMotor;  //continuous rotation servo that drives the left wheel
VarSpeedServo rightMotor; //continuous rotation servo that drives the right wheel

int pingScan() //sets the ping for static ping servo directions
{
    pingServo.write(90);
  delay(delayTime);
  
  pingServo.write(180);
  delay(delayTime);
  
  pingServo.write(0);
  delay(delayTime);
   
  pingServo.write(90);
  delay(delayTime);
  
  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  distanceInches = microsecondsToInches(duration); //displays returned conversion to inches
  distanceCm = microsecondsToCentimeters(duration); //displays returned conversion to centimeters
  Serial.print(distanceInches);
  Serial.print("in, ");
  Serial.print(distanceCm);
  Serial.print("cm");
  Serial.println();
  delay(100); 
  
}

void setup()
{
  rightMotor.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  leftMotor.attach(5);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  panMotor.attach(4); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(90); //set PING))) pan to center "90 by default"
  
    lcd.begin(2, 12);        // This sets the lcd to a two line, 12 char display
  // initialize serial communication:
     pinMode(TxPin, OUTPUT);
     digitalWrite(TxPin, HIGH); 
     mySerial.begin(9600); 
  Serial.begin(9600);

}

void loop()
{
  
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);        //Prints to Serial Monitor
  Serial.print("in, ");        //Prints to Serial Monitor
  Serial.print(cm);            //Prints to Serial Monitor
  Serial.print("cm");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor  
    mySerial.print(" Inches ");            // First line
    mySerial.print(inches);               // Second line
      delay(100);                        // Wait 3 seconds is 3000
  lcd.clear();                 //Prints to LCD
  lcd.print("Inches ");        //Prints to LCD 
  lcd.print(inches);           //Prints to LCD
  lcd.setCursor(0, 1);         //Prints to LCD
  lcd.print("Centmtrs ");      //Prints to LCD
  lcd.print(cm);               //Prints to LCD

  delay(100);
      
      
      
      
      
  int distanceFwd = pingScan(); //ping stright ahead  
  if (distanceFwd>dangerThresh) //if the path is clear

  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  
  else //if path is blocked
  {
    leftMotor.write(LNeutral); //stop left drive motor
    rightMotor.write(RNeutral); //stop right drive motor
    panMotor.write(0); //turn ping sensor right, 0 for full right
    delay(500);        //wait this many milliseconds
    rightDistance = pingScan(); //ping
    delay(500);  //wait this many milliseconds
    panMotor.write(170); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
    delay(500);  //wait this many milliseconds
    leftDistance = pingScan(); //ping
    delay(500);  //wait this many milliseconds
    panMotor.write(83); //return to center, "90 by default" my servo is off center
    delay(100);  //wait this many milliseconds
    compareDistance(); //compare the two distances measured
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //rotate left drive servo bacward "skid steer"
    rightMotor.write(RForward); //rotate right drive servo forward "skid steer"
    delay(1000); //run motors this many milliseconds
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward "skid steer"
    rightMotor.write(RBackward); //rotate right drive servo backward "skid steer"
    delay(1000);  //run motors this many milliseconds
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward
    rightMotor.write(RBackward); //rotate right drive servo backward
    delay(2000); //run motors this many milliseconds
  }
}

t would be nice to be able to step through the code

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

  long duration, inches, cm;
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

inches and cm are valued based on an uninitialized local variable. How is that helpful?

How does having a local variable with the same name as a global variable help?

int pingScan() //sets the ping for static ping servo directions
{
    pingServo.write(90);
  delay(delayTime);
  
  pingServo.write(180);
  delay(delayTime);
  
  pingServo.write(0);
  delay(delayTime);
   
  pingServo.write(90);
  delay(delayTime);

Prior to calling this function, you point the pingServo in the desired direction. Then, in this function, you wave it all over the place. How does that help?

  delay(100);

As if all the serial printing wasn't slow enough...

You need a ping() function. You may, or may not, need a pingScan() function. The ping() function should be called from pingScan(), if you need pingScan() at all. In most other places, calling ping() is more appropriate than calling pingScan().

AWOL, I do not know anything about debug prints. How do I go about getting those?
PaulS, I think I understand your comments and will see if I can modify the code to reflect them. One thing I have not addressed yet and is probably not relevant until the Ping returns are able to control the hacked continuous rotation servos, is that the servos themselves are not centered and I do not know how to do that. There is no physical adjustment I can see so I thought there was an electronic way to center them before the rest of the code begins. Thanks for all the help!!

const int RNeutral = 90; //value 90 for center
const int LNeutral = 90; //value 90 for center

One thing I have not addressed yet and is probably not relevant until the Ping returns are able to control the hacked continuous rotation servos, is that the servos themselves are not centered and I do not know how to do that.

What does "centered" mean for a continuous rotation servo?

If you are referring to the regular servo, you control the full range of motion. You simply write() for it to go to the middle of the range. Whatever the range is.

I think the best way to indicate that a continuous rotation servo is at a theoretical center would be to capture the value of the servo when it is stopped and use that value as the current center for the rest of the code to work from. Every time the servo stops, this value is re-determined. But I would not know how to code this. A quick test of your write.() suggestion indicated I need to code another function block so I will work on that as well as the rest. Thanks again!!

Ok...latest code. I added some statements so I can watch the serial monitor for changes in the variables. Currently, the rightMotor and leftMotor spin continuously even when the ping results show that the chkDistanceFwd<=dangerThresh is crossed (item too close to ping sensor). Here is a snippet of the serial monitor display when moving an item slowly to the Ping sensor:

1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 11cm
1  chkDistanceFwd  10  dangerThresh  4in, 10cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
10  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 2cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
2  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm
1  chkDistanceFwd  10  dangerThresh  0in, 1cm

I think for this to work, chkDistanceFwd needs to cm (which has been coded) after the ping() function has been initiated. This should send s ping, convert the return data to inches and cm, then assign the value of cm to chkDistanceFwd, so they should be the same value in the serial monitor but they are not. Additionally, once the 10cm threshold is crossed (at which time chkDistanceFwd and dangerThresh are equal, which would seem to indicate that the chkDistanceFwd = cm statement is working) there is a drop in the inches and cm data on the serial monitor even though the item being pinged is still 9 or 10 cm away. I tried playing with the delay thinking a longer time would allow for the processing to complete but it did not have the desired effect. Any help is greatly appreciated!!!

//const int TxPin = 2;
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

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

long duration, inches, cm; //time it takes to recieve PING))) signal

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

const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
//const int RNeutral = 90; //value 90 for center, my servo is a little off
//const int LNeutral = 90; //value 90 for center, my servo is a little off
const int pingPin = 8;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
//const int irPin = 0;  //Sharp infrared sensor connected to this arduino pin "not currently used"
const int dangerThresh = 10; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance, distanceFwd, chkDistanceFwd; //distances on either side
int delayTime = 2;

VarSpeedServo pingServo; //pings the servo when called
VarSpeedServo panMotor; //servo that pans the ping sensor 
VarSpeedServo leftMotor;  //continuous rotation servo that drives the left wheel
VarSpeedServo rightMotor; //continuous rotation servo that drives the right wheel

int ping() //sets up ping when called
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
}

void leftMotorStop()
{
digitalWrite(7, LOW);
}

void rightMotorStop()
{
digitalWrite(6, LOW);
}

void setup()  //SERVO MOTORS SHOULD NOT TURN ON UNTIL PING RETURNS TELL THEM TO!!!
{
  rightMotorStop(); //stops servo
  rightMotor.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  rightMotorStop(); //stops servo
  leftMotorStop(); //stops servo    
  leftMotor.attach(7);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  leftMotorStop(); //stops servo  
  panMotor.attach(4); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(90); //set PING))) pan to center "90 by default"

  
    lcd.begin(2, 12);        // This sets the lcd to a two line, 12 char display
  // initialize serial communication:
///     pinMode(TxPin, OUTPUT);
///     digitalWrite(TxPin, HIGH); 
///     mySerial.begin(9600); 
  Serial.begin(9600);

}

void loop()
{
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);        //Prints to Serial Monitor
  Serial.print("in, ");        //Prints to Serial Monitor
  Serial.print(cm);            //Prints to Serial Monitor
  Serial.print("cm");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor 
    Serial.print(chkDistanceFwd);
    Serial.print("  chkDistanceFwd  "); //displays value of chkDistanceFwd to validate operation
//    Serial.print(cm);
//    Serial.print("  rightDistance  "); //displays value of rightDistance to validate operation  
 //   Serial.print(cm);  
  //  Serial.print("  leftDistance  "); //displays value of leftDistance to validate operation
    Serial.print(dangerThresh);  
    Serial.print("  dangerThresh  "); //displays value of dangerThresh to validate operation   
    
    
//    mySerial.print(" Inches ");            // First line
//    mySerial.print(inches);               // Second line
 //     delay(100);                        // Wait 3 seconds is 3000
  lcd.clear();                 //Prints to LCD
  lcd.print("Inches ");        //Prints to LCD 
  lcd.print(inches);           //Prints to LCD
  lcd.setCursor(0, 1);         //Prints to LCD
  lcd.print("Centmtrs ");      //Prints to LCD
  lcd.print(cm);               //Prints to LCD

//  delay(100);

  //distanceFwd = ping(); //ping stright ahead
    ping(); //ping stright ahead
  if (cm>dangerThresh) //if the path is clear

  {
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  
  else //if path is blocked
  {
    rightMotorStop(); //stops servo
    leftMotorStop(); //stops servo 
   // leftMotor.write(LNeutral); //stop left drive motor
   // rightMotor.write(RNeutral); //stop right drive motor   
        
    ping();
    chkDistanceFwd = cm; //ping stright ahead 
    delay(200);        //wait this many milliseconds WAS 500    
//    panMotor.write(0); //turn ping sensor right, 0 for full right
//    delay(500);        //wait this many milliseconds WAS 500
//    rightDistance = ping(); //ping
//    delay(500);  //wait this many milliseconds WAS 500
//    panMotor.write(170); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
//    delay(500);  //wait this many milliseconds WAS 500
//    leftDistance = ping(); //ping
//    delay(500);  //wait this many milliseconds WAS 500
//    panMotor.write(83); //return to center, "90 by default" my servo is off center
//    delay(100);  //wait this many milliseconds
//    compareDistance(); //compare the two distances measured
  }
}
  
void compareDistance()
{
  if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance
  {
    rightMotorStop(); //stops servo NOT WORKING
    leftMotorStop(); //stops servo  NOT WORKING 
  //  rightMotor.attach(12); //turns off servo by attaching to unused pin
  //  leftMotor.attach(11); //turns off servo by attaching to unused pin   
 //   delay(1000); //run motors this many milliseconds
  }  
  else if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); //rotate left drive servo bacward "skid steer"
    rightMotor.write(RForward); //rotate right drive servo forward "skid steer"
    delay(1000); //run motors this many milliseconds
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward "skid steer"
    rightMotor.write(RBackward); //rotate right drive servo backward "skid steer"
    delay(1000);  //run motors this many milliseconds
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); //rotate left drive servo forward
    rightMotor.write(RBackward); //rotate right drive servo backward
    delay(2000); //run motors this many milliseconds
  }
}

Found out how to debug so I will not post the code, but here are the results. I tried to be descriptive in the debug comments so 'Begin Loop' is at the beginning of the 'void Loop()' or right before and after a decision or action like the 'rightMotorStop();' part of the code that should stop the servo if it is run. What it is currently showing is that the functions

void leftMotorStop()
{
digitalWrite(7, LOW);
}

void rightMotorStop()
{
digitalWrite(6, LOW);
}

are not coded correctly. I thought a LOW signal would turn off the input to the pins and stop the servos. How should these be coded?

Begin Loop

After inches and cm calculations

4in, 11cm
0  chkDistanceFwd  10  dangerThresh  

Between serial print and lcd print

Before ping and compare (cm>dangerThresh)

if (cm>dangerThresh) //if the path is clear

leftMotor.write(LForward); //move forward

rightMotor.write(RForward); //move forward



Begin Loop

After inches and cm calculations

4in, 10cm
0  chkDistanceFwd  10  dangerThresh  

Between serial print and lcd print

Before ping and compare (cm>dangerThresh)

  else //if path is blocked

rightMotorStop(); //stops servo***** NOT WORKING

leftMotorStop(); //stops servo***** NOT WORKING

Before ping and compare chkDistanceFwd = cm

Before (chkDistanceFwd<=dangerThresh)

if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance)

After if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance

After ping and compare chkDistanceFwd = cm



Begin Loop

After inches and cm calculations

3in, 9cm
10  chkDistanceFwd  10  dangerThresh  

Between serial print and lcd print

Before ping and compare (cm>dangerThresh)

  else //if path is blocked

rightMotorStop(); //stops servo

leftMotorStop(); //stops servo

Before ping and compare chkDistanceFwd = cm

Before (chkDistanceFwd<=dangerThresh)

if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance)

After if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance

After ping and compare chkDistanceFwd = cm



Begin Loop

After inches and cm calculations

2in, 7cm
9  chkDistanceFwd  10  dangerThresh  

Between serial print and lcd print

Before ping and compare (cm>dangerThresh)

  else //if path is blocked

rightMotorStop(); //stops servo

leftMotorStop(); //stops servo

Before ping and compare chkDistanceFwd = cm

Before (chkDistanceFwd<=dangerThresh)

if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance)

After if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance

After ping and compare chkDistanceFwd = cm

Figured it out. Was missing a library so added the library so now

void leftMotorStop()
{
leftMotor.detach();
}

void rightMotorStop()
{
rightMotor.detach();
}

works. As the object moves into the minimum distance of 10cm, the servos stop. When the object moves farther than 10cm, the servos start. Now to work on moving backwards, turning right and left based on the Ping return distance. Thanks for the help so far!!!

Hello again. After some research and trial and error, I have working code that will move the servos forwards, backwards, and stop depending on the ping distance compared with the default thresholds. I have tried several methods but have been unable to prevent the servos from rotating in reverse when connected to the signal pins. I do not want the servos to move until the command is given through the program. I tried stopping them before every block of code that didn't cause an error and even directly after they were connected but since they have to be connected to work, no matter where i connected them, they would turn on for a couple seconds. Please help me stop them!!! Thanks.

#include <SoftwareServo.h>

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

long duration, inches, cm; //time it takes to recieve PING))) signal

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

#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#endif


const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
const int pingPin = 8;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int dangerThresh = 5; //threshold for obstacles (in cm), this was 10 by default
const int dangerReverseThresh = 3; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance, distanceFwd, chkDistanceFwd; //distances on either side
int delayTime = 2;
int pos = 90;    // variable to store the servo position 

VarSpeedServo pingServo; //pings the servo when called
VarSpeedServo panMotor; //servo that pans the ping sensor 
VarSpeedServo leftMotor;  //continuous rotation servo that drives the left wheel
VarSpeedServo rightMotor; //continuous rotation servo that drives the right wheel

void leftMotorStop()
{
leftMotor.detach();
}

void rightMotorStop()
{
rightMotor.detach();
}

void ping() //sets up ping when called
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
}

  void reverse() //if they are equally obstructed
{
    rightMotor.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
   // rightMotorStop();
    leftMotor.attach(7); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin    
   // leftMotorStop();
    leftMotor.write(LBackward); //rotate left drive servo forward
    rightMotor.write(RBackward); //rotate right drive servo backward
    delay(200); //run motors this many milliseconds
   // rightMotorStop();
   // leftMotorStop();
}

void setup()
{
  rightMotor.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  rightMotorStop(); //stops servo
  leftMotor.attach(7);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  leftMotorStop(); //stops servo  
  panMotor.attach(5); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(90); //set PING))) pan to center "90 by default"
  Serial.begin(9600);
}

void loop()
{
  {
  DEBUG_PRINT("Begin Loop");
  }

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  {
  DEBUG_PRINT("After inches and cm calculations");
  }
  
  Serial.print(inches);        //Prints to Serial Monitor
  Serial.print("in, ");        //Prints to Serial Monitor
  Serial.print(cm);            //Prints to Serial Monitor
  Serial.print("cm");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor 
  Serial.print(chkDistanceFwd);
  Serial.print("  chkDistanceFwd  "); //displays value of chkDistanceFwd to validate operation
  Serial.print(dangerThresh);  
  Serial.print("  dangerThresh  "); //displays value of dangerThresh to validate operation   

    panMotor.write(pos); //return to center, "90 by default"
    delay(200);  //wait this many milliseconds   
    ping(); //ping stright ahead
    
  if (cm>dangerThresh) //if the path is clear
  {
    leftMotor.attach(7); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
    rightMotor.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
    leftMotor.write(LForward); //move forward
    rightMotor.write(RForward); //move forward
  }
  
  else //if path is blocked
  {
    panMotor.write(pos); //return to center, "90 by default"
    delay(200);  //wait this many milliseconds  
    ping();
    chkDistanceFwd = cm; //ping stright ahead 
    delay(100);        //wait this many milliseconds WAS 500    
    panMotor.write(80); //turn ping sensor right, 0 for full right
    delay(300);        //wait this many milliseconds WAS 500
    ping();
    rightDistance = cm;
    delay(100);  //wait this many milliseconds WAS 500
    panMotor.write(100); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
    delay(400);  //wait this many milliseconds WAS 500
    ping();    
    leftDistance = cm;
    delay(100);  //wait this many milliseconds WAS 500
    panMotor.write(pos); //return to center, "90 by default" my servo is off center
    delay(300);  //wait this many milliseconds
    compareDistance(); //compare the two distances measured
  }
}
  
void compareDistance()
{
  if (chkDistanceFwd<=dangerReverseThresh) //if they are equally obstructed
 {
  reverse();
 }
  else if (chkDistanceFwd<=dangerThresh) //if center object is <= minimum programmed distance
  {
    rightMotorStop();
    leftMotorStop();
  }
}

Please help me stop them!!!

Detaching the servos is not the way to stop them. If they are continuous rotation servos, there is an angle value where the servo stops. You need to determine that experimentally, then write that value to the servo to make it stop.

When using continuous rotation servos, the writeMicroseconds() method is preferred to the write() method, as it gives better control over the speed of the servo. The value ranges generally from somewhere between 500 and 2500. You need to determine the minimum value that makes the servo go fastest in one direction, the maximum value that makes it go fastest in the other direction, and the value (range) that makes it stop.

PaulS, thanks very much for your help. I created a little hacked servo program to test multiple servos for their stopping point. This still does not prevent them from moving right at the beginning but it has provided greater control over the servos with respect to speed and direction.

This still does not prevent them from moving right at the beginning

Perhaps you are not aware that you can write to the servo before you attach it. Doing so will set the initial "position" of the servo when it gets attached. If you set the initial "position" to whatever the stopped value is, the servos should hold still when attached.

Actually, I did see that it was supposed to be possible but the following code does not prevent the right and left wheel motors from spinning. I do not have attach statements anywhere else in the code so I am not sure what the issue is.

void setup()
{
  rightMotor.writeMicroseconds(1390);
  rightMotor.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  rightMotor.writeMicroseconds(1390);
  leftMotor.writeMicroseconds(1400); 
  leftMotor.attach(7);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  leftMotor.writeMicroseconds(1400);   
  panMotor.attach(5); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(pos); //set PING))) pan to center "90 by default"
  Serial.begin(9600);
}

First observation is that there is no need to call writeMicroseconds() again after attach() if you've just called it before attach().

Second is that you have two different "stopped" values. Do your motors really have two different stopped settings?

A question is just how fast the motors are moving when attached this way?

Final observation is that all the comments I've made so far refer to the Servo library. I see that you are not using it. I'm not sure why. For the continuous rotation servos, at least, being able to vary the speed is inherent in the design of the motor, so using the VarSpeedServo class is unnecessary.