Object Avoidance with Ping))) sensor

So I am working a arduino robot. I have a ping))) sensor and an arduino. I am trying to make code that will avoid objects without a servo because I don't have one. The robot is a tank kit. You can find it here: http://www.robotshop.com/dfrobotshop-rover-tracked-robot-basic-kit.html
Basically, the program that I am working on is going to find an object that is 50 cm away then the tank would turn to the right, read the sensor and set that value to integer ping1, then the tank would turn to the left of the object and read the sensor and set that value as integer ping2.

const int ping = 4;
int ping1
int ping2



void setup()
{ 
 digitalWrite(ping, OUTPUT);
}


void loop()
{
   long duration, inches, cm;
   pinMode(ping,OUTPUT)
   digitalWrite(ping, LOW); // Send low pulse 
   delayMicroseconds(2);  // Wait for 2 microseconds 
   digitalWrite(ping, HIGH); // Send high pulse 
   delayMicroseconds(5); // Wait for 5 microseconds 
   digitalWrite(ping, LOW); // Holdoff 
   pinMode(ping, INPUT); // Switch signalpin to input 
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
  //Compare Values
 
  if (ping > 50)
 {
  
  int leftspeed = 255;  //255 is maximum speed 
  int rightspeed = 255; 
  
   {  
    { 
     left (leftspeed,rightspeed); 
     
     //send out high/low signals
    digitalWrite(ping1, LOW); // Send low pulse 
    delayMicroseconds(2); // Wait for 2 microseconds 
    digitalWrite(ping1, HIGH); // Send high pulse 
    delayMicroseconds(5); // Wait for 5 microseconds 
    digitalWrite(ping, LOW); // Holdof
    pinMode(ping, INPUT); // Switch signalpin to input 
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration); 
    }
    
      
   { 
     right (leftspeed,rightspeed); 
     right (leftspeed,rightspeed); 
       //send out high/low signals
    digitalWrite(ping2, LOW); // Send low pulse 
    delayMicroseconds(2); // Wait for 2 microseconds 
    digitalWrite(ping2, HIGH); // Send high pulse 
    delayMicroseconds(5); // Wait for 5 microseconds 
    digitalWrite(ping, LOW); // Holdof
    pinMode(ping, INPUT); // Switch signalpin to input 
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);
   }
   
    if (ping1 > ping2)
     {
      right (leftspeed,rightspeed); 
     }
    if (ping2 > ping1)
     {
       left (leftspeed,rightspeed); 
     } 
    } 
  }
}

I am looking at more posts/tutorials about ping sensors. Sorry, I guess I'm just confused by some of the basics.
Thank you for reading my post and any help, guidance or code will be greatly appreciated.

I am trying to make code that will avoid objects without a servo

I don't think the Ping has good enough resoultion to decide if an object has a servo or not.

if (ping > 50);

ping has a value of 4, so it is never going to be greater than 50.
The semicolon says you're not going to do anything, even if ping was greater than 50.

You don't appear to have posted any code that actually reads a value from the ping sensor.

I know you know this, but your code can't actually compile - fails on the first line.

There are quite a few example codes for the ping that do work (even if they aren't very well written) - I suggest you study a few and work from there.

Do you know of any links to some code? I know that the code doesn't work, I just wanted some advice/help on programming.

Thanks for the reply

AWOL:

I am trying to make code that will avoid objects without a servo

I don't think the Ping has good enough resoultion to decide if an object has a servo or not.

No, you miss understood what I was trying to say. I meant that I am trying to ovoid objects, yet the ping sensor isn't attached to a servo, for rotation.

 digitalWrite(ping, LOW); // Send low pulse 
   delayMicroseconds(2);  // Wait for 2 microseconds 
   digitalWrite(ping, HIGH); // Send high pulse 
   delayMicroseconds(5); // Wait for 5 microseconds 
   digitalWrite(ping, LOW); // Holdoff 
   pinMode(ping, INPUT); // Switch signalpin to input 
   
  
  //Compare Values
 
  if (ping > 50)
 {

You're confusing the pin the device is attached to with the value that it might return, had you bothered to read it.
http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

if (ping1 > ping2);

See my comments earlier.

I took AWOL's advice and looked at an example of robot with a ping sensor. I changed my code around.

Yes, but you need to take some more.
"ping" has a value of 4 - it is never going to be > 50

Hello i am also working on the same type of project with the same base from robotshop and an arduino uno i have run into alot of problems and i need help fast i am trying to build a robot that will race down a hallway with all left turns i have two ping sensors and i can not figure out the code if anyone has any good suggestions please post them as soon as possible or email me at delectronics01@gmail.com

if anyone has any good suggestions please post them as soon as possible

My suggestion is: Post your code, tell us about your robot (like how and where the Pings are positioned)
Without this, we can't help you with your assignment/project.

Maybe exercise those shift and punctuation keys next time?

Ok but don't laugh i have no experience with code

#define ping2pin      10      // left sensor
  #define pingPin       9      // Ping ultrasonic sensor on pin D9.
  int E1 = 6; //M1 Speed Control 
int E2 = 5; //M2 Speed Control 
int M1 = 8; //M1 Direction Control 
int M2 = 7; //M2 Direction Control 
 
  #define YBOUNDARY     20
  #define XBOUNDARY     20    // (cm) Avoid objects closer than 20cm.
  #define INTERVAL     25      // (ms) Interval between distance readings.

// setup
// Set motor pins as OUTPUTS, initialize Serial

void setup()                                                 
{
  int i; 
  for(i=5;i<=8;i++) 
  pinMode(i, OUTPUT); 
  Serial.begin(9600);
  
}


// Main program
// Roam around while avoiding objects.
// 
// Set motors to move forward,
// Take distance readings over and over, 
// as long as no objects are too close (determined by BOUNDARY).
// If object is too close, avoid it -- back up and turn.
// Repeat.
void loop() 
{

                     

  long xdistance;                    // Distance reading from rangefinder.
  long ydistance;
  forward();                        // Robot moves forward continuously.
  do 
  {
    int leftDir1 = 255; //255 is maximum speed 
    int righdir1 = 255; 
    analogWrite (E1,225); 
    digitalWrite(M1,LOW); 
    analogWrite (E2,255); 
    digitalWrite(M2,LOW); 
 
    xdistance = readxDistance();      // Take a distance reading.
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);
    ydistance = readydistance();
    Serial.println(ydistance);
    delay(INTERVAL);
  }
  while(xdistance >= XBOUNDARY);while(ydistance >= YBOUNDARY);      // Loop while no objects close-by.
  
  // Robot has sensed a nearby object and exited the while loop.
  // Take evasive action to avoid object.       
  LEFTTurn(1000);                   // Turn LEFT 1s.
  do
  {
    int leftDirl = 2515;
    int rightdirl = 255;
    analogWrite (E1,255);
    digitalWrite (M1,LOW);
    analogWrite (E2,255);
    digitalWrite (M2,HIGH);
    
     xdistance = readxDistance();      // Take a distance reading.
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);
    ydistance = readydistance();
    Serial.println(ydistance);
    delay(INTERVAL);
  }
  while(xdistance <= XBOUNDARY);while(ydistance <= YBOUNDARY);      // Loop while no objects close-by.
  
  
 } // end Main program


// forward
//
// Move robot forward by setting both wheels forward.
// Will persist until something else changes the
// motors' directions.

void forward()
{
  digitalWrite(M1, HIGH);      // Left motor forward.             
  digitalWrite(E1, LOW);
  digitalWrite(M2, HIGH);     // Right motor forward.
  digitalWrite(E2, LOW);
}  


// backward
//
// Move robot backward by setting both wheels backward.
// Will persist until something else changes the
// motors' directions.

void backward()
{
  digitalWrite(M1, HIGH);     // Left motor backward.
  digitalWrite(E1, LOW);
  digitalWrite(M2, HIGH);    // Right motor backward.
  digitalWrite(E2, LOW);
}


// rightTurn
//
// Turn robot to right by moving wheels in opposite directions.
// Amount of turning is determined by duration argument (ms).

void LEFTTurn(int duration)
{
  digitalWrite(M1, HIGH);     // RIGHT motor backward.
  digitalWrite(E1, LOW);
  digitalWrite(M2, LOW);     // LEFT motor forward.
  digitalWrite(E2, HIGH);
  delay(duration);                  // Turning time (ms).
}


// readDistance
// Take a distance reading from Ping ultrasonic rangefinder.
// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor
 
