Filtering Serial Monitor from HC-SR04

Hi Guys,

i have a specific question, which i could not find in the forum or the web. Maybe i looked at the wrong sites, but i looked for a lot of different keywords so i hope my topic is not a repost for you. So here is what i have done:

  • i connected my HC-SR04 with my Arduino and looked for a working code
  • i modified the code, because i will not use the exact distance (the arduino is used to send data to processing)

Here is my modified code:

#define trigPin 10
#define echoPin 13

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

void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;

Serial.println(distance);
delay(250);
}

My Problem is, that the data is sometimes wrong, when i move to the sensor. It´s than looking like the picture in the attachment. Is it possible to filter the bigger and wrong data with arduino. What do i have to do to filter that data?

Thank you guys.

Bildschirmfoto 2017-02-24 um 19.02.14.png