Some code somewhere is going to have to do the hard work.
You could use a FOR look and work through the array from start to finish and break when you get to the one that is next higher than your test value.
Perhaps you could test for greater or less than the middle value and start at the top or bottom of the array accordingly.
Another option is to start by testing the value in the middle. If it is too high then try the value half-way between the middle and bottom. etc etc. That should get you to the result with fewer tests. Whether, on average, it uses fewer MCU cycles is another matter.
Robin2:
Another option is to start by testing the value in the middle. If it is too high then try the value half-way between the middle and bottom. etc etc. That should get you to the result with fewer tests. Whether, on average, it uses fewer MCU cycles is another matter.
Delta_G:
The absolute simplest way is if you can map your input values to a run of contiguous integers and just use them as the index into the look-up table.
I thought of that... but would that not mean a bigger lookup table.
input is 0-1023
current lookup table is 255 values
also as I mention in my OP, my aim is to return the 2 values between which the ADC value lie if it not equal to any.
sherzaad:
also as I mention in my OP, my aim is to return the 2 values between which the ADC value lie if it not equal to any.
Sometimes you want to return two values, other times only one value. You mention when the switch will occur, but how are you handling sometimes getting one value and other times getting two?
This also means the lookup table must be descending or ascending only.
The confusion continues...
sherzaad:
current lookup table is 255 values
How do you only have one set of 255 values? There must be an input value and an output value which means at least 2x 255 values. One of the value must be the return result and the other value the ADC value. It appears your thinking is to make a an array of a structure (or two synchronized arrays) where one aspect is the ADC value and the other the return value. Either way you need 510 values and requires looping until the entry is found that exceeds or equal the input value. At which point you know the return result(s).
To skip the looping, use the input value as the array index and just hand populate the 1024 values of the array.
If a PROGMEM expert can jump in here to confirm or deny they ability to have the array placed in program memory instead of RAM will be of great usefulness.
adwsystems:
The confusion continues…
How do you only have one set of 255 values? There must be an input value and an output value which means at least 2x 255 values.
Not really if the data is sorted, then the index in the array is one of the value. it’s implicit.
say we have only 5 entries and the data is
0 100 300 900 1023
if the reading is 200, then I think OP wants to get that this value is between index 1 and 2
sherzaad:
I thought of that... but would that not mean a bigger lookup table.
input is 0-1023
current lookup table is 255 values
also as I mention in my OP, my aim is to return the 2 values between which the ADC value lie if it not equal to any.
Why would you need a larger table? If you have 1023 input values to map to 255 output values then you just divide by four to get the index. 0 through 3 all map to the same result and 4 through 7 all map to the same value etc.
If you want the one that lies one above or one below then just add one or subtract one from said index.
J-M-L:
Not really if the data is sorted, then the index in the array is one of the value. it's implicit.
say we have only 5 entries and the data is
0 100 300 900 1023
if the reading is 200, then I think OP wants to get that this value is between index 1 and 2
That is correct in this case, my return values would be 100 and 300.
If it reading was 900, then I return value would be 900 and 900
Delta_G:
Why would you need a larger table? If you have 1023 input values to map to 255 output values then you just divide by four to get the index. 0 through 3 all map to the same result and 4 through 7 all map to the same value etc.
If you want the one that lies one above or one below then just add one or subtract one from said index.
Interesting concept. I'll have to try it out to see if that actually works reliably for me