Ultrasonic Sensor going back to zero problem

Hi! I've a Autonomous 4WD, 2 Ultrasonic sensors in front. I've a code problem. How can I make my Ultrasonic sensor not to go back at zero when it's not hitting anything, I want it to stop at the max distance that I set when object is too far. I'm using newping library.

I've this on my code.

if((echoMovementLeft < 100) && (echoMovementRight < 100)) {
motorStop();
motorReverse();
quickLeft();

it will reverse at < 100 but if the sensor is not hitting anything, it'll go to zero and my 4WD will reverse, even if it's not hitting anything.

here is the part of the code that i'm not sure if right.

delay(50);
unsigned int echoLeft = sonarLeft.ping();
echoMovementLeft = map(echoLeft, 0, 8300, 0, 200); //200 is just a random number that i chose.
echoMovementLeft = constrain(echoMovementLeft, 0, 200);
//Serial.print("echo left : ");
//Serial.println(echoMovementLeft);
delay(50);
unsigned int echoRight = sonarRight.ping();
echoMovementRight = map(echoRight, 0, 8300,0, 200);
echoMovementRight = constrain(echoMovementRight, 0, 200);
//Serial.print("echo right : ");
//Serial.println(echoMovementRight);

Here is part of my answer: post your code.
Tell us what it does, tell us what it doesn't do that you expect it to.
Use code tags.

Thanks for the reply. Sorry I thought it's not necessary to post the whole code since my question is just How to make the ultrasonic sensor value not to return to zero when not hitting anything.
here is my code.
this is for RC and Autonomous 4WD, it's working but the only problem is when object infront is too far, sonic sensor backs to 0 from 255.

#include <NewPing.h>
int forwardLed = 13; //Indicator for Quick Start.
int ledStatus;
// Motors
int motor1a = 10; // Motor1 is LEFT Side (Back View).
int motor1b = 9; 
int motor2a = 5; // Motor2 is RIGHT Side (Back View).
int motor2b = 6;

//Heatsink Fan
int fan = 7 ;  

//For Forward and Reverse
int rcThrottle = 11; //Rx to Arduino Pin 11. 
int rcThrottleValue;
int rcThrottleMovement;

//For Left and Right
int rcRudder = 3; //Rudder Used for Left and Right
int rcRudderValue;
int rcRudderMovement;

//For Light Sensor
int photoSen = A5;
int photoSenValue;
int photoSenMovement;

//For Lights
int headLights = 4;

//Sensors for Autonomous.
int blackIR = A1; // IR Sensor
int whiteIR = 8; // IR Emitter/IR Sensor (will switch on both).
int blackIRValue;
int blackIRMovement;

//Ultrasonic Sensors
int echoLeft = A0; //Left(back view)
int triggerLeft = A0; //Trigger and Echo using Same Pin.
int echoMovementLeft;

int echoRight = A4; //Right (back view)
int triggerRight = A4; //Trigger and Echo using Same Pin.
int echoMovementRight;
int MAX_DISTANCE = 400;
NewPing sonarLeft(triggerLeft, echoLeft, MAX_DISTANCE); 
NewPing sonarRight(triggerRight, echoRight, MAX_DISTANCE);

//Switch for RC or Autonomous Mode.
int mode = 2;
int switchMode;
//Mode Lights
int rcLed = 12; // RC Mode Led, Color Red.
int autoLed = 1; // Autonomous Mode Led, Color Yellow.
//Millis
long previousMillis = 0;
long interval = 70;

void setup() {
  
 //Head Lights
  pinMode(headLights, OUTPUT);
//Light Sensor
  pinMode(photoSen, INPUT);
  // Motors 
  pinMode(motor1a, OUTPUT);
  pinMode(motor1b, OUTPUT);
  pinMode(motor2a, OUTPUT);
  pinMode(motor2b, OUTPUT);
//RX Inputs
  pinMode(rcThrottle, INPUT);
  pinMode(rcRudder, INPUT);
//H-Bridge Heatsink Fan  
  pinMode(fan, OUTPUT);
//IR Sensors  
  pinMode(blackIR, INPUT);
  pinMode(whiteIR, OUTPUT);
//Switch for RC Mode or Autonomous Mode.
  pinMode(mode, INPUT);
// Mode Lights
  pinMode(rcLed, OUTPUT);
  pinMode(autoLed, OUTPUT);
//Forward Movement LED
  pinMode(forwardLed, OUTPUT); 
//Serial.begin(9600); // For TESTING ONLY. Turning this ON will disable TX or D1.
//-----------------------------  
  analogWrite(motor1a, LOW);
  analogWrite(motor1b, LOW);
  analogWrite(motor2a, LOW);
  analogWrite(motor2b, LOW);
  analogWrite(fan, 200); //Fan Starts when Arduino is On.

}
  void loop() {
 
 //For Headlights   
  photoSenValue = analogRead(photoSen);
  photoSenMovement = map(photoSenValue, 0, 700, 0, 255);
  photoSenMovement = constrain(photoSenMovement, 0, 255);
// Serial.println(photoSenMovement);
 if(photoSenMovement > 177) { 
    digitalWrite(headLights, HIGH);
//   motorStop(); //Enable to Turn motor off on Dark Places.
 }else{ 
    digitalWrite(headLights, LOW);
}    
    
 //For RC or Autonomous Mode Switch    
  switchMode = digitalRead(mode);
//  Serial.println(switchMode);
 if(switchMode == HIGH) {
   
   RC(); //RC Mode, No Sensors.
    digitalWrite(rcLed, HIGH);
    digitalWrite(autoLed, LOW);
 
 }else if(switchMode == LOW) {
    Autonomous(); // Uncontrollable by TX, Sensors On. Camera Servos can still be controlled.
    digitalWrite(autoLed, HIGH);
    digitalWrite(rcLed, LOW);
  }
  } 
  
// RC MODE

 void RC() { 
   
    rcThrottleValue = pulseIn(rcThrottle, HIGH, 25000); 
    rcThrottleMovement = map(rcThrottleValue, 1110, 1867,-378, 378 ); 
    rcThrottleMovement = constrain(rcThrottleMovement, -255, 255);
    
    rcRudderValue = pulseIn(rcRudder, HIGH, 25000);
    rcRudderMovement = map(rcRudderValue, 948,2031, -541, 541);
    rcRudderMovement = constrain(rcRudderMovement, -255, 255);
 //   Serial.println(rcRudderMovement);

   if((rcThrottleMovement >=-20) && (rcThrottleMovement <20))  {
     motorStop();
   }else if(rcThrottleMovement >=20) {
      motorForward(); // Forward, Left, Right.
   }else if(rcThrottleMovement <-20) {
     motorReverse();
   } 
} 

  void motorForward() {
   
    analogWrite(motor1a, rcThrottleMovement);
    digitalWrite(motor1b, LOW);
    analogWrite(motor2a, rcThrottleMovement);
    digitalWrite(motor2b, LOW);
   
  //Right Turn
 //   rcRudderMovement = abs(rcRudderMovement);
   if((rcRudderMovement >10) && (rcRudderMovement < 230)) {
    
     analogWrite(motor1a, 250);
     digitalWrite(motor1b, LOW);
     
     analogWrite(motor2a, 100); //
     digitalWrite(motor2b, LOW);

 }else if(rcRudderMovement > 230) { // Quick Turn.
    
     analogWrite(motor1a, 250);
     digitalWrite(motor1b, LOW);
     
     digitalWrite(motor2a, LOW); 
     analogWrite(motor2b, 250);
 }
 //Left Turn
  
  if((rcRudderMovement < -10) && (rcRudderMovement > -230)) {
    //rcRudderMovement = abs(rcRudderMovement);
    analogWrite(motor1a, 100);
    digitalWrite(motor1b, LOW);
    
    analogWrite(motor2a, 250);
    digitalWrite(motor2b, LOW);
 
 }else if(rcRudderMovement <-230) { // Quick Turn.
 
    digitalWrite(motor1a, LOW);
    analogWrite(motor1b, 250);
    
    analogWrite(motor2a, 250);
    digitalWrite(motor2b, LOW);
     
 } 
 }

  void motorReverse() {
    
    rcThrottleMovement = abs(rcThrottleMovement); // To reverse the effect of Stick(from slow to fast).
    digitalWrite(motor1a, LOW);
    digitalWrite(motor1b, HIGH);
    analogWrite(motor1b, rcThrottleMovement); 
    digitalWrite(motor2a, LOW);
    digitalWrite(motor2b, HIGH);
    analogWrite(motor2b, rcThrottleMovement);   
}
 
  void motorStop() {
   
    digitalWrite(motor1a, LOW);
    digitalWrite(motor1b, LOW);
    digitalWrite(motor2a, LOW);
    digitalWrite(motor2b, LOW);
    
 } 
 

/code]

