Simple Analog -- Could Use Some Help
JonnyMac
Posts: 9,510
I have this constant:
con ' %AAAA_BBBB_FFF_PPP_PPP_PPPPPPP_TT_MMMMM_0 ANALOG = %0000_0000_000_100_011_0000000_00_11000_0...and this little snippet of code using a 10K pot with between 3.3v and ground, with the wiper attached to pin 40.
pinstart(40, ANALOG, %00_1011, 0)
repeat
waitms(100)
level := rdpin(40)
term.fstr1(string("POT = %d\r"), level)
It works, but instead of 0..4095 I am getting 705..3389. 
Comments
con ' %AAAA_BBBB_FFF_PPP_PPP_PPPPPPP_TT_MMMMM_0 AIN_GND = %0000_0000_000_100_000_0000000_00_11000_0 AIN_VIO = %0000_0000_000_100_001_0000000_00_11000_0 AIN_PIN = %0000_0000_000_100_011_0000000_00_11000_0 pub main() | lo, hi, level setup() pinstart(40, AIN_GND, %00_1100, 0) waitms(100) lo := rdpin(40) pinstart(40, AIN_VIO, %00_1100, 0) waitms(100) hi := rdpin(40) pinstart(40, AIN_PIN, %00_1100, 0) repeat waitms(100) level := map(rdpin(40), lo, hi, 0, 4095) term.fstr1(string("POT = %d\r"), level) pub map(value, inmin, inmax, outmin, outmax) : result '' Maps value in range inmin..inmax to new value in range outmin..outmax value := (value - inmin) * (outmax - outmin) / (inmax - inmin) + outmin return outmin #> value <# outmaxwhen we take a 16 bit ADC sample (0..65535),
"0" corresponds to around -0.7v DC
"10~12000" corresponds to 0v GND
"53~55000" corresponds to 3v3
"65535" corresponds to around 4v
You're working on 12 bits, so divide the above values by 16, gives you around 700 to 3400 that you are seeing