Hi there

Does anyone out there have a solution for converting the sharp IR sensors (GP2Y0A21YK) to distance - at the moment it is spitting out values exponentially. It seems like there are a lot of different libraries for this in arduino (https://github.com/guillaume-rico/SharpIR), but we cannot make it work in bela. Should I write my own function in c++ (and if so, how?) or is there a way to do it directly reading it from the adc~ in PD?

So, I understand that the sensor has an analog output that depends on the distance. According to the readme of the repo you linked, the distance (in cm) is given as : Distance = 29.988 X POW(Volt , -1.173)

When you read the voltage at the analog input with Puredata using [adc~ 3], the range is 0:1, and corresponds to input voltages in the range 0:4.096V. Therefore, in order to convert the [adc~] value to Volt, you need to multiply it * 4.096. Then you can use that value in the formula above. In Pd terms, this means:

with discrete operators:

[adc~ 3]
|
[*~ 4.096]
|
[pow~ -1.173]
|
[*~ 29.988]
|

or using [expr~]:

[adc~ 3]
|
[expr~ 29.988 * pow($v1 * 4.096, -1.173)]
|