Trouble with Ping

Hey, Iv'e been working on a robot, I've been trying to get it to find its way through a maze with a Ping sensor. Now I only have one mounted on the front so I want to go forward check if its less than 4 cm. If it is I want it to turn right and check again. If it reads less than 4 cm again do a 180 and go strait and repeat. Heres the code I came up with:

const int leftMotor = 10;
const int rightMotor = 5;
const int pingPin = 7;
const int ledPin = 12;

void setup() {
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
}
void loop()
{

long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if(cm<4) {
digitalWrite(ledPin, HIGH);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(575);
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
}
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if(cm<4) {
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(1150);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
} else {
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

Now I keep getting errors like; redeclaration of 'long int duration' and a function definition is not allowed before '{' token. I would like some help striating out my program, oh but please don't just fix my problems tell me why you did what you did so I can learn.

Thanks
oh by the way delay(575); is the time it takes my robot to do a 90 degree turn

You'd be better off factoring the Ping code into a single function.
I must've posted that code a few times now, and it'll make debugging easier.

Also, do yourself a favour and use the auto format tool in the IDE and code tags when pasting into forum posts.

First off, simply delete the second declaration of "duration" etc.

Ok thank you. But I have a couple more questions;

  1. How do you "factor the Ping code into a single function" ?
  2. I've been getting errors like 'mircosecoundsToCenimeters' was not declared in this scope. What does that error mean and how do I fix it?

'mircosecoundsToCenimeters'

It means you mispelled "microsecondsToCentimeters"

Forget the factoring, for now.

Sorry but I misspelled it on my post but its write in my code. That error also came up with microsecondsToInches.

Also, do yourself a favour and use the auto format tool in the IDE

When you did that, you got an error.

Fix that first.

Ok I auto formatted it. Heres the new code

const int leftMotor = 10;
const int rightMotor = 5;
const int pingPin = 7;
const int ledPin = 12;

void setup() {
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if(cm<4) {
digitalWrite(ledPin, HIGH);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(575); //the time it takes for one 90 degree turn
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
}
else {
digitalWrite(ledPin, LOW);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
}
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if(cm<4) {
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(1150);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
}
else {
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
}

Ok now what? And again thanks for all the help.

Ok now what?

Now move the closing brace you added at the end to the correct place. It needs to go before the two microsecondsToX() functions, not after them.

Wait which microsecondsToBlank functions are you talking about? And which brace?

ok so robot with treads and some toy motors. I use one relay to drive each motor and have a ping sensor mounted on the front with the following code;

const int leftMotor = 10;
const int rightMotor = 9;
const int pingPin = 7;
const int ledPin = 12;

void setup() {
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
}
void loop()
{

long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if(cm<10) { //I want it to turn when it see something less than 10 centimeters
digitalWrite(ledPin, HIGH); //away
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, LOW);
delay(575); //the amount of time it takes to turn 90 degrees
digitalWrite(leftMotor, LOW);
digitalWrite (rightMotor, LOW);
}
else {
digitalWrite(ledPin, LOW);
digitalWrite(leftMotor, HIGH);
digitalWrite(rightMotor, HIGH);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

Now I've already done some trouble shooting and found out the relay on pin 9 (right motor) is not working. So I switched the relays and got the right one to turn but very slightly and not for a long time while the other one works fine. Please help me solve this problem.

Wait I've done a little more testing and realized its something with my program. Still please help.

which microsecondsToBlank functions are you talking about?

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

These functions are currently inside your loop() function. They need to be outside it. Functions can't be defined within other functions.

And which brace?

The one on the very last line. It terminates your loop() function. It needs to be moved to just above

  long microsecondsToInches(long microseconds)

See what happens when you start a new thread on the same subject?
People answer your question on the old thread, not realising you've already fixed the problem, so you've wasted their time.

DON'T CROSS-POST.

Also, after 50 posts, don't you think it is time you started using tags to enclose your code?

Well I really don't know how to use code tags and Im sorry for the cross post.

Well I really don't know how to use code tags

Before pasting code, select the icon with the # symbol on it.

http://arduino.cc/forum/index.php/topic,97455.0.html