not declared in scope problem.

I’m probably overlooking it but I’m not sure what the problem is. It says leftDistance is not declared in scope and highlights NewPing sonar(trigPin,echoPin);

And are int readPing() and bestPath() in the right spot or am I using them incorrectly? Any help or input appreciated.

I’m using a seeedstudio v2 motor shield and mega 2560.

#include <Servo.h>
#include <MotorDriver.h>
#include <NewPing.h>
int leftDistance = readLPing();
int rightDistance = readRPing();
int frontDistance = readPing();
int trigPin = 0; 
int echoPin = 1;  
const int led = 15;   
Servo servo;                 
NewPing sonar(trigPin,echoPin); 


void setup(){

  pinMode(led,OUTPUT);
  servo.attach(servopin);
  motordriver.setSpeed(80,MOTORA);
  motordriver.setSpeed(80,MOTORB);
  Serial.begin(9600);
  Serial.println("Begin");
  delay(1000);
}


void loop(){
  Serial.println("LED On");
  digitalWrite(led,HIGH);
  servo.write(90);
  delay(3000);
}


void goForward(){
  motordriver.goForward();
  frontDistance = readPing();
  if (frontDistance<15){
    motordriver.stop();
    compareDistance();
  }
  else if(frontDistance>15){
    goForward();
  }
}



void compareDistance(){  
  servo.write(20);
  leftDistance = readPing();
  delay(500);

  servo.write(160);
  rightDistance = readPing();
  delay(500);

  servo.write(90);
  delay(500);



  if (bestPath(); = leftDistance){
    motordriver.goLeft();
    delay(500);
  }

  else if (bestPath(); = rightDistance){
    motordriver.goRight();
    delay(500);
  }

  else {
    goForward();
  }
}





int readLPing() {
  unsigned int uS = sonar.ping();  
  int leftDistance = uS/US_ROUNDTRIP_CM;    
  Serial.print(leftDistance);
  Serial.println(" cm to the left");
  return leftDistance;
}

int readRPing() {
  unsigned int uS = sonar.ping();
  int rightDistance = uS/US_ROUNDTRIP_CM;
  Serial.print(rightDistance);
  Serial.println(" cm to the right");
  return rightDistance;
}

int readPing() {
  unsigned int uS = sonar.ping();
  int frontDistance = uS/US_ROUNDTRIP_CM;
  Serial.print(frontDistance);
  Serial.println(" cm in front");
  return frontDistance;
}

int bestPath(leftDistance,rightDistance){
  int longestPath;
  longestPath = max(leftDistance,rightDistance);
  return(bestPath);
}

EDIT

I changed quite a bit. No longer getting errors but this code doesn’t work.

#include <Servo.h>
#include <MotorDriver.h>
#include <NewPing.h>
int trigPin = 0; 
int echoPin = 1;  
const int led = 15;   
Servo servo;                 
NewPing sonar(trigPin,echoPin); 
int frontDistance = readPing();
int leftDistance = readLPing();
int rightDistance = readRPing();

void setup(){

  pinMode(led,OUTPUT);
  servo.attach(7);
  motordriver.setSpeed(80,MOTORA);
  motordriver.setSpeed(80,MOTORB);
  delay(1000);
}


void loop(){

  digitalWrite(led,HIGH);
  servo.write(90);
  goForward();
  frontDistance = readPing();

  if (frontDistance<20){
    motordriver.stop();
    delay(500);
    compareDistance();
  }
  else {
    goForward();
  }
}


void goForward(){
  motordriver.goForward();
  delay(1000);
}


void compareDistance(){  
  servo.write(20);
  leftDistance = readLPing();
  delay(500);

  servo.write(160);
  rightDistance = readRPing();
  delay(500);

  servo.write(90);
  delay(500);



  if (leftDistance>rightDistance){
    motordriver.goLeft();
    delay(500);
  }

  else if (leftDistance<rightDistance){
    motordriver.goRight();
    delay(500);
  }

  else {
    motordriver.goRight();
    delay(1000);
  }
}



