Go Down

Topic: AnalogDebounce: New Library for debouncing Analog Keypads (Read 3 times) previous topic - next topic


  I want to share a thought but, I will have to use an example to do it.

I have a Olimex STM 32 (LeafLabs Maple clone) and the counts are different when using analog buttons in comparison to the Arduino Uno. To use the DFRobot sketches, I had to adjust the values by testing each button with the serial monitor showing the analogRead results. This is because of the higher bit resolution of the analogRead.

That being said, to be able to port the library to other boards such as the Due, you may need a way to work with a spread of analogRead results.


Indeed you are correct, the current defaults are based on a 10bit ADC, though really once you set it up you could do button.adc_key_val[0-4] = whatever.

I am going to figure out a much better way to handle the number of buttons and different resistor values. 


Oct 19, 2012, 11:16 am Last Edit: Oct 19, 2012, 11:18 am by robtillaart Reason: 1
using the resitor values will work

Quick python sketch shows it can be done quite easily (C++ will need array size parameter)
Code: [Select]

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.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up