from machine import Pin
from utime import sleep
rgb_r = Pin(4, Pin.OUT)
rgb_g = Pin(3, Pin.OUT)
rgb_b = Pin(2, Pin.OUT)
#Define colors as [R, G, B] - 1-ON, 0-OFF
color_list = [
(1, 0, 0), # Red
(0, 1, 0), # Green
(0, 0, 1), # Blue
(1, 1, 0), # Yellow
(0, 1, 1), # Cyan
(1, 0, 1), # Magenta / Pink
(1, 1, 1), # White
(0, 0, 0) # Off
]
# Function to apply values to the output pins
i = 0
while True:
r, g, b = color_list[i]
rgb_r.value(r)
rgb_g.value(g)
rgb_b.value(b)
# Iterate through the color list
i = (i + 1) % len(color_list) # Move to next color, loop back
sleep(0.5)
# # Another Code by using PWM
# from machine import Pin, PWM
# from time import sleep
# # Setup PWM Pins
# rgb_r = PWM(Pin(4))
# rgb_g = PWM(Pin(3))
# rgb_b = PWM(Pin(2))
# # Set PWM Frequency
# for pin in (rgb_r, rgb_g, rgb_b):
# pin.freq(1000)
# # List of colors to cycle through (R, G, B) — values 0-65535
# color_list = [
# (65535, 0, 0), # Red
# (0, 65535, 0), # Green
# (0, 0, 65535), # Blue
# (65535, 65535, 0), # Yellow
# (0, 65535, 65535), # Cyan
# (65535, 0, 65535), # Magenta
# (65535, 65535, 65535), # White
# (0, 0, 0) # Off
# ]
# i = 0
# def set_color(r, g, b):
# rgb_r.duty_u16(r)
# rgb_g.duty_u16(g)
# rgb_b.duty_u16(b)
# while True:
# r, g, b = color_list[i]
# set_color(r, g, b)
# print(f"Color: {i} -> RGB({r}, {g}, {b})")
# i = (i + 1) % len(color_list) # Move to next color, loop back
# sleep(0.5)