import machine
import time
# Pin definitions
LED_PIN = 2
RGB1_R = 3
RGB1_G = 4
RGB1_B = 5
RGB2_R = 6
RGB2_G = 7
RGB2_B = 8
RGB3_R = 9
RGB3_G = 10
RGB3_B = 11
# Timing constants
COLOR_DURATION = 2000
BLINK_INTERVAL = 2000
# Initialize variables
last_blink_time = 0
last_color_change_time = 0
led_state = False
current_color_index = 0
# Define colors (R, G, B)
colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(0, 255, 255), # Cyan
(255, 0, 255), # Magenta
(255, 255, 255) # White
]
# Initialize pins
led = machine.Pin(LED_PIN, machine.Pin.OUT)
# Initialize PWM for RGB LEDs
rgb1_r_pwm = machine.PWM(machine.Pin(RGB1_R))
rgb1_g_pwm = machine.PWM(machine.Pin(RGB1_G))
rgb1_b_pwm = machine.PWM(machine.Pin(RGB1_B))
rgb2_r_pwm = machine.PWM(machine.Pin(RGB2_R))
rgb2_g_pwm = machine.PWM(machine.Pin(RGB2_G))
rgb2_b_pwm = machine.PWM(machine.Pin(RGB2_B))
rgb3_r_pwm = machine.PWM(machine.Pin(RGB3_R))
rgb3_g_pwm = machine.PWM(machine.Pin(RGB3_G))
rgb3_b_pwm = machine.PWM(machine.Pin(RGB3_B))
# Set PWM frequency
rgb1_r_pwm.freq(1000)
rgb1_g_pwm.freq(1000)
rgb1_b_pwm.freq(1000)
rgb2_r_pwm.freq(1000)
rgb2_g_pwm.freq(1000)
rgb2_b_pwm.freq(1000)
rgb3_r_pwm.freq(1000)
rgb3_g_pwm.freq(1000)
rgb3_b_pwm.freq(1000)
def set_rgb_color(red, green, blue):
# Convert 0-255 values to 0-65535 for PWM duty cycle
red_duty = int((red / 255) * 65535)
green_duty = int((green / 255) * 65535)
blue_duty = int((blue / 255) * 65535)
# Set PWM duty cycles for all RGB LEDs
rgb1_r_pwm.duty_u16(red_duty)
rgb1_g_pwm.duty_u16(green_duty)
rgb1_b_pwm.duty_u16(blue_duty)
rgb2_r_pwm.duty_u16(red_duty)
rgb2_g_pwm.duty_u16(green_duty)
rgb2_b_pwm.duty_u16(blue_duty)
rgb3_r_pwm.duty_u16(red_duty)
rgb3_g_pwm.duty_u16(green_duty)
rgb3_b_pwm.duty_u16(blue_duty)
# Initial color setup
set_rgb_color(*colors[current_color_index])
# Main loop
while True:
current_time = time.ticks_ms()
# Check if it's time to blink the LED
if time.ticks_diff(current_time, last_blink_time) >= BLINK_INTERVAL:
led_state = not led_state
led.value(led_state)
last_blink_time = current_time
# Check if it's time to change colors
if time.ticks_diff(current_time, last_color_change_time) >= COLOR_DURATION:
current_color_index = (current_color_index + 1) % len(colors)
set_rgb_color(*colors[current_color_index])
last_color_change_time = current_time