Magnet sensor sweep code

Very first hit on Google: