Problem with my HC-SR04.

Hi,
I am having a problem with my HC-SR04 Range sensor. I’m building a obstacle avoiding robot. Sometimes my robot turns when it has enough space to drive further. Do you know how i can solve this?
This is my code: at void auto_mode() {} is the code for my HC-SR04.

long duration, distance;
int mode;
#define Green 24
#define Red 25
#define trigPin 22
#define echoPin 23
#include <AFMotor.h>
#include <IRremote.h>
int IRpin = 44;
int PotPin = A8;
IRrecv irrecv(IRpin);
decode_results results;
AF_DCMotor motorR(1);
AF_DCMotor motorL(2);

void setup() {
  irrecv.enableIRIn();
  Serial.begin(9600);
  motorR.setSpeed(100);
  motorL.setSpeed(103);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(Green, OUTPUT);
  pinMode(Red, OUTPUT);
pinMode(PotPin, INPUT);
}

void loop() {
checkMode();
}

void Forward() {
  motorR.run(FORWARD);
  motorL.run(FORWARD);
}
void Backward() {
  motorR.run(BACKWARD);
  motorL.run(BACKWARD);
  delay(250);
}

void Right() {
  motorL.run(BACKWARD);
  motorR.run(FORWARD);
  delay(375);
}

void Left() {
  motorL.run(FORWARD);
  motorR.run(BACKWARD);
  delay(375);
}
void Brake() {
  motorR.run(RELEASE);
  motorL.run(RELEASE);
  delay(100);
}

void auto_mode() {
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  delay(1);
  distance = (duration / 2) / 29.1;
  if (distance <= 25) {
    Brake();
    digitalWrite(Green, LOW);
    digitalWrite(Red, HIGH);
    Backward();
    Brake();
    Right();
  }
  else if(distance >=26) {
    digitalWrite(Red, LOW);
    digitalWrite(Green, HIGH);
    Forward();

  }
}

void manual_mode() {
  if (irrecv.decode(&results)) {
    
    Serial.print("irCode: ");            

    Serial.print(results.value, HEX); 

    Serial.print(",  bits: ");           

    Serial.println(results.bits); 

    irrecv.resume();  
  }
delay(10);
  if (results.value == 0xC796DFC || results.value == 0xB10C7C8B) {
    Forward();
  } else if (results.value == 0xDB510F56) {
    Backward();
  }
  else if(results.value == 0x4B12992B|| results.value == 0x7E16B93A){
    
  motorL.run(BACKWARD);
  motorR.run(FORWARD);
    delay(375);
    results.value == 0xC796DFC;
    delay(10);
  }
  else if(results.value == 0x1BE8C80D || results.value == 0x8ECEA106 || results.value == 0xC2A82EEA
){
  
    motorR.run(BACKWARD);
  motorL.run(FORWARD);
    delay(375);
results.value == 0xC796DFC;
delay(10);
  }  
   else if(results.value == 0xEFF8316D|| results.value == 0xF4704794){
   Brake();
  }
  else if(results.value == 0x37FF1DB2 || results.value == 0xE5EDF8D7){
   Right();
}
}

void checkMode(){
  mode = analogRead(PotPin);
  if(mode <= 512){
    manual_mode();
  }
  else if(mode >= 513){
    auto_mode();
    Serial.println(distance);
  }
 
  
}

Try taking several readings, discarding outliers, and using the average.

DaveEvans:
Try taking several readings, discarding outliers, and using the average.

How can i do this?

Sometimes my sensor gives weird numbers like 2389. When i try to solve it in the code it still turns.

use the NewPing library and its ping_median method

DaveEvans:
use the NewPing library and its ping_median method

Can you maybe give me a sample code?

I found a code on google but now im getting this error:

libraries\NewPing\NewPing.cpp.o: In function `__vector_13':

C:\Users\van Grinsven\Documents\Arduino\libraries\NewPing/NewPing.cpp:214: multiple definition of `__vector_13'

libraries\Arduino-IRremote-master\IRremote.cpp.o:C:\Users\van Grinsven\Documents\Arduino\libraries\Arduino-IRremote-master/IRremote.cpp:124: first defined here

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

collect2.exe: error: ld returned 1 exit status

exit status 1
Fout bij compileren.

This is my code now:

#define Green 24
#define Red 25
#define TRIGGER_PIN 22
#define ECHO_PIN 23
#define MAX_DISTANCE 400
#include <AFMotor.h>
#include <IRremote.h>
#include <NewPing.h>
int IRpin = 44;
int PotPin = A8;
int mode;
IRrecv irrecv(IRpin);
decode_results results;
AF_DCMotor motorR(1);
AF_DCMotor motorL(2);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
  irrecv.enableIRIn();
  Serial.begin(9600);
  motorR.setSpeed(100);
  motorL.setSpeed(103);
  pinMode(Green, OUTPUT);
  pinMode(Red, OUTPUT);
pinMode(PotPin, INPUT);
}

void loop() {
checkMode();
}

void Forward() {
  motorR.run(FORWARD);
  motorL.run(FORWARD);
}
void Backward() {
  motorR.run(BACKWARD);
  motorL.run(BACKWARD);
  delay(250);
}

void Right() {
  motorL.run(BACKWARD);
  motorR.run(FORWARD);
  delay(375);
}

void Left() {
  motorL.run(FORWARD);
  motorR.run(BACKWARD);
  delay(375);
}
void Brake() {
  motorR.run(RELEASE);
  motorL.run(RELEASE);
  delay(100);
}

void auto_mode() {
int dist = sonar.ping_median(5); 
dist = sonar.convert_cm(dist); 
  if (dist < 25 & dist != 0) {
 Brake();
    digitalWrite(Green, LOW);
    digitalWrite(Red, HIGH);
    Backward();
    Brake();
    Right();
  }
  else {
    Forward();
  }
  }
 


void manual_mode() {
  if (irrecv.decode(&results)) {
    
    Serial.print("irCode: ");            

    Serial.print(results.value, HEX); 

    Serial.print(",  bits: ");           

    Serial.println(results.bits); 

    irrecv.resume();  
  }
delay(10);
  if (results.value == 0xC796DFC || results.value == 0xB10C7C8B) {
    Forward();
  } else if (results.value == 0xDB510F56) {
    Backward();
  }
  else if(results.value == 0x4B12992B|| results.value == 0x7E16B93A){
    
  motorL.run(BACKWARD);
  motorR.run(FORWARD);
    delay(375);
    results.value == 0xC796DFC;
    delay(10);
  }
  else if(results.value == 0x1BE8C80D || results.value == 0x8ECEA106 || results.value == 0xC2A82EEA
){
  
    motorR.run(BACKWARD);
  motorL.run(FORWARD);
    delay(375);
results.value == 0xC796DFC;
delay(10);
  }  
   else if(results.value == 0xEFF8316D|| results.value == 0xF4704794){
   Brake();
  }
  else if(results.value == 0x37FF1DB2 || results.value == 0xE5EDF8D7){
   Right();
}
}

void checkMode(){
  mode = analogRead(PotPin);
  if(mode <= 512){
    manual_mode();
  }
  else if(mode >= 513){
    auto_mode();
  }
 
  
}

Try here

AWOL:
Try here

I did but i couldnt find a answer.

Finaly it is working. I had to comment out Timer 2 in the NewPing lib.

/*
#if defined (AVR_ATmega32U4) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
ISR(TIMER4_OVF_vect) {
#else
ISR(TIMER2_COMPA_vect) {
//#endif
if(intFunc) intFunc(); // If wrapped function is set, call it.
}
*/
This part in NewPing.ccp