Last active
July 21, 2022 19:36
-
-
Save todbot/f1dbdd6bec4f88ee2f1a3464e2e06853 to your computer and use it in GitHub Desktop.
Demo analog touch value using raw_filter and filtering with CircuitPython touchio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# touch_analog_code.py -- demo analog touch value using raw_filter and filtering | |
# 21 Jul 2022 - @todbot / Tod Kurt | |
# A simpler version of concept in "Trinkey Theramin" (sic) | |
# https://gist.github.com/todbot/50c0f388add780f9d75489e0bebad96c | |
import time | |
import board | |
import touchio | |
touch_pin = touchio.TouchIn(board.A3) # can be almost any pin on most boards | |
touch_baseline = touch_pin.raw_value * 1.02 # 2% over | |
def smooth(val, last_val, smoothing=0.90): | |
return (smoothing * last_val) + (1-smoothing) * val | |
touch_val = 0 | |
while True: | |
touch_new = touch_pin.raw_value - touch_baseline | |
touch_new = min(max(touch_new, 0),255) # clamp between 0-255 | |
touch_val = smooth(touch_new, touch_val, smoothing=0.93) # smooth it | |
print("touch: %3d" % touch_val) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment