PROYECT VIDEO TEST AND PROGRAM HELP NEEDED

NEED HELP WITH THE CODE....... I WANT IT TO STOP WHEN SENSOR FINDS OBJECT, THEN CHOOSE ITS WAY...

#include <Servo.h>

int pin6 = 6;
int pin7 = 7;
int pin13 = 13;
int pin2 = 2; // PIN DE SEÑAL PARA EL PARALLAX
int valor = 0;
int pin2Value = 0;
int timecount = 0; // Echo counter
Servo myservo1;
Servo myservo2;
Servo myservo3;
int bumperisquierdo = 0;
int bumperderecho = 0;
int val = 0;

void setup() {

// crea objetos y los asigna a mines
myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(11);
pinMode(pin6, INPUT);
pinMode(pin7, INPUT);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,HIGH);

}
void loop()
// comienza a moverse el robot hacia adelante
{
myservo1.write(100);
myservo2.write(80);
myservo3.write(90);
digitalWrite(pin13, LOW);

parallax(); //arranca el parallax dentro del loop

//////////////////////////////////////////////////////////////////////////////////////////////////////

if(timecount < 400) //lee el parallax mientras avanza, si baja de 400
{
miraalaizquierda(); // mira a la izquierda y opera sefun funcion
}
else (timecount >400);
{
myservo1.write(100);
myservo2.write(70);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////bumpers//////

{
val = digitalRead(pin6);
if (val == LOW)

parader();
}

{
val = digitalRead(pin7);
if (val == LOW)

paraiz();
}

}

void parallax()
{
timecount = 0;
valor = 0;
pinMode(pin2, OUTPUT); // PONE AL pin2 COMO OUTPUT

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(pin2, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pin2, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pin2, LOW); // Holdoff
pinMode(pin2, INPUT); // Switch signalpin to input
valor = digitalRead(pin2); // Append signal value to val
while(valor == LOW) { // Loop until pin reads a high value
valor = digitalRead(pin2);
}
while(valor == HIGH) { // Loop until pin reads a high value
valor = digitalRead(pin2);
timecount = timecount +1; // Count echo pulse time
}
pin2Value = timecount;
{
delay(100);
}
}

void miraalaizquierda()
{

do
{
myservo3.write(10);
parallax();
myservo1.write(20);
}
while(timecount < 400);

{

// do
// {
// miraaladerecha(); // mira a la derecha y opera sefun funcion
// }
// while(timecount < 400); // si al voltear a la izquierda no ve obstaculo sale de esta condicional
//
}

}

void miraaladerecha()
{
myservo3.write(170);
{
parallax();

myservo2.write(140);

}
}

void parader()

{
myservo2.write(170);
delay(2000);
}

void paraiz()

{
myservo1.write(80);
delay(2000);
}

THIS IS WHAT I NEED,

FIRST MOVE FORWARD. SENSING DISTANCE WITH PING))

WHEN DE PING SENSES AN OBJECT IT STOPS THE ROBOT..

THEN I WANT IT TO TURN THE PING)) TO A SIDE,
SENSE DISTANCE,
IF IT SENSES NO OBJECT IT CAN TURN THE BOT TO THAT SIDE,
AND KEEP GOING FORWARD.
IF IT SENSES AN OBSTACLE
TURN PING TO THE OTHER SIDE.
SENSE DISTANCE,
IF NO OBSTACLE SENSED
IT TURNS DE BOT TO THE SIDE IT IS SENSING.
IF IT SENSES AN OBSTACLE IN BOTH SIDES, IT MOVES BACK A FEW...

MY PROBLEM IS THAT, WHEN PROGRAMING, I CAN NOT ESTABLISH A LOGIC PATTERN FOR THE ROUTINE...
TRYED MAKING FUNCTIONS, THEN MAKING "IF AND ELSE" OR "IF AND DO" CALLING A SPECIFIC FUNCTION...

IN THE VIDEO YOU CAN SEE I MANAGED TO MAKE IT TURN THE PING)) TO A SIDE, AND KEEP LOOKING AND TURNING UNTILL AVOIDED....

BUT WHEN I TRY TO MAKE IT LOOK TO THE OTHER SIDE TO COMPARE, OR, MAKE IT LOOK TO THE OTHER SIDE WHEN AN OBSTACLE IS SENSED, IT DOES NOT WORK AT ALL, KEEPS LOOPING IN A FUNNY WAY MOVING THE PING)) LIKE JITTER......

OTHER PROBLEM I HAVE.... I MADE A FUNCTION NAMED PARALLAX TO CALL THE SENSING.... IN CASES, WHEN A FUNCTION WHERE CALLED, IT TURNED OFF THE PARALLAX AND STOPPED THE PROGRAM.....

THIS IS DRIVING ME NUTTS...... :o

This bit:

 while(valor == LOW) { // Loop until pin reads a high value
    valor = digitalRead(pin2);
  }
  while(valor == HIGH) { // Loop until pin reads a high value
    valor = digitalRead(pin2);
    timecount = timecount +1;            // Count echo pulse time
  }
  pin2Value = timecount;

would be better handled by pulseIn
http://arduino.cc/en/Reference/PulseIn

Calling pin 6 "pin6" doesn't really help - give it a name that describes its function.
Also, if you declare it const int bumperSwitch = 6;, it can help avoid accidental reassignment. (I chose "bumperSwitch" as an example; I don't know what you've got on pin 6)