from machine import Pin, PWM, ADC
import time

# Pin definitions
LED1_PIN = PWM(Pin(2))
LED2_PIN = PWM(Pin(3))
LED3_PIN = PWM(Pin(4))
RGB1_R_PIN = PWM(Pin(5))
RGB1_G_PIN = PWM(Pin(6))
RGB1_B_PIN = PWM(Pin(7))
RGB2_R_PIN = PWM(Pin(8))
RGB2_G_PIN = PWM(Pin(9))
RGB2_B_PIN = PWM(Pin(10))
POT1_PIN = ADC(Pin(26))
POT2_PIN = ADC(Pin(27))

# Initialize PWM frequency
LED1_PIN.freq(1000)
LED2_PIN.freq(1000)
LED3_PIN.freq(1000)
RGB1_R_PIN.freq(1000)
RGB1_G_PIN.freq(1000)
RGB1_B_PIN.freq(1000)
RGB2_R_PIN.freq(1000)
RGB2_G_PIN.freq(1000)
RGB2_B_PIN.freq(1000)

# In MicroPython, PWM duty cycle is from 0 to 65535
def map_value(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min

while True:
    # Read potentiometer values (0-65535) and scale to 0-1023 to match Arduino behavior
    pot1_value = map_value(POT1_PIN.read_u16(), 0, 65535, 0, 1023)
    pot2_value = map_value(POT2_PIN.read_u16(), 0, 65535, 0, 1023)
    
    # Map potentiometer values to LED brightness (0-65535 for PWM duty cycle)
    led1_brightness = map_value(pot1_value, 0, 1023, 0, 65535)
    led2_brightness = map_value(pot2_value, 0, 1023, 0, 65535)
    
    # Set LED3 brightness based on condition
    led3_brightness = 0
    if pot1_value + pot2_value == 2046:  # 1023 + 1023
        led3_brightness = 65535
    
    # Set LED brightness
    LED1_PIN.duty_u16(led1_brightness)
    LED2_PIN.duty_u16(led2_brightness)
    LED3_PIN.duty_u16(led3_brightness)
    
    # Map potentiometer values to RGB colors
    red = map_value(pot1_value, 0, 1023, 0, 65535)
    green = map_value(pot2_value, 0, 1023, 0, 65535)
    
    # Set blue based on condition
    blue = 0
    if pot1_value + pot2_value == 2046:  # 1023 + 1023
        blue = 65535
    
    # Set RGB LED colors
    RGB1_R_PIN.duty_u16(red)
    RGB1_G_PIN.duty_u16(green)
    RGB1_B_PIN.duty_u16(blue)
    RGB2_R_PIN.duty_u16(red)
    RGB2_G_PIN.duty_u16(green)
    RGB2_B_PIN.duty_u16(blue)
    
    # Short delay
    time.sleep_ms(20)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT