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 ...@@ -15,5 +15,8 @@ from machine import Pin
# User LED 1 on gpio21 # User LED 1 on gpio21
LED = Pin(21, Pin.OUT) LED = Pin(21, Pin.OUT)
# User LED 2 on gpio120
LED2 = Pin(120, Pin.OUT)
# Button S3 on gpio107 # Button S3 on gpio107
BUTTON = Pin(107, Pin.IN) BUTTON = Pin(107, Pin.IN)
# Like soft_pwm_uasyncio.py, but fading 2 LEDs with different phase.
# Also see original soft_pwm.py.
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:
led.value(1)
await uasyncio.sleep_ms(duty)
if duty_off:
led.value(0)
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.create_task(fade_in_out(LED))
loop.call_later_ms_(800, fade_in_out(LED2))
loop.run_forever()
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