Using Knob as Encoder


Well-known member
I recall Mr. PedalPCB talking about it being theoretically possible to use a knob as an encoder. Has anyone figured out how to do this yet?

I'm playing around with the petal Tremolo example now and it relies on the encoder to change the waveform, but would be nice to use on several different of these examples.


Admin (Robert)
Staff member
There are a few different ways to do it.

The easiest way would be to set the knob up with the minimum / maximum "switch" positions required then quantize the value. The maximum knob value will need to be one higher than the number of "switch" positions you require.
pot_rotary.Init(hw.knob[hw.KNOB_1], 0, 10, Parameter::LINEAR);  // For a 10-position switch (0 - 9)

Then use std::floor to round the pot value down to the nearest integer. This will quantize the pot value to an integer between 0 and 9. (simulating a 10 position rotary switch)
int position = std::floor(pot_rotary.Process());  // Round down to nearest integer value

In order to use the std::floor function you'll also need to include the cmath header at the top of your source file.
#include <cmath>
Last edited:
I am working on a tremolo too that is pretty close to done (just trying to minimize the 1kHz noise stuff....). It turns out the Daisy code already has a version of the floor function (edit* actually I think DSY_CLAMP is a like a rounding function instead of floor) built in which is called 'DSY_CLAMP', so you don't need to pull in the math library. Below is a snippet of how I implement this for 4 positions (using some of the code @PedalPCB already pasted).

uint8_t waveforms[NUM_WAVEFORMS] = {
pot_rotary.Init(hw.knob[hw.KNOB_1], 0, 4, Parameter::LINEAR);
waveform = DSY_CLAMP(pot_rotary.Process(), 0, NUM_WAVEFORMS)
osc_trem.Init(samplerate); // Initializes the oscillator
osc_trem.SetWaveform(waveforms[waveform]); // This sets the waveform to whatever the pot selected.
Last edited: