Encoder LED Brightness example script (phyduino.py) Sets the green LED's brightness with the rotary encoder: 20 clicks from off to full. As the LED lights when D6 is low, the PWM sets the time it is off.
- Connect
- JP3 (VIO) on 3.3 V.
- Uses
- encoder(): Encoder.position(), Encoder.set_position(); mcu.pwm()
import time
import analog_shield as shield
import phyduino as mcu
STEPS = 20
knob = shield.encoder()
led = mcu.pwm("D6", freq=1000, duty_u16=65535)
last = None
while True:
clicks = min(max(knob.position(), 0), STEPS)
knob.set_position(clicks)
led.duty_u16(65535 - clicks * 65535 // STEPS)
percent = clicks * 100 // STEPS
if percent != last:
print(percent, "%")
last = percent
time.sleep_ms(10)