This is still over complicated i meant literally a table like: {0,122,387,264,532, ...}
- User
This is still over complicated i meant literally a table like:
{0,122,387,264,532, ...}
Assistant
Ah, I see! Much simpler. You want a direct lookup table where the index represents the bit pattern for the LEDs. So index 0 = pattern 0000, index 1 = pattern 0001, index 5 = pattern 0101, etc.
const uint16_t thresholds[] = {0, 122, 387, 264, 532, 453, 705, /*...*/, 844}; const uint8_t tolerance = 15; // adjust as needed // Check in order of frequency - most common first const uint8_t checkOrder[] = {0, 1, 2, 4, 8, 3, 5, 6, 9, 10, 12, /*...*/ 15}; void loop() { int input = analogRead(A0); for (uint8_t i = 0; i < sizeof(checkOrder); i++) { uint8_t pattern = checkOrder[i]; if (input >= thresholds[pattern] - tolerance && input <= thresholds[pattern] + tolerance) { setleds((pattern >> 3) & 1, (pattern >> 2) & 1, (pattern >> 1) & 1, (pattern >> 0) & 1); return; } } // Default if no match setleds(0, 0, 0, 0); }Or even simpler if you just want to check in a specific order:
const uint16_t thresholds[] = {0, 122, 387, 264, 532, 453, 705, /*...*/, 844}; const uint8_t tolerance = 15; void loop() { int input = analogRead(A0); // Check no buttons first (pattern 0) if (input <= 122 + tolerance) { setleds(0, 0, 0, 0); } // Check single buttons next (patterns 1, 2, 4, 8) else if (abs(input - 844) <= tolerance) { // pattern 8 (1000) setleds(1, 0, 0, 0); } else if (abs(input - 532) <= tolerance) { // pattern 4 (0100) setleds(0, 1, 0, 0); } // ... continue with your frequency order }