int readLPing(){
  unsigned int uS = sonar.ping();  
  int leftDistance = uS/US_ROUNDTRIP_CM;    
  Serial.print(leftDistance);
  Serial.println(" cm to the left");
  return leftDistance;
}

int readRPing() {
  unsigned int uS = sonar.ping();
  int rightDistance = uS/US_ROUNDTRIP_CM;
  Serial.print(rightDistance);
  Serial.println(" cm to the right");
  return rightDistance;
}

int readPing() {
  unsigned int uS = sonar.ping();
  int frontDistance = uS/US_ROUNDTRIP_CM;
  Serial.print(frontDistance);
  Serial.println(" cm in front");
  return frontDistance;
}

Whenever you want help, saying “this doesn’t work” is the worst thing you can say. Since only you know what your device is doing and what your device is SUPPOSED to be doing only you can say WHAT is going wrong. If you don’t say we have little clue how to fix it except to suggest “Try doing it a different way”.

Little cars with a distance sensor on a servo are very common projects. You can probably find a sketch from one of those many projects where someone has already figured out a way to do it right.

int leftDistance = readLPing();
int rightDistance = readRPing();
int frontDistance = readPing();

You should NOT be calling these functions here.

The function names and print statements in the code make it appear that they do different things, but they don't. All three functions do exactly the same thing. Therefore, you only need ONE function.

Whenever you want help, saying “this doesn’t work” is the worst thing you can say. Since only you know what your device is doing and what your device is SUPPOSED to be doing only you can say WHAT is going wrong. If you don’t say we have little clue how to fix it except to suggest “Try doing it a different way”.

Fair enough.

Little cars with a distance sensor on a servo are very common projects. You can probably find a sketch from one of those many projects where someone has already figured out a way to do it right.

Yep that’s exactly what it is. I’m trying to build one for my little brother. And I originally based it off a tutorial I found but it didn’t work very well so I did a few more tutorials and ended up doing something of a mix. It worked well enough a while back but then it stopped working entirely for reasons unknown to me. The multimeter read 0 from the motor shield. So I assumed it wasn’t working anymore and switched it out for a seeedstudio motor shield and wrote a new code.
At first only one wheel would work, and it kept saying leftDistance not declared in scope. That’s why I put the distances up top. Now the code isn’t doing much at all. I mainly put it up here to see if one of you could tell me if I’m doing things just flat out wrong.

You should NOT be calling these functions here.

I thought so. The functions were originally called below but it kept giving me an error when compiling saying leftDistance was not declared in this scope and then highlighting a random line above setup.

The function names and print statements in the code make it appear that they do different things, but they don’t. All three functions do exactly the same thing. Therefore, you only need ONE function.

Yes I wondered about that too. My first code just had one readPing();
But it had problems so I was trying something else with this one. Will readPing() save 2 values at once? Can you compare leftDistance = readPing() and rightDistance = readPing() and choose the highest value as left or right rather than a number? Because I need the outcome to have a name, either left or right distance, in order to choose right or left. I will change it back to one readPing though. Thanks for letting me know.

Here’s my most recent code. It seems like it should work to me but the distance sensor isn’t picking anything up. it says 0cm in the serial monitor. Trying to figure it out now.

#include <Servo.h>
#include <MotorDriver.h>
#include <NewPing.h>
int trigPin = 0; 
int echoPin = 1;  
const int led = 15;   
Servo servo;                 
#define maxdistance 200
NewPing sonar(trigPin,echoPin,maxdistance); 
int frontDistance;
int leftDistance, rightDistance;

void setup(){

  pinMode(led,OUTPUT);
  servo.attach(7);
  motordriver.setSpeed(80,MOTORA);
  motordriver.setSpeed(80,MOTORB);
  Serial.begin(9600);
  delay(1000);
}


void loop(){

  digitalWrite(led,HIGH);
  servo.write(90);
  goForward();
  frontDistance = readPing();
  if (frontDistance<20){
    motordriver.stop();
    delay(500);
    compareDistance();
  }
  else {
    goForward();
  }
}


void goForward(){
  motordriver.goForward();
  delay(1000);
}


