Stepped potentiometer
Connect a potentiometer (stepped or not) to Bela's analog input 0. This project quantises the reading from that potentiometer and will keep track of when it changes from one step to the next.
We define the steps in volts and list them in steppedPot.setup().
In this example we use the quantised range of the pot to change the frequecy of a sinetone oscillator. We use the position integer to multiply the base frequency of the oscillator and cycle through the harmonic series.
#include <cmath>
#include <libraries/SteppedPot/BelaSteppedPot.h>
const unsigned int kPotInChannel = 0;
int gStepNumber = 0;
float gFrequency = 220.0;
float gPhase;
float gInverseSampleRate;
{
steppedPot.
setup(context, kPotInChannel, {
0,
0.33,
0.66,
0.99,
1.32,
1.65,
1.98,
2.31,
2.64,
2.97,
3.3,
},
0.1,
4.096
);
gPhase = 0.0;
return true;
}
{
bool hasChanged = steppedPot.
process(context);
if(hasChanged)
{
rt_printf(
"Pot has moved to position %u\n", steppedPot.
get());
gStepNumber = steppedPot.
get();
}
for(
unsigned int n = 0; n < context->
audioFrames; n++) {
float out = 0.8f * sinf(gPhase);
gPhase += 2.0f * (float)M_PI * ((gStepNumber + 1) * gFrequency) * gInverseSampleRate;
if(gPhase > M_PI)
gPhase -= 2.0f * (float)M_PI;
}
}
}
{
}