continuation...my message exceed the max allowable.

// AUTONOMOUS MODE
 void Autonomous() { // Autonomous Loop.
 
  ledStatus = digitalRead(forwardLed); //For quick Start.
  digitalWrite(whiteIR, HIGH); // IR Sensor and Emiiter On.
   unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  
  blackIRValue = analogRead(blackIR);
  blackIRMovement = map(blackIRValue, 1023, 560, 0, 563);
  blackIRMovement = constrain(blackIRMovement, 0, 563);
//Serial.println(blackIRMovement); //For TESTING ONLY.

//if(blackIRMovement < 50) // FOR WHITE FLOORS
 if(blackIRMovement > 200) //FOR BLACK FLOORS
 motorReverseIR();
 }else{
   sonicCheck();
//   ExsonicCheck();
 }
 }

void sonicCheck() {
 // Ultrasonic Sensors
 delay(50);
 unsigned int echoLeft = sonarLeft.ping();
 echoMovementLeft = map(echoLeft, 0, 8300, 0, 255);
 echoMovementLeft = constrain(echoMovementLeft, 0, 255);
 //Serial.print("echo left : ");
 //Serial.println(echoMovementLeft);
 delay(50);
 unsigned int echoRight = sonarRight.ping();
 echoMovementRight = map(echoRight, 0, 8300,0, 255);
 echoMovementRight = constrain(echoMovementRight, 0, 255);
 //Serial.print("echo right : ");
 //Serial.println(echoMovementRight);



if((echoMovementLeft < 100) && (echoMovementRight < 100)) {
  digitalWrite(forwardLed, LOW);
  motorStop();
  motorReverseA();
  quickLeft();
}else if((echoMovementLeft < 37) || (echoMovementRight < 37)) {  
  digitalWrite(forwardLed, LOW);
  motorStop();
  motorReverseA();
  quickLeft();  
}else if((echoMovementLeft < 155) && (echoMovementRight > 155) && (echoMovementRight > echoMovementLeft+10)) { 
  digitalWrite(forwardLed, LOW);
  quickRight();
}else if((echoMovementLeft > 155) && (echoMovementRight < 155) && (echoMovementLeft > echoMovementRight+10)) { 
  digitalWrite(forwardLed, LOW);
  quickLeft();
}else if(ledStatus == HIGH) {
 motorForwardSlowA(); 
}else{
 motorForwardA();
}
}
 void motorForwardA() {
   
    analogWrite(motor1a, 250); //Faster speed, just to make the car move.
    digitalWrite(motor1b, LOW);
    analogWrite(motor2a, 250);
    digitalWrite(motor2b, LOW);
    digitalWrite(forwardLed, HIGH); 
    delay(200); 
    
    analogWrite(motor1a, 100);  //Speed that I want.
    digitalWrite(motor1b, LOW);
    analogWrite(motor2a, 100);
    digitalWrite(motor2b, LOW);
    
 }
 
 void motorForwardSlowA() {
   
    analogWrite(motor1a, 90);  //Speed that I want.
    digitalWrite(motor1b, LOW);
    analogWrite(motor2a, 90);
    digitalWrite(motor2b, LOW);
 }  

 void motorReverseA() {
   
    digitalWrite(motor1a, LOW); 
    analogWrite(motor1b, 200);
    digitalWrite(motor2a, LOW);
    analogWrite(motor2b, 200);
    delay(250); 
} 

