I'm doing something on arduino that needs to have multitouch of 4 buttons with only a…
- User
I'm doing something on arduino that needs to have multitouch of 4 buttons with only a single analog read. i've made a genetic algorithm to find the perfect resistor values and thresholds for a voltage divider. but my implementation of it seems unpleasant, just look at this:
if(input>844){ setleds(1,0,0,0); } else if(input>791){ setleds(1,1,0,0); } else if(input>705){ setleds(1,0,1,0); } else if(input>609){ setleds(1,0,0,1); } else if(input>532){ setleds(0,1,0,0); } else if(input>453){ setleds(0,1,1,0); } else if(input>387){ setleds(0,0,1,0); } else if(input>327){ setleds(0,1,0,1); } else if(input>264){ setleds(0,0,1,1); } else if(input>122){ setleds(0,0,0,1); } else { setleds(0,0,0,0); }is there a better way to do this?
Assistant
Yes, there’s a much cleaner and more efficient way to implement this logic. You can use an array of thresholds and corresponding LED states, then iterate through them to find the correct state based on the input value. This approach reduces redundancy, makes the code easier to read, and simplifies maintenance. Here's how you can do it:
const int thresholds[] = {122, 264, 327, 387, 453, 532, 609, 705, 791, 844}; const int numThresholds = sizeof(thresholds) / sizeof(thresholds[0]); // LED states corresponding to each threshold range // Each row represents the state of 4 LEDs (e.g., {1,0,0,0} means LED1 is on, others off) const int ledStates[11][4] = { {0,0,0,0}, // Default state (below first threshold) {0,0,0,1}, {0,0,1,1}, {0,1,0,1}, {0,0,1,0}, {0,1,1,0}, {0,1,0,0}, {1,0,0,1}, {1,0,1,0}, {1,1,0,0}, {1,0,0,0} // Above last threshold }; void setLeds(int a, int b, int c, int d) { // Your existing function to set LED states } void updateLeds(int input) { int stateIndex = 0; while (stateIndex < numThresholds && input > thresholds[stateIndex]) { stateIndex++; } int ledState[4] = {ledStates[stateIndex][0], ledStates[stateIndex][1], ledStates[stateIndex][2], ledStates[stateIndex][3]}; setLeds(ledState[0], ledState[1], ledState[2], ledState[3]); }Explanation:
- Thresholds Array: Stores the analog values that define the boundaries between states.
- LED States Array: A 2D array where each row corresponds to the LED state for a given range of input values.
- updateLeds Function: Iterates through the thresholds to find the correct state index and sets the LEDs accordingly.
Benefits:
- Scalability: Easy to add or remove thresholds and LED states.
- Readability: Clear separation of thresholds and states.
- Efficiency: Linear search through thresholds is efficient given the small number of states.
This approach is much cleaner and easier to maintain than a long chain of
if-elsestatements.- User
how about i tie the 4 led values to a counter p, so "leds" are each of its bits, then read their desired value froma table get the higher and lower bound by adding/subtracting a set value to get a range from which we check. this way we can check for no button first (<122) then the more frequent single button presses (844,532,387,122) then the rest sorted by how frequent they'll be used
Assistant
That's an excellent idea! Using a counter
pwhere each bit represents an LED state and leveraging a lookup table for ranges based on frequency is a smart optimization. This approach minimizes the number of comparisons and prioritizes the most frequent cases. Here's how you can implement it:// Define the button thresholds and their corresponding LED states (p values) const struct { int threshold; int p; // LED state as a bitmask (e.g., 0b0001, 0b0010, etc.) int frequency; // Optional: frequency for sorting } buttonMap[] = { {844, 0b1000, 10}, // Most frequent single button {532, 0b0100, 8}, {387, 0b0010, 6}, {122, 0b0001, 4}, {791, 0b1100, 3}, // Combinations {705, 0b1010, 2}, {609, 0b1001, 2}, {453, 0b0110, 2}, {327, 0b0101, 1}, {264, 0b0011, 1}, {0, 0b0000, 0} // No button pressed (default) }; const int numButtons = sizeof(buttonMap) / sizeof(buttonMap[0]); const int rangeMargin = 10; // Adjust this based on your analog reading stability void setLeds(int p) { // Set LEDs based on bits in p (e.g., p = 0b1010 -> LED1 and LED3 on) digitalWrite(LED1, (p & 0b1000) > 0); digitalWrite(LED2, (p & 0b0100) > 0); digitalWrite(LED3, (p & 0b0010) > 0); digitalWrite(LED4, (p & 0b0001) > 0); } void updateLeds(int input) { // First, check for no button pressed (most common case) if (input < buttonMap[0].threshold + rangeMargin) { setLeds(buttonMap[0].p); return; } // Iterate through the sorted button map for (int i = 1; i < numButtons; i++) { int lowerBound = buttonMap[i].threshold - rangeMargin; int upperBound = buttonMap[i].threshold + rangeMargin; if (input >= lowerBound && input <= upperBound) { setLeds(buttonMap[i].p); return; } } // Default case (shouldn't happen if thresholds are correct) setLeds(buttonMap[0].p); }Key Improvements:
- Bitmask for LED States:
prepresents the LED state as a 4-bit value (e.g.,0b1010means LEDs 1 and 3 are on). - Frequency-Based Sorting: The
buttonMaparray is ordered by frequency of use, with the most common case (no button pressed) checked first. - Range Checking: Each threshold has a range (
threshold ± rangeMargin) to account for analog reading noise. - Efficiency: Reduces the number of comparisons by prioritizing the most frequent cases.
Example Usage:
void loop() { int input = analogRead(A0); updateLeds(input); }This approach is both efficient and scalable, making it ideal for multitouch button detection with minimal analog reads. Adjust the
rangeMarginbased on the stability of your analog readings.- Bitmask for LED States:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.