Tramway model controlled by ultrasonic sensor

Hello,

I am building an aerial tramway model. The cabin is suspended on a long cable reeled around a drum powered by a DC motor. The motor speed and direction is controlled by an H-bridge. I am using the ultrasonic sensor HC-SR04 to determine the distance between the tramway and the station. How I want it to work is: If fDistance is less than 5cm, then the motor will roll in one direction; and if fDistance is more than 100cm, then the motor will roll in the opposite direction, thus make the cabin come back.

The problem that I encountered: When (5<=fDistance<=100), the motor does not keep its current direction. Please help me out with the coding for the main loop.

const int In1 = 3;      
const int In2 = 5;      
const int In3 = 6;      
const int In4 = 11;

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
//#define LEDPin 13 // Onboard LED

int speedL, speedR, time, fDistance, Low, High;
long duration, distance; // Duration used to calculate distance


void setup()
{
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT); 
} 

void forward(int speedR)
{
analogWrite(In4, 0);
analogWrite(In3, speedR);

}

void reverse (int speedR)
{
analogWrite(In4, speedR);
analogWrite(In3, 0);

}

void stop() //Makes wheels stop
{
analogWrite(In4, 0);
analogWrite(In3, 0);
analogWrite(In2, 0);
analogWrite(In1, 0);
}

void scan()
  {
  /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 Serial.println(distance);
 //digitalWrite(LEDPin, LOW);
 if(distance == 0) //If no ping was recieved
    { 
    distance = 100; //Set the distance to max
    }
 
 //Delay 10ms before next reading.
 delay(10);
  }
void loop()
{
}