Encoder LED Brightness example script (arduino.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(); analogWrite(), map(), constrain()
from arduino import *
import analog_shield as shield
GREEN_LED = 6
STEPS = 20
knob = None
last = None
def setup():
global knob
knob = shield.encoder()
analogWrite(GREEN_LED, 255)
def loop():
global last
clicks = constrain(knob.position(), 0, STEPS)
knob.set_position(clicks)
analogWrite(GREEN_LED, 255 - map(clicks, 0, STEPS, 0, 255))
percent = map(clicks, 0, STEPS, 0, 100)
if percent != last:
print(percent, "%")
last = percent
delay(10)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()