Weird "not declared" error, Please help

Hey everyone,
I have a robotic platform with a servo with a ping sensor on top of the servo, i want the servo to look right, measure the centimeters and also do that with left and forward and then it compares what is the biggest length and drive that way, but i have a problem.

This is the full code:

//Motorshield uses digital pins 11 and 3
//Receiver uses digital pin 0 (RX)
//PING))) sensor uses digital pin 7
//ping servo right = 0 degrees middle = 85 degrees left = 171 degrees
#include <AFMotor.h>
#include <Servo.h> 
 
Servo pingservo;  // create servo object to control a servo 
AF_DCMotor M_left(1, MOTOR12_64KHZ); // driver-side motor
AF_DCMotor M_right(2, MOTOR12_64KHZ); // passenger-side motor
const int pingPin = 2;
int message = 0; // This will hold one byte of the serial message
#define velocity (80) /* how fast the wheels spin */
unsigned long dright = 0;
unsigned long dfront = 0;
unsigned long dleft = 0;
void loop()
{
autosteer();
}

void forward()
{
M_left.run(FORWARD);
M_right.run(FORWARD); 
}

void autosteer()
{
  
  pingservo.write(0); // make the servo turn the ping sensor right
  delay(400); // wait for the servo to get there
  ping(); // measure centimeters
  dright = cm; //store value, earlier in the code declared with: unsigned long dright = 0; 
  // now here's the error: it says cm is not declared in this scope
  pingservo.write(85);
  delay(400);
  ping();
  dfront = cm;
  pingservo.write(171);
  delay(400);
  ping();
  dleft = cm;
  pingservo.write(85);
  delay(400);
  
  if (dright > dfront && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(BACKWARD); 
  }
  
    if (dleft > dfront && dright)
  {
    M_left.run(BACKWARD);
    M_right.run(FORWARD); 
  }
    if (dfront > dright && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(FORWARD); 
  }
}

void ping()
{
  
  

    long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
 
  //Serial.print(inches);
 // Serial.print("in, ");
 //writeUInt(cm);
 
//  Serial.print("cm");
//  Serial.println();
  




 
}

Now i want to know 3 things:

  1. Am i even storing the values with unsigned long dleft = 0; ?

  2. How can I fix the "not declared" error?

  3. since i'm pretty much a noob, i never found the definition of "scope", what is it?

dright = cm; //store value, earlier in the code declared with: unsigned long dleft = 0;

right != left

Would have been simpler if you'd posted all the code.

Scope is to do with the visibility of entities to other parts of the program/
Braces {} define code blocks.
If you define a variable within the braces, then the variable is only visible within the code block.
Its scope is said to be local to that code block.

If you declare the variable outside of all code blocks, like usually before "setup", then it is said to have global scope and is visible within all code blocks.

Thanks for that info,
i have edited my post and added the full code.

Your problem is that "cm" is declared inside "ping".
Code in "autosteer" can't see 'into' "ping", so "cm" is not declared in scope when you try to assign it to "dright".

I have now changed the whole code to:

//robot car v1.0 by Niek Blankers
//Motorshield uses digital pins 11 and 3
//Receiver uses digital pin 0 (RX)
//PING))) sensor uses digital pin 7
//ping servo right = 0 degrees middle = 85 degrees left = 171 degrees
#include <AFMotor.h>
#include <Servo.h> 
 
Servo pingservo;  // create servo object to control a servo 
AF_DCMotor M_left(1, MOTOR12_64KHZ); // driver-side motor
AF_DCMotor M_right(2, MOTOR12_64KHZ); // passenger-side motor
const int pingPin = 2;
int message = 0; // This will hold one byte of the serial message
#define velocity (80) /* how fast the wheels spin */
unsigned long dright = 0;
unsigned long dfront = 0;
unsigned long dleft = 0;
void setup()
{
   pingservo.attach(5);
   pingservo.write(0);
   delay(400);
   pingservo.write(85);
   delay(400);
   pingservo.write(171);
   delay(400);
   pingservo.write(85);
   M_left.setSpeed(velocity);
  M_right.setSpeed(velocity);
  Serial.begin(1200);  // Hardware supports up to 2400, but 1200 gives longer range
  delay(500);
}




void loop()
{
autosteer();
}

void forward()
{
M_left.run(FORWARD);
M_right.run(FORWARD); 
}

void autosteer()
{
  long duration, inches, cm;
  pingservo.write(0);
  delay(400);
  ping();
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  dright = cm; 
  pingservo.write(85);
  delay(400);
  ping();
  dfront = cm;
  pingservo.write(171);
  delay(400);
  ping();
  dleft = cm;
  pingservo.write(85);
  delay(400);
  
  if (dright > dfront && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(BACKWARD); 
  }
  
    if (dleft > dfront && dright)
  {
    M_left.run(BACKWARD);
    M_right.run(FORWARD); 
  }
    if (dfront > dright && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(FORWARD); 
  }
}

void ping()
{
  
  

    long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
 // duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
 // inches = microsecondsToInches(duration);
 // cm = microsecondsToCentimeters(duration);
  
 
  //Serial.print(inches);
 // Serial.print("in, ");
 //writeUInt(cm);
 
//  Serial.print("cm");
//  Serial.println();
  




 
}  
  
  
  




long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}




// left.run(BACKWARD);
//right.run(FORWARD);

Now it uploads the code and the servo keeps turning but it doesn't drive,
my guess is that the cm values are not stored, how do i store them correctly?

The "duration" "inches" and "cm" you've put into "autosteer" can't see the values in "ping".
If you want to work like this (and it'll do for no, but not recommended) is to put them solely at global scope.

//robot car v1.0 by Niek Blankers
//Motorshield uses digital pins 11 and 3
//Receiver uses digital pin 0 (RX)
//PING))) sensor uses digital pin 7
//ping servo right = 0 degrees middle = 85 degrees left = 171 degrees
#include <AFMotor.h>
#include <Servo.h> 

Servo pingservo;  // create servo object to control a servo 
AF_DCMotor M_left(1, MOTOR12_64KHZ); // driver-side motor
AF_DCMotor M_right(2, MOTOR12_64KHZ); // passenger-side motor
const int pingPin = 2;
int message = 0; // This will hold one byte of the serial message
#define velocity (80) /* how fast the wheels spin */
unsigned long dright = 0;
unsigned long dfront = 0;
unsigned long dleft = 0;
long duration, inches, cm;

void setup()
{
  pingservo.attach(5);
  pingservo.write(0);
  delay(400);
  pingservo.write(85);
  delay(400);
  pingservo.write(171);
  delay(400);
  pingservo.write(85);
  M_left.setSpeed(velocity);
  M_right.setSpeed(velocity);
  Serial.begin(1200);  // Hardware supports up to 2400, but 1200 gives longer range
  delay(500);
}




void loop()
{
  autosteer();
}

void forward()
{
  M_left.run(FORWARD);
  M_right.run(FORWARD); 
}

void autosteer()
{
  pingservo.write(0);
  delay(400);
  ping();
  duration = pulseIn(pingPin, HIGH);

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

  dright = cm; 
  pingservo.write(85);
  delay(400);
  ping();
  dfront = cm;
  pingservo.write(171);
  delay(400);
  ping();
  dleft = cm;
  pingservo.write(85);
  delay(400);

  if (dright > dfront && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(BACKWARD); 
  }

  if (dleft > dfront && dright)
  {
    M_left.run(BACKWARD);
    M_right.run(FORWARD); 
  }
  if (dfront > dright && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(FORWARD); 
  }
}

void ping()
{



  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  // duration = pulseIn(pingPin, HIGH);

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


  //Serial.print(inches);
  // Serial.print("in, ");
  //writeUInt(cm);

  //  Serial.print("cm");
  //  Serial.println();






}  







long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Thank you,
Now i only need to know how to save the cm values for each servo positions.

Does someone know how to do this?

Now i only need to know how to save the cm values for each servo positions

You need to explain what you mean by that.
Do you mean you need to store a value for each position of the servo?
Doesn't dleft, dright, dcentre do this?

Have a look at arrays:
http://arduino.cc/en/Reference/Array

Edit: Just re-read your sketch. You need to save the value after every time you call ping(). Currently, you're just saving the value you calculated and stored to dright.

You're calculating "inches" - why?

Uncompiled, untested (obviously):

//robot car v1.0 by Niek Blankers
//Motorshield uses digital pins 11 and 3
//Receiver uses digital pin 0 (RX)
//PING))) sensor uses digital pin 7
//ping servo right = 0 degrees middle = 85 degrees left = 171 degrees
#include <AFMotor.h>
#include <Servo.h> 

Servo pingservo;  // create servo object to control a servo 
AF_DCMotor M_left(1, MOTOR12_64KHZ); // driver-side motor
AF_DCMotor M_right(2, MOTOR12_64KHZ); // passenger-side motor
const int pingPin = 2;
int message = 0; // This will hold one byte of the serial message
#define velocity (80) /* how fast the wheels spin */
unsigned long dright = 0;
unsigned long dfront = 0;
unsigned long dleft = 0;

void setup()
{
  pingservo.attach(5);
  pingservo.write(0);
  delay(400);
  pingservo.write(85);
  delay(400);
  pingservo.write(171);
  delay(400);
  pingservo.write(85);
  M_left.setSpeed(velocity);
  M_right.setSpeed(velocity);
  Serial.begin(1200);  // Hardware supports up to 2400, but 1200 gives longer range
  delay(500);
}

void loop()
{
  autosteer();
}

void forward()
{
  M_left.run(FORWARD);
  M_right.run(FORWARD); 
}

void autosteer()
{
  pingservo.write(0);
  delay(400);
  dright = microsecondsToCentimeters(ping()); 

  pingservo.write(85);
  delay(400);
  dfront = microsecondsToCentimeters(ping()); 

  pingservo.write(171);
  delay(400);
  dleft = microsecondsToCentimeters(ping()); 

  pingservo.write(85);
  delay(400);

  if (dright > dfront && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(BACKWARD); 
  }

  if (dleft > dfront && dright)
  {
    M_left.run(BACKWARD);
    M_right.run(FORWARD); 
  }
  if (dfront > dright && dleft)
  {
    M_left.run(FORWARD);
    M_right.run(FORWARD); 
  }
}

unsigned long ping()
{
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH); 
}  

unsigned long microsecondsToCentimeters(unsigned long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Thanks, it works really good now!