pulseIn and led

Maybe you'll find the map function useful: