Parallax BING and robot car

Hello,

I'm trying to make a robot car with a Bing sensor. I want the car to go into reverse when it gets within a certain distance from a wall (to avoid collision), let's say about 6 inches (dimensions are arbitrary, I use 2000 for the Bing output). When it goes backward I turn the wheels to change direction, and then when it gets about a foot from the wall or obstacle it starts going forward again. I control the direction of the motor using an integer variable: forward. When forward = 0, the car goes backwards. When forward = 1, it goes forward. Simple. So I defined three "zones" depending on the distance to the wall or any obstacle, distance which is measured by the ping. Let's call it "measure". When measure is > 1 foot: forward = 1 no matter what. When measure < 6 inches, forward = 0 no matter what. For the zone in between, things get sort of hairy programming-wise (I'm not very good!) and depend on the previous value of forward (so the trick is to put the car on the ground a good distance from the wall so it doesn't fall in that middle zone where forward is not absolutely defined): if forward was equal to zero (i.e. the car was going backwards), then it needs to stay 0 so that the car keeps going backwards. If it was 1, then it needs to stay equal to 1.

Code is below. So say the car starts a good distance from a wall and gets closer: forward should be equal to 1, then when measure goes below 2000 forward is equal to 0 and the car start going backwards. When the distance increases again, forward should return to 1 when measure reaches 3000.

Instead what happens is that when the car start going backwards, forward returns to 1 as soon as measure reaches 2000. This doesn't seem correct and it's a problem because then the car gets stuck: reaches 2000 then starts going backwards, but as soon as it starts going backwards and goes over 2000 forward is equal to 1 again and the car resumes going forward...

Can somebody point the mistakes to me? I'm stumped on this one and I'm tired... :~ Any help would be greatly appreciated. This is my very first project with the Arduino. It involves an H bridge and works OK except for that part... Direction of the car is controlled by 2 LDRs. It just turns towards wherever it gets more light (looking for windows... :D).

/*
  Robot car
 */

const int enablePin = 4;
const int turnLeftPin = 2;
const int turnRightPin = 3;
const int threshold = 5;
const int enablePinMain = 7;
const int GoBack = 9;
const int GoForward = 8;
const int pingPin = 12;

void setup() {
  Serial.begin(9600);
  pinMode(turnLeftPin, OUTPUT);
  pinMode(turnRightPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(enablePinMain, OUTPUT);
  pinMode(GoForward, OUTPUT);
  pinMode(GoBack, OUTPUT);
}

void loop() {
// Sets both motors to "enable" and start the motor going forward  
  digitalWrite(enablePin, HIGH);
  digitalWrite(enablePinMain, HIGH);
  long measure;
  int forward;
  int sensorDiff;

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

  delay(100);

  if (measure >= 3000) {
   forward = 1;}
  
  if (measure <= 2000) {
    forward = 0;}

  if (measure < 3000 and measure > 2000) {
    if (forward == 1) {
      forward = 1;}
    else {
      forward = 0;}
  }

  Serial.print(measure);
  Serial.print(" = PING output -  ");
  Serial.print(forward);
  Serial.print(" = Direction ");
  Serial.println();

  if ( forward == 0) {
    digitalWrite(GoForward, LOW);
    digitalWrite(GoBack, HIGH);
    digitalWrite(turnLeftPin, HIGH);
    digitalWrite(turnRightPin, LOW);}

// Motors and LDRs section
  int sensorValue0 = analogRead(A0);
  int sensorValue1 = analogRead(A1);
  sensorDiff = sensorValue0 - sensorValue1;
  
  if (sensorDiff > threshold and forward == 1) {
    digitalWrite(GoForward, HIGH);
    digitalWrite(GoBack, LOW);
    digitalWrite(turnLeftPin, HIGH);
    digitalWrite(turnRightPin, LOW);}
    
  else if (sensorDiff < -threshold and forward == 1) {
    digitalWrite(GoForward, HIGH);
    digitalWrite(GoBack, LOW);
    digitalWrite(turnLeftPin, LOW);
    digitalWrite(turnRightPin, HIGH);}
    
  else if (abs(sensorDiff) <= threshold and forward == 1) {
    digitalWrite(GoForward, HIGH);
    digitalWrite(GoBack, LOW);
    digitalWrite(turnLeftPin, LOW);
    digitalWrite(turnRightPin, LOW);}
    
}

Doing some reading on scope and lifetime of variables might be useful for you.

  int forward;

In loop(), this defines a variable that is local to the loop() function. That is, it is not visible outside the loop() function. It also has a lifetime that exists for one call of the loop function. You get a brand new copy of forward each time loop() is called.

No wonder your program doesn't seem to remember what was stored in forward before. That's because it doesn't remember.

forward needs to be either a global variable, which changes its scope and lifetime, not a good thing, or static, which changes just its lifetime, which is all you need to change.

static int forward;

will change the behavior of your program.

You're perfectly right, thank you so much, inserting the "static" worked.