Hello Forum need help with if, else

hello all
I am new to coding and could use some help with if,else.
i have created and attached my first program which is for a hacked rc car with a ping sensor front and back.
in particular, my loop does not seem to "loop".
it only seems to run through once, it either senses nothing and driveForward, or senses something and driveBackwardTurn.
it loops driveForward, but once it gets to driveBackwardTurn it does nothing else without restart.
any advice or constructive criticism welcome.

thanks!
retro900

here is full sketch.
https://www.dropbox.com/s/eozlf247mur9w9u/rover3.ino

here is a snippet of void loop, i also tried to achieve this with void scanAvoid() found in full sketch.

void loop ()

{
int distanceInchesFront = scanFront();

if (distanceInchesFront > 1){
if(distanceInchesFront > objectMid){
driveForward();
}else{
driveBackwardTurnRight();
}
}
}

it only seems to run through once

Your serial debug prints tell you that?

the car drives forward till it see's something, backs up a bit and then that is all that happens.

A number of people here me included will not access dropbox to see your code. You can attach it by going to Additional Options below the typing area though.

My advice is to put some Serial.prints in the different "legs" of the ifs, that way you can see where the logic leads, eg:

void loop ()

{
   int distanceInchesFront = scanFront();

   if (distanceInchesFront > 1){
   Serial.println("leg A");
   if(distanceInchesFront > objectMid){
 Serial.println("leg B");
         driveForward();              
   }else{
 Serial.println("Leg C");
          driveBackwardTurnRight();
             }
 }
}

OP: You might consider putting each { on its own line, and using Tools + Auto Format. If you did, you see that you have two if statements, and one else statement. Perhaps the else is hung off the wrong if statement. Perhaps you need two else statements.

You really should NOT have so much code that you can't post it here, at this time. You appear to have an issue involving one ping type sensor and some motors. That doesn't require more than 50 lines of code to test.

void scanAvoid()
{
  scanFront();
  scanRear();
  if((distanceInchesFront < objectClose)and(distanceInchesRear > objectClose)) 
  {
    void driveBackwardTurnRight();
  }
  if ((distanceInchesFront > objectClose)and(distanceInchesRear < objectClose)) 
  {
    void driveForward();
  }
  if ((distanceInchesFront < objectClose)and(distanceInchesRear < objectClose))
  { 
    void lightStrobeSound();
  }
}

I am surprised that this even compiles because you appear (to me) to have function declarations in the middle of another function. The use of 'and' instead of && is also unusual but not necessarily wrong.

I am surprised that this even compiles because you appear (to me) to have function declarations in the middle of another function.

No, just a function prototype.
Pointless, but not syntactically incorrect.

OK, a function prototype, but what it does not do is call the function and the code is littered with examples of the same thing.

I simplified the code to test this

when I eliminate driveForward(); and driveBackwardTurnRight(); from the void loop,
everything looks as expected in the monitor.

but when i include the driving code, it locks up, stops pinging and just repeats driveBackwardRight();

thanks for the help so far, i've learned alot already.

#include <Servo.h>

Servo steerServo;


int centerPos = 90;
int halfRight = 45;
int right =0;
int halfLeft = 135;
int left = 180;

//pin setups
const int frontPing = 4;
const int motorDir = 12;
const int motorPwm = 3;
const int motorBrake = 9;

//trigger distances for pings on 4 and 5
const int objectFar = 60;
const int objectMid = 45;
const int objectClose = 20;

const int pause1 = 2000;
const int pause2 = 1000;//unused
const int pause3 = 500;//unused
const int drivePulse = 500;


//ping variables
long int 
durationFront,
durationRear,
distanceInchesFront,
distanceInchesRear,
distanceCmFront, 
distanceCmRear;

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

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



//movements-----------------------------------------------------------
void driveForward(){
  steerServo.write(centerPos);
  digitalWrite(motorDir, LOW);
  digitalWrite(motorPwm, HIGH);
  delay(200);
  digitalWrite(motorPwm, LOW);
}


void driveBackwardTurnRight(){
  steerServo.write(right);
  digitalWrite(motorDir, HIGH);
  digitalWrite(motorPwm, HIGH);
  delay(200);
  digitalWrite(motorPwm, LOW);
}

void driveForwardLeft(){
  steerServo.write(left);
  digitalWrite(motorDir, LOW);
  digitalWrite(motorPwm, HIGH);
  delay(1000);
  digitalWrite(motorPwm, LOW);
  steerServo.write(centerPos);
}

void driveForwardRight(){
  steerServo.write(right);
  digitalWrite(motorDir, LOW);
  digitalWrite(motorPwm, HIGH);
  delay(drivePulse);
  digitalWrite(motorPwm, LOW);
  steerServo.write(centerPos);
}


void setup ()

{
  Serial.begin(9600);
  steerServo.attach(6);

}

void loop ()

{
  delay(400);
  int distanceInchesFront = scanFront();
  if(distanceInchesFront > objectClose)
  {
    driveForward(); 
    Serial.println("forward");
  }
  if(distanceInchesFront <= objectClose)
  { 
    driveBackwardTurnRight();
     Serial.println("backward");
  }
}




long scanFront(){
  pinMode(frontPing, OUTPUT);
  digitalWrite(frontPing, LOW);
  delayMicroseconds(2);
  digitalWrite(frontPing, HIGH);
  delayMicroseconds(5);
  digitalWrite(frontPing, LOW);

  pinMode(frontPing, INPUT);
  durationFront = pulseIn(frontPing, HIGH);

  return distanceInchesFront = microsecondsToInches(durationFront);
  distanceCmFront = microsecondsToCentimeters(durationFront);
}

Retro900:
but when i include the driving code, it locks up, stops pinging and just repeats driveBackwardRight();

  if(distanceInchesFront <= objectClose)

{
    driveBackwardTurnRight();
     Serial.println("backward");
  }

The code above is, IMO, the next place to look. If driveBackwardTurnRight() keeps being called, then that must be because distanceInchesFont keeps being <= objectClose. Next step I would recommend is to add serial.print() lines to output the value of distanceInchesFront and objectClose each loop(). Confirm that, as the vehicle backs up, the values change as expected.