M M
M M
DIIDevHeads IoT Integration Server
Created by Camila_99$$ on 9/27/2024 in #firmware-and-baremetal
How can I set variable increments for a rotary encoder?"
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 }
9 replies