Start the code by defining the constants and the global variables that will be used throughout the code.

Then define the pin modes in the setup section of the code.

Then create a function to turn off all the LEDs and the buzzer.

Now, calculate the "Distance" in inches by reading the values received from the Ultrasonic Sensor.

Then by checking the value of the "Distance" we will turn on and off the LEDs based on how far the object is. If the distance is greater than 200 then turn off all the LEDs and the buzzer as the object is out of range. Else if it is between 55 and 200 then turn on the green LED. If the object is between 15 and 55 then turn on the yellow LED, and if the object goes closer than 15 inches then turn on the red LED until it reaches 8 inches. When the distance becomes less than 8 start the buzzer along with the red LED.

Next bit of the code is to set the value of the counter based on the cars movement which then decides when to turn off the LEDs. It compares the value of "Distance" with the "TempDistance" and if the values are same (object hasn't moved) then increments the counter. If the object moves any-time during this process the counter is reset to 0. Finally the "TempDistance" is set to the value of "Distance".

Just before comparing the Distances we also need to check if the counter value has exceed 20. I am doing this to stop the below code from executing if the car is in a steady position.

Lastly we just need to add a small delay to our sketch to pause the code for a while.