long readxDistance()
{
  long duration, inches, cm;

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We 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 an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

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 traveled.
  return microseconds / 29 / 2;
}
long readydistance()
{
  long duration, inches, cm;  //left sensor variable

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(ping2pin, OUTPUT);
  digitalWrite(ping2pin, LOW);
  delayMicroseconds(2);
  digitalWrite(ping2pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ping2pin, 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 an object.
  pinMode(ping2pin, INPUT);
  duration = pulseIn(ping2pin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

I am rely lost i am trying to make it go straight while the front and left side ping are with in 2 feet and then i want it to turn left 90 degrees when the front ping is less than 2 feet and the left ping is more than 2 feet if i can get there that would be great

OK, now go back to your last post, click "modify", highlight all the code, then click the # icon on the editor's toolbar.

It seems to me that "backward" is the same as "forward".
How much testing have you done, and when is our project due to be submitted?

sorry never done this before

It also seems that much of readxdistance is repeated in readydistance.

yes i am trying to use two ping sensors instead of a sweep survo

OK, that can be fixed, but why is backwards the same as forwards?

 while(xdistance >= XBOUNDARY);while(ydistance >= YBOUNDARY);

I don't think you want that - have you considered how xdistance and ydistance cannot change?
I also think you want a single "while", with an &&, not consecutive whiles.

I was testing it by running the bot and the motors were acting funny so i changed things to see how it would act.

and the motors were acting funny

Jerry Lewis "funny", or RIcky Gervais "funny"?

well they were going forward great but the min it got to the BOUNDARY it stopped and just and sat there

i seem to be getting good readings from both sensors when i run the serial monitor i get good readings from both sensors.

You need to post your code as it is now.

this one seems to be the best the only problem is that it moves very slow

#define ping2pin      8      // left sensor
#define pingPin       9      // forward Ping ultrasonic sensor on pin D9.
#define leftDir1      7      // Left motor direction 1, to AIn1.+
#define leftDir2      6      // Left motor direction 2, to AIn2.-
#define rightDir1     5      // Right motor direction 1, to BIn1.+
#define rightDir2     4      // Right motor direction 2, to BIn2.-

#define BOUNDARY     20      // (cm) Avoid objects closer than 20cm.
#define INTERVAL     25      // (ms) Interval between distance readings.

// setup
// Set motor pins as OUTPUTS, initialize Serial

void setup()                                                 
{
  pinMode(leftDir1, OUTPUT);        // Set motor direction pins as outputs.
  pinMode(leftDir2, OUTPUT);      
  pinMode(rightDir1, OUTPUT);      
  pinMode(rightDir2, OUTPUT); 
  Serial.begin(9600);
}


// Main program
// Roam around while avoiding objects.
// 
// Set motors to move forward,
// Take distance readings over and over, 
// as long as no objects are too close (determined by BOUNDARY).
// If object is too close, avoid it -- back up and turn.
// Repeat.

void loop()                     
{
  long xdistance;                    // Distance reading from rangefinder.
  long ydistance;
  forward();                        // Robot moves forward continuously.
  do 
  {
    xdistance = readxDistance();      // Take a distance reading.
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    ydistance = readydistance();
    Serial.println(ydistance);
    delay(INTERVAL);
  }
  while(xdistance >= BOUNDARY);while(ydistance >= BOUNDARY);      // Loop while no objects close-by.

  // Robot has sensed a nearby object and exited the while loop.
  // Take evasive action to avoid object.                        
  leftTurn(300);                   // Turn left 300ms.

} // end Main program


// forward
//
// Move robot forward by setting both wheels forward.
// Will persist until something else changes the
// motors' directions.

void forward()
{
  digitalWrite(leftDir1, LOW);      // Left motor forward.             
  digitalWrite(leftDir2, HIGH);
  digitalWrite(rightDir1, LOW);     // Right motor forward.
  digitalWrite(rightDir2, HIGH);
}  


// backward
//
// Move robot backward by setting both wheels backward.
// Will persist until something else changes the
// motors' directions.

void backward()
{
  digitalWrite(leftDir1, HIGH);     // Left motor backward.
  digitalWrite(leftDir2, LOW);
  digitalWrite(rightDir1, HIGH);    // Right motor backward.
  digitalWrite(rightDir2, LOW);
}


// leftTurn
//
// Turn robot to left by moving wheels in opposite directions.
// Amount of turning is determined by duration argument (ms).

void leftTurn(int duration)
{
  digitalWrite(leftDir1, HIGH);     // Left motor backward.
  digitalWrite(leftDir2, LOW);
  digitalWrite(rightDir1, LOW);     // Right motor forward.
  digitalWrite(rightDir2, HIGH);
  delay(duration);                  // Turning time (ms).
}


// readDistance
// Take a distance reading from Ping ultrasonic rangefinder.
// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

long readxDistance()
{
  long duration, inches, cm;

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We 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 an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

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 traveled.
  return microseconds / 29 / 2;
}
long readydistance()
{
  long duration, inches, cm;  //left sensor variable

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(ping2pin, OUTPUT);
  digitalWrite(ping2pin, LOW);
  delayMicroseconds(2);
  digitalWrite(ping2pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ping2pin, 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 an object.
  pinMode(ping2pin, INPUT);
  duration = pulseIn(ping2pin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

As I pointed out earlier, these:

while(xdistance >= BOUNDARY);while(ydistance >= BOUNDARY);

are pretty much one-way tickets.
There is no way xdistance gets updated, so if it ends up >= BOUNDARY just once, it stays here.