ADC to milliseconds

westaust55

Moderator
Use READADC to get a value from 0 to 255.
Your range is 2000 - 250 = 1750
So if you multiply 255 by 1750/255 = 6.86 then add 250 you will have the desired range.
As you cannot use decimal/fractional maths and cannot allow internal math overflow you will need to multiply by 686/3 = 228 (0r try 229) and divide by 100/3 = 33 before adding the 250.
That still leads to some discrepancy
So use READADC * 247 / 36+ 250 and the max value will be 1999
 

lbenson

Senior Member
What kind of accuracy do you want? readadc10 will give you 1024 steps. You have 1750 ms-steps between your low and high values, so you multiply your adc value by 1750 and then divide by 1024. Unfortunately, picaxe can't handle numbers larger than 65536, so you have to scale things to fit the picaxe math.

Say bump 1024 up to 1025 and each is divisible by 25--1750/25=70 and 1025/25=41. Your maximum adc value is 1024, so you can't multiply that by 70 without overflowing. By dividing those numbers in half, you get ADCVal / 35 * 20.5--you still have the problem of the fractional value. You can get around this by calculating your half an ADCVal first, and then doing the rest of the calculation, and finally adding the half back in, plus your offset of 250. Something like this.
Code:
readadc10 w1
w2 = w1 / 2
w1 = w1 * 35 / 20 + w2 + 250
pause w1
 

geoff07

Senior Member
Or use a digital input with a rotary encoder, and use the pulses to step up/down the required delay time. This will give you absolute precision, but at the price of a rotary encoder (a few pounds). You might want to scale to a log progression. It all depends on precision and repeatability required.
 
Top