Use analogRead() to get the pot position and then call the functions to adjust the filter accordingly.
For an example, check out Part 2-7 in the audio library tutorial, which starts on page 19 in the PDF.
https://www.pjrc.com/store/audio_tutorial_kit.html
The code can be opened in Arduino by clicking File > Examples > Audio > Tutorial > Part_2_07_Filters.
While this example covers the normal filter, the process is pretty much the same for any filter or effect, including the ladder filter.
Serial.begin(9600);
while (!Serial) ; // wait for serial monitor to open
for (int knob=0; knob <= 1000; knob += 50) {
Serial.print(knob);
Serial.print(" -> ");
float freq = expf((float)knob / 150.0) * 10.0 + 80.0;
Serial.println(freq);
}
}
void loop() {
}
0 -> 90.00
50 -> 93.96
100 -> 99.48
150 -> 107.18
200 -> 117.94
250 -> 132.94
300 -> 153.89
350 -> 183.12
400 -> 223.92
450 -> 280.86
500 -> 360.32
550 -> 471.21
600 -> 625.98
650 -> 841.98
700 -> 1143.43
750 -> 1564.13
800 -> 2151.27
850 -> 2970.69
900 -> 4114.29
950 -> 5710.30
1000 -> 7937.72
It converts linear scale that your potentiometer has to logarithmic frequency scale (how human hearing works), so equal knob movement represents one octave movements say from 100 to 200Hz and 1kHz to 2kHz (for example)
Here's a quick way to see what it (or pretty much any equation) really does.
Code:Serial.begin(9600); while (!Serial) ; // wait for serial monitor to open for (int knob=0; knob <= 1000; knob += 50) { Serial.print(knob); Serial.print(" -> "); float freq = expf((float)knob / 150.0) * 10.0 + 80.0; Serial.println(freq); } } void loop() { }
If you run this, you'll see this printed in the serial monitor
Code:0 -> 90.00 50 -> 93.96 100 -> 99.48 150 -> 107.18 200 -> 117.94 250 -> 132.94 300 -> 153.89 350 -> 183.12 400 -> 223.92 450 -> 280.86 500 -> 360.32 550 -> 471.21 600 -> 625.98 650 -> 841.98 700 -> 1143.43 750 -> 1564.13 800 -> 2151.27 850 -> 2970.69 900 -> 4114.29 950 -> 5710.30 1000 -> 7937.72
If i used a log pot instead of linear would i need to use a linear equation?
Maybe a log pot could avoid the need for the equation. But a log or "audio taper" pot is a poor solution for a couple reasons.
1: The ADC resolution is poorly utilized, with just a tiny part of its range to cover all the lower octaves which are most important for human hearing and most of the analog range used for the top few octaves which aren't nearly as important. Half the analog range gets used for the top octave from approx 10kHz to 20kHz, which hardly any people hear well.
2: Most log / audio taper pots are really just piecewise linear, in many cases with just 2 linear regions, maybe 3 in high quality pots. It's a poor approximation compared to the equation.
The equation is practically "free", especially in Teensy 3.5 to 4.1 which have floating point in hardware.