void compareDistance(){  
  servo.write(20);
  leftDistance = readPing();
  delay(500);

  servo.write(160);
  rightDistance = readPing();
  delay(500);

  servo.write(90);
  delay(500);



  if (leftDistance>rightDistance){
    motordriver.goLeft();
    delay(500);
  }

  else if (leftDistance<rightDistance){
    motordriver.goRight();
    delay(500);
  }

  else {
    motordriver.goRight();
    delay(1000);
  }
}

int readPing() {
  unsigned int uS = sonar.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  Serial.print(cm);
  Serial.println(" cm");
  return cm;
}

Will readPing() save 2 values at once?

What two values? The function gets the distance between the sensor and the nearest (hard) object in front of the sensor? What other value do you want it to “save”. I’ll presume you actually mean return, since the whole point of most functions is that they do not save data.

Can you compare leftDistance = readPing() and rightDistance = readPing() and choose the highest value as left or right rather than a number?

The question shows a lack of understanding. When you call readPing(), it returns a value. You are assigning that value to leftDistance. You call the function again, after moving the servo, and assign the return value to rightDistance.

The last part of your question is gibberish.

Your question should be “Can you compare leftDistance and rightDistance and choose the highest value?”. I think that you can see that the answer is yes.

Here’s my most recent code. It seems like it should work to me but the distance sensor isn’t picking anything up. it says 0cm in the serial monitor. Trying to figure it out now.

int trigPin = 0; 
int echoPin = 1;

Are you serious? You are trying to use the hardware serial pins for Serial AND for the pin sensor. Not a snowball’s chance in hell that that is going to work.

The question shows a lack of understanding.

Give the man a prize!!! Ya think?! Why would I ask if I knew? Jesus christ dude this is what I mean by disrespectful answers. Is it so hard to answer nicely?

When you call readPing(), it returns a value. You are assigning that value to leftDistance. You call the function again, after moving the servo, and assign the return value to rightDistance.

The last part of your question is gibberish.

Your question should be “Can you compare leftDistance and rightDistance and choose the highest value?”. I think that you can see that the answer is yes.

No that isn’t my question, Einstein. My question is, when it returns left, obviously that is a number that it saves. When it returns right, is left still saved to be compared or does it replace cm with the new distance? I want to be able to compare the two distances as left or right, not as a a number, because that number will change. I don’t think you get it.
When it chooses left or right it says if(leftDistance<rightDistance){} therefor I’m asking if those will be saved as they are read in readPing() or will it only save one readPing process at one time.

Are you serious? You are trying to use the hardware serial pins for Serial AND for the pin sensor. Not a snowball’s chance in hell that that is going to work.

I’m sorry is this where you post to talk about how flawless and awesome your code is? I must be in the wrong section again.
They use to be analog 8 and 9 but I switched out my arduino and rewired everything so I put them down to 0 and 1. I don’t know anything about hardware serial pins.I’ll check into that.

Again, that’s why I ask.

My question is, when it returns left, obviously that is a number that it saves.

In a memory location associated with the name leftDistance.

When it returns right, is left still saved to be compared or does it replace cm with the new distance?

leftDistance is unaffected if you assign the return value to a memory location associated with the name rightDistance.

I want to be able to compare the two distances as left or right, not as a a number, because that number will change.

The two numbers are saved in different memory locations.

I don't think you get it.

Well, you are in the minority.

therefor I'm asking if those will be saved as they are read in readPing() or will it only save one readPing process at one time.

It isn't an either/or question. You call the readPing() function; It returns a value. You save it somewhere. You call the function again. It returns a value. You save it somewhere else.

If Joe gives you a phone number, and, later, Mary gives you a phone number, and you dial the first phone number, who are you going to talk to? Why would you assume that computers can't keep discrete values separate?

I'm sorry is this where you post to talk about how flawless and awesome your code is?

Yes. Have you got a couple of weeks?

I don't know anything about hardware serial pins

I can believe that.

leftDistance is unaffected if you assign the return value to a memory location associated with the name rightDistance.

That's what I'm asking. -_- Thanks a lot.

I can believe that.

Wow. Cool guy alert.

Edit

Forget it. Thanks for the "help." I'm out. I'll figure it out myself. Obviously this is was waste of time.