ultrasonic sensors for accident avoidance

I need to have an ultrasonic on a vehicle to measure the distance of the vehicle in front of it, in case the vehicle infront of it starts to stop suddenly the Board will detect this.

In order to detect this I need an algorithms to read a sudden decrements in the reading in term of time so

for example if the distance is decreasing 10 cm per second, it's an alert.

Thank you !

velocity = (previous distance - current distance)/(time between readings)

Use the millis() timer to determine the time between two distance measurements.