From 48ed5c156ae2013aac89b97d6c000d4a69b98df1 Mon Sep 17 00:00:00 2001 From: thirdr Date: Fri, 29 Nov 2024 12:00:55 +0000 Subject: [PATCH] Examples: adding button_control.py --- .../examples/plasma_2350/button_control.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 micropython/examples/plasma_2350/button_control.py diff --git a/micropython/examples/plasma_2350/button_control.py b/micropython/examples/plasma_2350/button_control.py new file mode 100644 index 0000000..7282917 --- /dev/null +++ b/micropython/examples/plasma_2350/button_control.py @@ -0,0 +1,48 @@ +''' +An example that uses the buttons to control the colour and brightness of the attached LED strip. + +Button A - Change the hue +BOOT/User button - Cycle through brightness. 10 levels of brightness available. + +''' +import time + +import machine +import plasma +from plasma import plasma2040 + +# Set how many LEDs you have +NUM_LEDS = 66 + +# Setup for the button pins +user_button = machine.Pin(22, machine.Pin.IN) +a_button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP) + +# WS2812 / NeoPixelâ„¢ LEDs +led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT, color_order=plasma.COLOR_ORDER_BGR) + +# Start updating the LED strip +led_strip.start() + +hue = 0.0 +brightness = 1.0 + +while True: + + if a_button.value() == 0: + hue += float(10) / 2000.0 + time.sleep(0.02) + + if user_button.value() == 0: + if brightness >= 0.0 and brightness < 1.0: + brightness += 0.1 + else: + brightness = 0.0 + + # Wait until the user releases the button + while user_button.value() == 0: + pass + + # Set each LED in the strip to the colour we want + for i in range(NUM_LEDS): + led_strip.set_hsv(i, hue, 1.0, brightness)