void leftTurnA() {
  
   analogWrite(motor1a, 50); 
   digitalWrite(motor1b, LOW);
   analogWrite(motor2a, 200);
   digitalWrite(motor2b, LOW);
   delay(1000);
}    
void rightTurnA() {      
  
   analogWrite(motor1a, 200);
   digitalWrite(motor1b, LOW);
   analogWrite(motor2a, 50); 
   digitalWrite(motor2b, LOW);
   delay(1000);
}
 void brake() { //Reverse code with very short delay. NOT TESTED
 
  digitalWrite(motor1a, LOW);
  analogWrite(motor1b, 10);
  digitalWrite(motor2a, LOW);
  analogWrite(motor2b, 10);
 delay(50);  
}
void quickLeft() {
   digitalWrite(motor1a, LOW);  
   analogWrite(motor1b, 150);
   
   analogWrite(motor2a, 200);
   digitalWrite(motor2b, LOW);
   delay(100);
}
void quickRight() {
     analogWrite(motor1a, 200);
     digitalWrite(motor1b, LOW);
     
     digitalWrite(motor2a, LOW); 
     analogWrite(motor2b, 150);
     delay(100);
} 
void motorReverseIR() { //Reverse for Floor Sensor
  
    digitalWrite(motor1a, LOW); // NOT TESTED
    analogWrite(motor1b, 200);
    digitalWrite(motor2a, LOW);
    analogWrite(motor2b, 200);
    delay(250);
    digitalWrite(forwardLed, LOW); //NOT TESTED
    quickRight();
}
/code]

my question is just How to make the ultrasonic sensor value not to return to zero when not hitting anything.

You can't. That makes no more sense than trying to make a pin not read LOW when the switch is not being pressed. What you need to do is complete a set of actions before you read the sensor again, if it is necessary that the set of actions be completed as a transaction.

PaulS:

my question is just How to make the ultrasonic sensor value not to return to zero when not hitting anything.

You can't. That makes no more sense than trying to make a pin not read LOW when the switch is not being pressed. What you need to do is complete a set of actions before you read the sensor again, if it is necessary that the set of actions be completed as a transaction.

Do you mean it's normal that when object is too close it reads zero and when its too far it also reads zero? Thanks for the reply. I'm gonna edit my code, ill try to add actions when it reaches max distance.

Hmmm if too far and too close are both zero, how can I make a code to stop at close zero and forward at far zero? LOL

Do you mean it's normal that when object is too close it reads zero and when its too far it also reads zero?

Yes. If the time-of-flight of the signal is very short (the object is very close), you will get a value of 0. If the time-of-flight exceeds some value (MAX_DISTANCE), you will also get 0 back.

You can determine whether a given value of 0 means very close or very far by keeping track of whether the values are increasing (the thing is getting farther away) or decreasing (the thing is getting closer).