Filter a control input to control an audible signal and explore signal precision.
Digitized signals are represented by a finite number of bits and thus are discretized in value as well as time. However, there is a often a tradeoff between time and resolution, since multiple samples of a measurement can be combined over time.
This exercise continues with audible signal generation since our ears are very sensitive to subtle changes of pitch.
static float smoothed_value = 0.0; // filtered value of the input int new_value = analogRead( analogPin ); // read the current input float difference = new_value - smoothed_value; // compute the 'error' smoothed_value += 0.1 * difference; // apply a constant gain to move the smoothed value toward the reading
As a challenge, try adding a differentiator to the potentiometer input to map the change rate of the input to the pitch.