Already sovled!!!!!!!!!!!!!!1
Please post the code you have, using code tags, and explain the problem you are having with it.
See the forum instructions.
You have to remember the old range reading then take a new reading. If the difference is positive the object is going away, if negative the object is getting closer to the sensor. The state change detection example shows the concept.
Lots here are ready to help, share with us the effort you have made so far.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Have you tried the example sketches for controlling LEDs and how to use an ultra sonic sensor ?
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
could you please elaborate
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.

You should only have one loop( ) function in a sketch.
Please use code tags when showing your code.
Did you try the ultra sonic example ?
no because I only have to use the HC-SR04 distance sensor
You need a variable like oldRange to hold the older range. When you take a newRange, compare that with the oldRange and find the difference. Light the LED depending on that difference. Then save the newRange into the oldRange for the next iteration.
Hello, I'm having a similar issue, and its really confusing the code, could you please write it?
Nope. I finished with homework almost 60 years ago.
Can you get the rangefinder to work?
i have written this sketch at least three times on this forum. look my messages.