import time
from machine import Pin,PWM, ADC
 
# RGB
RED = 0
GREEN = 1
BLUE = 2

# Declare pins
pwm_pins = [18, 5, 17]
adc_pins = [32, 33, 35]  #adc pin
# Setup pins for ADC
adcs = [ADC(Pin(adc_pins[RED])),ADC(Pin(adc_pins[GREEN])),
                ADC(Pin(adc_pins[BLUE]))]
#Set up the PWM
pwms = [PWM(Pin(pwm_pins[RED])),PWM(Pin(pwm_pins[GREEN])),
                PWM(Pin(pwm_pins[BLUE]))]

# Set pwm frequency
[pwm.freq(1000) for pwm in pwms] 
[adc.atten(ADC.ATTN_11DB) for adc in adcs] 

def turn_off_rgb():
    pwms[RED].duty_u16(0)
    pwms[GREEN].duty_u16(0)
    pwms[BLUE].duty_u16(0)
    time.sleep(0.1)
    
# Deinitialize PWM on all pins
def deinit_pwm_pins():
    pwms[RED].deinit()
    pwms[GREEN].deinit()
    pwms[BLUE].deinit()
# main function
def main():
    while True:
        pwms[RED].duty_u16(6*adcs[RED].read())
        pwms[GREEN].duty_u16(6*adcs[GREEN].read())
        pwms[BLUE].duty_u16(6*adcs[BLUE].read())
        time.sleep(0.001)
            
if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        deinit_pwm_pins()