Interpolation - BleepLabs/Arduino-Light-And-Sound GitHub Wiki
Often we have an analogRead return a value that's considerably differnt that that last reading.
This is fine but want if you want the pot or other device to have a smoother response and not jump from one place to another.
All you need to do is interpolate between values. Here's a basic example of linear interpolation aka "lerp":
raw_reading = analogRead(A0);
if (raw_reading < interpolated_output){
interpolated_output--;
}
if (raw_reading > interpolated_output){
interpolated_output++;
}
interpolated_output can never move by more that 1 so will always be chasing the raw _reading.
Try out this code and open the serial plotter in the Tools menu to easily visualize it.
For exponential interpolation you could do something like this.
Everything should be a float.
raw_reading = analogRead(A0);
float increase_jump_size=1.01;
float decrease_jump_size=.95; //it will decrease faster
if (raw_reading < interpolated_output){
interpolated_output*=decrease_jump_size;
}
if (raw_reading > interpolated_output){
interpolated_output*=increase_jump_size;
}
Here's a full example of an exponential interpolation. You can see it's a bit trickier as you can never hit 0. Also you might not ever get to the exact target value.