using the resitor values will work
Quick python sketch shows it can be done quite easily (C++ will need array size parameter)
def analogValues(resistors):
"""
builds a list of values for analogRead() based upon resistor values
"""
sum = 0
for val in resistors:
sum += val
partial = 0
av = [0]
for val in resistors:
partial += val
# print partial, (partial * 1023) / sum
av.append( (partial * 1023) / sum)
return av
def findClosestIndex(val, values):
"""
finds the index in values that matches val best
"""
min = val
idx = 0
for i in range(len(values)):
dist = abs(val-values[i])
if (dist < min):
min = dist
idx = i
# print dist
return idx
#
# prog starts here
#
resistors = [100,400,100,200,400,300,400,100,200,400,300,100]
av = analogValues(resistors)
print av
for i in range(103):
val = i * 10
idx = findClosestIndex(val, av)
print val, idx, av[idx], abs(val-av[idx])
findClosestIndex() can be optimized by stopping the search when the distance rises again (~ factor 2)
1023 in the code is a hard coded max value of the ADC; could be a parametrized.