Distance sensor for Arduino?

I want to make a sort of self-educating machine as a programming project:
there is an electromagnet and a magnetic thing and if the distance between 
the magnets has increased between two measurements the Arduino lowers the
current through the magnet, and if had fallen (the distance, not the object) it ircreases...
In some time it should be able to reduce the aplitude of the object's oscillations.

Is there a good and easy way to make a non-contact distance sensor?
AND Is it possible at all????

AndyGadget6 years ago
That would be a magnetic levitator you're making then.
As Rick says, a Sharp distance sensor would be ideal for this.  They're great little devices if used properly,  but be careful as they have a quirk :-
As an object approaches the measured distance decreases (as you'd expect) but get closer than the minimum rated distance and the reading starts to increase.
rickharris6 years ago
ultra sonic

more accurate a sharp infra-red sensor.