Commit bf318801 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

examples/hwapi: Add uasyncio example of fading 2 LEDs in parallel.

parent 8212773a
......@@ -15,5 +15,8 @@ from machine import Pin
# User LED 1 on gpio21
LED = Pin(21, Pin.OUT)
# User LED 2 on gpio120
LED2 = Pin(120, Pin.OUT)
# Button S3 on gpio107
BUTTON = Pin(107, Pin.IN)
# Like, but fading 2 LEDs with different phase.
# Also see original
import uasyncio
from hwconfig import LED, LED2
async def pwm_cycle(led, duty, cycles):
duty_off = 20 - duty
for i in range(cycles):
if duty:
await uasyncio.sleep_ms(duty)
if duty_off:
await uasyncio.sleep_ms(duty_off)
async def fade_in_out(LED):
while True:
# Fade in
for i in range(1, 21):
await pwm_cycle(LED, i, 2)
# Fade out
for i in range(20, 0, -1):
await pwm_cycle(LED, i, 2)
loop = uasyncio.get_event_loop()
loop.call_later_ms_(800, fade_in_out(LED2))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment