Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active June 18, 2023 22:56
Show Gist options
  • Save todbot/cd964282dd3c2599a629f5ecf5d1ca98 to your computer and use it in GitHub Desktop.
Save todbot/cd964282dd3c2599a629f5ecf5d1ca98 to your computer and use it in GitHub Desktop.
Convert dual pot "360 endless pot" to angle position
# convert a dual pot "360 endless pot" aka sin/cos pot to angle position
# 17 Jun 2023
# JP found these cool dual pots that act like endless rotary encoders,
# in a Elektron Samples box. They have two pots on a single shaft,
# one 90º out of phase. e.g.
# RV112FF-40-15A-0B20K - http://www.taiwanalpha.com/downloads?target=products&id=79
#
# see: https://forum.pjrc.com/threads/59830-Read-Endless-Potentiometer
# answer: float angle = atan2f(potA, potB)
#
import time
# make up some fake "pot value" arrays that hold possible A & B pot values, 0.0-1.0
divs = 50
# A has an up ramp going from 0-180 degrees
a = [ (i/divs) for i in range(0,divs)]
# A then has down ramp from 180-360 degrees
a += [ 1-(i/divs) for i in range(0,divs)]
# B is 90 degrees phase offset from A
b = a[divs//2:] + a[0:divs//2]
# print out our arrays to show we match the datasheet
#print("a:", len(a), ["%.2f" % x for x in a], "\n")
#print("b:",len(b), ["%.2f" % x for x in b], "\n")
while True:
for i in range(0,divs*2):
aval = a[i]
bval = b[i]
# convert knob positions aval & bval to angle
if aval < 0.5:
angle = 360 - bval * 180 # here we show degrees to be clear
else:
angle = bval * 180
# angle = 360 - bval * 180 if aval<0.5 else bval *180
print( "(%.2f, %.2f, %.2f)" % (aval, bval, angle/360))
@todbot
Copy link
Author

todbot commented Jun 17, 2023

Simple linear version (orange is angle output 0-1):

Screenshot 2023-06-17 at 2 14 11 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment