import time
import board
from rainbowio import colorwheel
import neopixel
import analogio #library to read analog values
import pwmio #library to write analog values
import digitalio

#neopixel
pixel_pin = board.NEOPIXEL #The neopixel can be accessed in this way
num_pixels = 1 #only one pixel

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)


#set pin number and direction for each led color
REDled = pwmio.PWMOut(board.D7, duty_cycle=0, frequency=5000)
GREENled = pwmio.PWMOut(board.D6, duty_cycle=0, frequency=5000)
BLUEled = pwmio.PWMOut(board.D0, duty_cycle=0, frequency=5000)

#analog pins for each potenciometer
REDpot = analogio.AnalogIn(board.A1) # Initialize A1 as analog Input
GREENpot = analogio.AnalogIn(board.A2) # Initialize A1 as analog Input
BLUEpot = analogio.AnalogIn(board.A3) # Initialize A1 as analog Input

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

#-----------------------------------------------------------------------------------

while True:
    #apply the potenciometer values to the variables and adjust their values to go from 0 to 255
    REDpwm = int((REDpot.value))-1 # Read and save an analog reading for color red
    GREENpwm = int((GREENpot.value))-1 # Read and save an analog reading for color green
    BLUEpwm = int((BLUEpot.value))-1 # Read and save an analog reading for color blue

    #turn on each led with their corresponding pwm
    REDled.duty_cycle=REDpwm
    GREENled.duty_cycle=GREENpwm
    BLUEled.duty_cycle=BLUEpwm

    REDpwm = int((REDpot.value)/255)-1
    GREENpwm = int((GREENpot.value)/255)-1
    BLUEpwm = int((BLUEpot.value)/255)-1

    #set the color to the neo pixel
    pixels.fill((REDpwm,GREENpwm,BLUEpwm),)
    pixels.show()

    #To verify what's happenning in the pwm
    print(f"red: {REDpwm}, green: {GREENpwm}, blue: {BLUEpwm}")
    time.sleep(0.5)