Here is my code. I use an encoder wheel breakout as my controller. Rotating the wheel adjusts the brightness up or down.
import time
import plasma
import machine
import micropython
from plasma import plasma_stick
from pimoroni_i2c import PimoroniI2C
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
from breakout_encoder_wheel import BreakoutEncoderWheel, UP, DOWN, LEFT, RIGHT, CENTRE, NUM_BUTTONS, NUM_LEDS
BUTTON_NAMES = ["Up", "Down", "Left", "Right", "Centre"]
i2c = PimoroniI2C(plasma_stick.SDA, plasma_stick.SCL)
wheel = BreakoutEncoderWheel(i2c)
#position = 0
last_pressed = [False] * NUM_BUTTONS
pressed = [False] * NUM_BUTTONS
GRB_LEDS = 144
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(GRB_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_GRB)
led_strip.start()
brightness = (0.5)
red = (1 / 360)
green = (130 / 360)
blue = (250 / 360)
yellow = (60 / 360)
orange = (30 / 360)
white = (1.0)
while True:
# Read all of the encoder wheel's buttons
for b in range(NUM_BUTTONS):
pressed[b] = wheel.pressed(b)
if pressed[b] != last_pressed[b]:
print(BUTTON_NAMES[b], "Pressed" if pressed[b] else "Released")
last_pressed[b] = pressed[b]
if pressed[UP]:
wheel.clear()
for i in range(NUM_LEDS):
wheel.set_hsv(i, 1.0, 0, brightness)
for s in range(GRB_LEDS):
led_strip.set_hsv(s, 1.0, 0, brightness)
wheel.set_hsv(0, 1.0, 0, brightness)
wheel.set_hsv(6, red, 1.0, brightness)
wheel.set_hsv(12, green, 1.0, brightness)
wheel.set_hsv(18, blue, 1.0, brightness)
color = white
if pressed[RIGHT]:
wheel.clear()
for i in range(NUM_LEDS):
wheel.set_hsv(i, red, 1.0, brightness)
for s in range(GRB_LEDS):
led_strip.set_hsv(s, red, 1.0, brightness)
wheel.set_hsv(0, 1.0, 0, brightness)
wheel.set_hsv(6, red, 1.0, brightness)
wheel.set_hsv(12, green, 1.0, brightness)
wheel.set_hsv(18, blue, 1.0, brightness)
color = red
if pressed[DOWN]:
wheel.clear()
for i in range(NUM_LEDS):
wheel.set_hsv(i, green, 1.0, brightness)
for s in range(GRB_LEDS):
led_strip.set_hsv(s, green, 1.0, brightness)
wheel.set_hsv(0, 1.0, 0, brightness)
wheel.set_hsv(6, red, 1.0, brightness)
wheel.set_hsv(12, green, 1.0, brightness)
wheel.set_hsv(18, blue, 1.0, brightness)
color = green
if pressed[LEFT]:
wheel.clear()
for i in range(NUM_LEDS):
wheel.set_hsv(i, blue, 1.0, brightness)
for s in range(GRB_LEDS):
led_strip.set_hsv(s, blue, 1.0, brightness)
wheel.set_hsv(0, 1.0, 0, brightness)
wheel.set_hsv(6, red, 1.0, brightness)
wheel.set_hsv(12, green, 1.0, brightness)
wheel.set_hsv(18, blue, 1.0, brightness)
color = blue
if pressed[CENTRE]:
wheel.clear()
for s in range(GRB_LEDS):
led_strip.set_hsv(s, 0, 0, 0)
# Has the dial been turned since the last time we checked?
change = wheel.delta()
if change != 0:
# Print out the direction the dial was turned, and the count
if change > 0:
print("Clockwise, Count =", wheel.count())
else:
print("Counter Clockwise, Count =", wheel.count())
# Record the new position (from 0 to 23)
position = wheel.count()
'''
if position < 1:
position = 1
if position > 25:
position = 25
'''
print ("Position =", position)
brightness = ((position + 25) / 50)
if brightness < 0:
brightness = 0
if brightness > 1:
brightness = 1
print ("Brightness =", brightness)
wheel.clear()
if color == white:
for i in range(NUM_LEDS):
wheel.set_hsv(i, 1.0, 0, brightness)
for s in range(GRB_LEDS):
led_strip.set_hsv(s, 1.0, 0, brightness)
else:
for i in range(NUM_LEDS):
wheel.set_hsv(i, color, 1.0, brightness)
for s in range(GRB_LEDS):
led_strip.set_hsv(s, color, 1.0, brightness)
wheel.set_hsv(0, 1.0, 0, brightness)
wheel.set_hsv(6, red, 1.0, brightness)
wheel.set_hsv(12, green, 1.0, brightness)
wheel.set_hsv(18, blue, 1.0, brightness)
wheel.show()