'''
#Method -1
from RGB import RGB_COLORS
from machine import Pin, PWM
RED_GPIO = 14
GREEN_GPIO = 13
BLUE_GPIO = 12
red_pwm = PWM(Pin(RED_GPIO))
green_pwm = PWM(Pin(GREEN_GPIO))
blue_pwm = PWM(Pin(BLUE_GPIO))
for pwm in (red_pwm, green_pwm, blue_pwm):
pwm.freq(1000)
def set_color(index):
r, g, b = RGB_COLORS[index]
# INVERT PWM for common anode
red_pwm.duty_u16(65535 - int(r * 65535 / 255))
green_pwm.duty_u16(65535 - int(g * 65535 / 255))
blue_pwm.duty_u16(65535 - int(b * 65535 / 255))
while True:
print("""
Choose color:
0 - RED
1 - GREEN
2 - BLUE
3 - YELLOW
4 - CYAN
5 - MAGENTA
6 - WHITE
7 - OFF
""")
choice = int(input("Enter color number: "))
if 0 <= choice < len(RGB_COLORS):
set_color(choice)
else:
print("Invalid number")
''''
#Method 2
from machine import Pin, PWM
from RGB import *
# ================= RGB TYPE =================
# Set this correctly
# True = COMMON ANODE (most lab RGB LEDs)
# False = COMMON CATHODE
COMMON_ANODE = True
# ============================================
# GPIO pins (CHANGE ONLY IF NEEDED)
RED_PIN = 14
GREEN_PIN = 13
BLUE_PIN = 12
# PWM setup
red = PWM(Pin(RED_PIN))
green = PWM(Pin(GREEN_PIN))
blue = PWM(Pin(BLUE_PIN))
for led in (red, green, blue):
led.freq(1000)
# Convert 0–255 to PWM
def pwm_value(value):
duty = int(value * 65535 / 255)
if COMMON_ANODE:
return 65535 - duty
else:
return duty
# Set RGB color
def set_color(color):
r, g, b = rgb[color]
red.duty_u16(pwm_value(r))
green.duty_u16(pwm_value(g))
blue.duty_u16(pwm_value(b))
# Main loop
try:
while True:
color = input("What Color Do You Desire: ").lower()
if color in rgb:
set_color(color)
print("Color:", color)
else:
print("Invalid! Available:", list(rgb.keys()))
except KeyboardInterrupt:
set_color("off")
print("\nStopped, LED OFF")