The disclosure describes a system that measures the distance between one
or more electrodes and tissue of a patient, and controls one or more
parameters of the stimulation delivered to the tissue by the electrodes
based on the measured distance. The system controls the measurement of
the distance between the electrodes and the tissue as a function of
activity of the patient. The system uses, for example, a piezoelectric
transducer to sense activity of the patient, and may determine whether or
how frequently to measure the distance between electrodes and tissue
based on the sensed physical activity. A piezoelectric transducer may be
used both to sense activity and to measure the distance between the
electrodes and the tissue.