pimoroni-pico/micropython/examples/encoder/value_dial.py

41 lines
939 B
Python
Raw Normal View History

2022-04-19 20:22:35 +01:00
import gc
from encoder import Encoder
2022-05-12 16:15:40 +01:00
# from encoder import REVERSED_DIR
2022-04-19 20:22:35 +01:00
"""
2022-05-12 14:45:20 +01:00
A demonstration of a rotary encoder being used to control a value.
2022-04-19 20:22:35 +01:00
"""
# Free up hardware resources ahead of creating a new Encoder
gc.collect()
# Create an encoder on the 3 ADC pins, using PIO 0 and State Machine 0
PIN_A = 26 # The A channel pin
PIN_B = 28 # The B channel pin
PIN_C = 27 # The common pin
enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C)
# Uncomment the below line (and the top import) to reverse the counting direction
2022-05-12 16:15:40 +01:00
# enc.direction(REVERSED_DIR)
2022-04-19 20:22:35 +01:00
2022-05-12 14:45:20 +01:00
# The min and max value
MIN_VALUE = 0
2022-05-12 16:15:40 +01:00
MAX_VALUE = 100
2022-05-12 14:45:20 +01:00
2022-05-12 16:15:40 +01:00
value = 50
2022-05-12 14:45:20 +01:00
# Print out the initial value
print("Value =", value)
2022-04-19 20:22:35 +01:00
# Loop forever
while True:
2022-05-12 14:45:20 +01:00
delta = enc.delta()
if delta != 0:
if delta > 0:
value = min(value + 1, MAX_VALUE)
else:
value = max(value - 1, MIN_VALUE)
2022-04-19 20:22:35 +01:00
2022-05-12 14:45:20 +01:00
# Print out the new value
print("Value =", value)