Measuring distance with hc-sr04

I am new to the Arduino, but I have a project that I am working on. I would like to use and HC-SR04 with an UNO to monitor the distance between a stationary object and a parked vehicle. I would like to store the distance in a variable when I trigger a switch and then compare the current distance with the saved distance and trigger an alarm when the distance moves apart. Accuracy isn't critical, say within a foot.

I have located code to interface with the HC-SR04 but the measurements I am getting seem to fluctuate widely at times. Sometimes I get large spikes. How do I go about getting a steady figure to use for my comparisons?

I have read about the Kalman filter and think it may be useful, but I'm not a math whiz and it really doesn't make any sense to me. That's why I'm here.

What is the target? Different target materials reflect ultra sound differently (a hanging cloth curtain might not reflect at all). "Clutter" or angled targets will cause funky reflections. How often are you ranging? You must wait long enough for reflections to die down or you will get errors.

The target would be the rear of the vehicle. I would try to point it at a flat surface perpendicular to the sensor.

The code I found goes low for 2 microseconds, high for 10 microseconds and then low again. After the pulseIn there is a delay of 100.

I just need to find a way to try and eliminate false alarms.

The vehicle will always be moving away from the sensor, so I can pretty much eliminate any reading close than my saved reading if that helps.

I would try something like: if(thisReading > lastReading + (lastReading * 0.2)) ignore thisReading. That is, if the reading increased more than 20% the reading is bad. Adjust for your situation.

I have seen it recommended to put a 10uf cap across the HCSR04 power pins to help stabilize the power (the ranger pulls current when pinging).

100 milliseconds, I think, should be long enough to let reflections die out.

That might work. How would you go about stabilizing the initial reading before saving it?

That is a good question. Without history you can't know what is a good reading. Maybe take 5 or 10 readings and average to start.