How can I set variable increments for a rotary encoder?"
Hi guys,, I want to use a rotary encoder with a selected range (for example, from 0 to 200) with varying increments. I came across an example from Matthias Hertel's library (RotaryEncoder-LimitedRotator) that I would like to upgrade by changing the increment from a certain value, but I haven't been able to achieve that.
For example, I would like my value to increment by 0.1 between 0 and 10, then by 0.5 between 10 and 20, and then by 1 from 20 to 200.
Do you have any solutions to propose?
Below is the code that I would like to upgrade.
For example, I would like my value to increment by 0.1 between 0 and 10, then by 0.5 between 10 and 20, and then by 1 from 20 to 200.
Do you have any solutions to propose?
Below is the code that I would like to upgrade.
Solution:Jump to solution
Yh @M M sure that the dynamic increment function actually addresses the issue .
We might also want to optimize the getIncrement function by using a single if- else if chain or a switch statement for better readability and performance, especially if the range of increments becomes more complex tho...
7 Replies
Hi @Camila_99$$ , I checked your code and you need to add and modify in any part.
add a function to adjust the increment dynamically base on the position.
float getIncrement(float pos) {
if (pos >= 0 && pos < 10) {
return 0.1;
} else if (pos >= 10 && pos < 20) {
return 0.5;
} else {
return 1.0;
}
}
then update the position in the loop.
Solution
Yh @M M sure that the dynamic increment function actually addresses the issue .
We might also want to optimize the getIncrement function by using a single if- else if chain or a switch statement for better readability and performance, especially if the range of increments becomes more complex tho
Good suggestion! The switch statement with range comparison definitely improves readability and performance. But, the range-based case (case 0 ... 9) is a GCC extension and may not be supported on all compilers or platforms.
Hi @M M @Marvee Amasi ,
Thanks for the insights! I’m leaning towards using a switch statement for improved readability. However, considering the compatibility issue with range-based cases, I’ll explore alternative approaches that ensure broader support. Maybe we could look into structuring ranges with separate cases or using a different method to maintain compatibility? What do you think?
Thanks for the insights! I’m leaning towards using a switch statement for improved readability. However, considering the compatibility issue with range-based cases, I’ll explore alternative approaches that ensure broader support. Maybe we could look into structuring ranges with separate cases or using a different method to maintain compatibility? What do you think?
yes, right!
Using a switch would definitely improve readability, but ensuring compatibility is important.
One way to handle this is to break down the range logic into discrete cases, but that could still get cumbersome as the ranges grow.
An alternative could be using a simple array or struct to map position ranges to increments. This would make it easier to extend or adjust the ranges without affecting performance.
struct Range {
float min;
float max;
float increment;
};
Range ranges[] = {
{0, 10, 0.1},
{10, 20, 0.5},
{20, 200, 1.0}
};
float getIncrement(float pos) {
for (Range range : ranges) {
if (pos >= range.min && pos < range.max) {
return range.increment;
}
}
return 1.0; // default case
}