import uasyncio as asyncio
from machine import Pin, PWM
import time
# Setup for relay
relay_pin = Pin(16, Pin.OUT) # Relay connected to GPIO 16
# Setup for PWM on GPIO 13, 14, and 15 (left to right arrangement)
pwm_pin_13 = Pin(13, Pin.OUT) # PWM on GPIO 13
pwm_pin_14 = Pin(14, Pin.OUT) # PWM on GPIO 14
pwm_pin_15 = Pin(15, Pin.OUT) # PWM on GPIO 15
# Create PWM objects for the three pins
pwm_13 = PWM(pwm_pin_13)
pwm_14 = PWM(pwm_pin_14)
pwm_15 = PWM(pwm_pin_15)
# Set PWM frequency for all three pins
pwm_13.freq(750)
pwm_14.freq(750)
pwm_15.freq(750)
# Relay flashing function
async def relay_flash():
while True:
relay_pin.high() # Set GPIO 16 high (relay on)
await asyncio.sleep(0.5) # Wait for 0.5 seconds
relay_pin.low() # Set GPIO 16 low (relay off)
await asyncio.sleep(0.5) # Wait for 0.5 seconds
# PWM cascading function (for GPIO 15, 14, 13, then reverse)
async def pwm_cascade():
while True:
# Fade in sequentially: 15 -> 14 -> 13
for dc in range(0, 65536, 5000): # Increase duty cycle from 0 to 65535
pwm_15.duty_u16(dc) # Fade GPIO 15 first
await asyncio.sleep(0.05) # Wait for each step
for dc in range(0, 65536, 5000): # Increase duty cycle for GPIO 14
pwm_14.duty_u16(dc) # Fade GPIO 14 after GPIO 15
await asyncio.sleep(0.05) # Wait for each step
for dc in range(0, 65536, 5000): # Increase duty cycle for GPIO 13
pwm_13.duty_u16(dc) # Fade GPIO 13 after GPIO 14
await asyncio.sleep(0.05) # Wait for each step
# Fade out sequentially: 13 -> 14 -> 15
for dc in range(65535, -1, -5000): # Decrease duty cycle for GPIO 13
pwm_13.duty_u16(dc) # Fade GPIO 13 first
await asyncio.sleep(0.05) # Wait for each step
for dc in range(65535, -1, -5000): # Decrease duty cycle for GPIO 14
pwm_14.duty_u16(dc) # Fade GPIO 14 after GPIO 13
await asyncio.sleep(0.05) # Wait for each step
for dc in range(65535, -1, -5000): # Decrease duty cycle for GPIO 15
pwm_15.duty_u16(dc) # Fade GPIO 15 last
await asyncio.sleep(0.05) # Wait for each step
# Ensure all LEDs are fully off
pwm_13.duty_u16(0) # Set GPIO 13 to completely off
pwm_14.duty_u16(0) # Set GPIO 14 to completely off
pwm_15.duty_u16(0) # Set GPIO 15 to completely off
await asyncio.sleep(0.5) # Wait for a moment before starting again
# Main function to run both tasks in parallel
async def main():
# Run both tasks in parallel
await asyncio.gather(
relay_flash(), # Flash the relay connected to GPIO 16
pwm_cascade() # Control the cascading effect on GPIO 15, 14, and 13
)
# Run the main function using asyncio
asyncio.run(main())