You can simplify and organize your code by using a lookup table rather than a long series of if/else statements. One common approach is to create an array of thresholds alongside an array (or a struct) for the LED patterns. Then, you can loop over these arrays to compare the analog value and set the corresponding LEDs. This also makes it easier to adjust your thresholds or patterns later if your design changes.
For example, you could do something like this:
// Define the thresholds (in descending order)
const uint16_t thresholds[] = {844, 791, 705, 609, 532, 453, 387, 327, 264, 122};
const uint8_t NUM_THRESHOLDS = sizeof(thresholds) / sizeof(thresholds[0]);
// Define the LED patterns for each threshold, in the same order.
// You can store them as an array of 4-element arrays.
const uint8_t patterns[][4] = {
{1, 0, 0, 0}, // for input > 844
{1, 1, 0, 0}, // for input > 791
{1, 0, 1, 0}, // for input > 705
{1, 0, 0, 1}, // for input > 609
{0, 1, 0, 0}, // for input > 532
{0, 1, 1, 0}, // for input > 453
{0, 0, 1, 0}, // for input > 387
{0, 1, 0, 1}, // for input > 327
{0, 0, 1, 1}, // for input > 264
{0, 0, 0, 1} // for input > 122
};
// Default LED state if no threshold is passed
const uint8_t defaultPattern[4] = {0, 0, 0, 0};
void loop() {
// Read the analog input
int input = analogRead(A0); // example analog pin
// Check the thresholds in order
bool patternFound = false;
for (uint8_t i = 0; i < NUM_THRESHOLDS; i++) {
if (input > thresholds[i]) {
setleds(patterns[i][0], patterns[i][1], patterns[i][2], patterns[i][3]);
patternFound = true;
break;
}
}
// If none of the thresholds match, use the default LED pattern
if (!patternFound) {
setleds(defaultPattern[0], defaultPattern[1], defaultPattern[2], defaultPattern[3]);
}
// Your other code ...
}
In the above code:
- Lookup arrays: The thresholds and corresponding LED patterns are stored in arrays. The order of the thresholds is critical; they must match the order of the patterns.
- Looping: A simple loop goes through each threshold, and if the input is greater than a threshold, the corresponding LED pattern is applied.
- Default pattern: If the input does not exceed any threshold, a default “all off” pattern is applied.
Advantages of This Approach
- Readability: The logic is separated from the configuration data (thresholds and patterns), which makes it much easier to understand what the code is doing.
- Maintainability: Should your voltage divider or resistor values change, you only need to adjust the arrays rather than modifying a long chain of conditionals.
- Scalability: If you add more thresholds or change the numbers, you simply update the arrays. The loop handles the rest.
Another alternative is to encapsulate the threshold and LED pattern into a struct and maintain an array of such structs:
struct ButtonMapping {
uint16_t threshold;
uint8_t pattern[4];
};
const ButtonMapping mappings[] = {
{844, {1, 0, 0, 0}},
{791, {1, 1, 0, 0}},
{705, {1, 0, 1, 0}},
{609, {1, 0, 0, 1}},
{532, {0, 1, 0, 0}},
{453, {0, 1, 1, 0}},
{387, {0, 0, 1, 0}},
{327, {0, 1, 0, 1}},
{264, {0, 0, 1, 1}},
{122, {0, 0, 0, 1}}
};
const uint8_t NUM_MAPPINGS = sizeof(mappings) / sizeof(mappings[0]);
void loop() {
int input = analogRead(A0);
bool found = false;
for (uint8_t i = 0; i < NUM_MAPPINGS; i++) {
if (input > mappings[i].threshold) {
setleds(mappings[i].pattern[0], mappings[i].pattern[1],
mappings[i].pattern[2], mappings[i].pattern[3]);
found = true;
break;
}
}
if (!found) {
setleds(0, 0, 0, 0);
}
// Other code...
}
This method gives the same benefit while grouping related data together.
Both approaches are much cleaner than a long chain of if/else statements, and the best method will depend on your own personal coding style and project requirements.