from machine import Pin, PWM
from utime import sleep
import urandom
# Set up the LED on GPIO pin 5 as a PWM object to control brightness
LED_PIN = 5
led = PWM(Pin(LED_PIN))
led.freq(1000) # Set frequency to 1kHz for smooth brightness control
# Function to simulate a single firework burst with flashes and fade-out
def firework_burst():
"""
Simulates a single firework burst with random flashes and a gradual fade-out.
"""
# Random burst flash count and flash speed
flashes = urandom.randint(3, 8)
for _ in range(flashes):
# Random brightness level for each flash
brightness = urandom.randint(30000, 65535)
led.duty_u16(brightness) # Set LED brightness to simulate flash
sleep(urandom.uniform(0.05, 0.2)) # Short random flash duration
led.duty_u16(0) # Turn LED off between flashes
sleep(urandom.uniform(0.05, 0.15)) # Short random interval between flashes
# Gradual fade-out after flashes to simulate the fading trail of the firework
fade_out()
# Function to gradually decrease LED brightness (fade-out effect)
def fade_out():
"""
Gradually decreases LED brightness to simulate a fading firework trail.
"""
for duty in range(30000, 0, -500): # Step down brightness for smooth fade
led.duty_u16(duty)
sleep(0.01) # Small delay for smooth transition
# Function to simulate an ongoing fireworks show with multiple bursts
def fireworks_show(cycles):
"""
Simulates an ongoing fireworks show by triggering multiple firework bursts.
:param cycles: Number of bursts in the fireworks show
"""
for _ in range(cycles):
firework_burst() # Trigger a single burst
sleep(urandom.uniform(1, 3)) # Random delay between each burst
# Start the fireworks show
fireworks_show(cycles=10) # Run the fireworks show with 10 bursts