Coding help; ultrasonic sensor

Hello again,

I have progressed on my simple obstacle avoidance car but I am running into an issue of false positives.

I am using an HC-SR04 ultra sonic sensor. I noticed that sometimes there would be bogus readings of 0, or -504 or something which would trigger the car to go into its "avoidance" sequence (stop back up for 2 sec turn right and continue). I am thinking that maybe if I set the parameters for the avoidance sequence to a range between maybe 5 and 40 CM I could avoid some of the bad readings I read online that this seems to be a common issue. . My question is simply how can I code for this? How can I tell my arduino that I want it to ignore any readings it gets other than the ones between 5-40 CM? and: is there any other way to avoid this problem?

THANKS!!!

I have bolded the areas of interest

This is the code for the whole car:

#define trigPin 6
#define echoPin 5

void setup()
{
Serial.begin (9600);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
//establish motor direction toggle pins
pinMode(12, OUTPUT); //drive motor -- HIGH = forwards and LOW = backwards
pinMode(13, OUTPUT); //turn motor -- HIGH = left and LOW = right

//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) the drive motor
pinMode(8, OUTPUT); //brake (disable) the turn motor

//Turns brake off for drive motor
digitalWrite(9, LOW);

//Turns brake on for turn motor
digitalWrite(8, HIGH);

//Sets initial speed of drive motor
analogWrite(3, 200);

//Sets initial direction of drive motor
digitalWrite(12, HIGH);
}

void loop()
{
//sensor
int duration, distance;
digitalWrite(trigPin, HIGH);
delay (10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1;
delay(10);
** if (distance < 40 ) { // distance set to 45 cm**
Serial.println (distance); // diagnostic for when connected to computer
//brake drive motor and pause 1/10 second
digitalWrite(9, HIGH);
delay(100);

//
//setting turn motor
//

//turn off brake for turn motor
digitalWrite(8, LOW);

//set turn motor direction
digitalWrite(13, HIGH);
//activate turn motor
analogWrite(11, 255);

//
//setting drive motor
//

//turn off brake of drive motor
digitalWrite(9, LOW);

//set drive motor backwards direction
digitalWrite(12, LOW);

//activate the drive motor
analogWrite(3, 200);

//backup for 2 seconds
delay(2000);

//
//stopping
//

//brake both motors
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);

}

//
//when nothing is within 40CM
//robot drives forwards
//

else{

//
//Setting drive motor
//

//set drive motor forward direction
digitalWrite(12, HIGH);

//turn off brake of drive motor
digitalWrite(9, LOW);

//activate drive motor
analogWrite(3, 200);

}

delay(100);
}

This is what I use to test the Sensor:

#define trigPin 6
#define echoPin 5

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

void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delay (10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1;
delay(10);
if (distance < 10 ) { // if there is something less than 10 CM from sensor, print distance
Serial.println (distance);
}
}

Hello, I will show you how to do that. Taking an average reading over a small period of time could also help.

if (distance < 40 ) {   // distance set to 45 cm
  
  if (distance > 5 ) { 

    Serial.println (distance); // diagnostic for when connected to computer
     //brake drive motor and pause 1/10 second
    digitalWrite(9, HIGH);
    delay(100);

    //
    //setting turn motor
    //
    
    //turn off brake for turn motor 
    digitalWrite(8, LOW);

    //set turn motor direction
    digitalWrite(13, HIGH);
    //activate turn motor
    analogWrite(11, 255);
    
    //
    //setting drive motor
    //
    
    //turn off brake of drive motor
    digitalWrite(9, LOW); 
    
    //set drive motor backwards direction
    digitalWrite(12, LOW);
    
    //activate the drive motor
    analogWrite(3, 200);

    
    //backup for 2 seconds
    delay(2000);
    
    //
    //stopping
    //
    
    //brake both motors
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    
    }
  }

Or this (probably best option)

  if (distance < 40 && distance > 5  ) {   // distance set to 45 cm
    Serial.println (distance); // diagnostic for when connected to computer
     //brake drive motor and pause 1/10 second
    digitalWrite(9, HIGH);
    delay(100);

    //
    //setting turn motor
    //
    
    //turn off brake for turn motor 
    digitalWrite(8, LOW);

    //set turn motor direction
    digitalWrite(13, HIGH);
    //activate turn motor
    analogWrite(11, 255);
    
    //
    //setting drive motor
    //
    
    //turn off brake of drive motor
    digitalWrite(9, LOW); 
    
    //set drive motor backwards direction
    digitalWrite(12, LOW);
    
    //activate the drive motor
    analogWrite(3, 200);

    
    //backup for 2 seconds
    delay(2000);
    
    //
    //stopping
    //
    
    //brake both motors
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    
    
  }

tico556:
Hello again,
if (distance < 40 ) {  // distance set to 45 cmSince when has 45 been less than 40? When I went to school, only 39.99999 or smaller was less than 40. :~