from machine import Pin, ADC, PWM
import time
# Set up the LED pins for RGB (Physical pins on the Raspberry Pi Pico)
redPin = Pin(0, Pin.OUT) # Red LED pin (Pin 0)
greenPin = Pin(1, Pin.OUT) # Green LED pin (Pin 1)
bluePin = Pin(2, Pin.OUT) # Blue LED pin (Pin 2)
# Set up ADC for potentiometers (green and blue)
potentiometer_green = ADC(Pin(26)) # GPIO 26 for Green potentiometer (ADC0)
potentiometer_blue = ADC(Pin(27)) # GPIO 27 for Blue potentiometer (ADC1)
# Set up PWM for controlling LED brightness
red_pwm = PWM(redPin)
green_pwm = PWM(greenPin)
blue_pwm = PWM(bluePin)
# Set the frequency of the PWM
red_pwm.freq(1000)
green_pwm.freq(1000)
blue_pwm.freq(1000)
# Function to scale ADC reading to PWM duty cycle (0-65535 range for PWM)
def scale_adc_to_pwm(adc_value):
return int(adc_value / 65535 * 65535)
# Set the red LED to default intensity (full intensity)
red_pwm.duty_u16(65535) # Start with Red at full intensity (100%)
# Function to update the LED intensities
def update_led_intensity(red_value, green_value, blue_value):
red_pwm.duty_u16(red_value)
green_pwm.duty_u16(green_value)
blue_pwm.duty_u16(blue_value)
# Main loop
running = True
while running:
# Get potentiometer values (0-65535)
green_value = potentiometer_green.read_u16() # Use read_u16() instead of read()
blue_value = potentiometer_blue.read_u16() # Use read_u16() instead of read()
# Scale potentiometer values to PWM range (0-65535)
green_pwm_value = green_value
blue_pwm_value = blue_value
# Handle user input for red intensity
user_input = input("Enter red intensity (0-9, any other key to exit): ")
if user_input.isdigit() and 0 <= int(user_input) <= 9:
# Scale the red intensity (0-9) to 0-65535
red_value = int(user_input) * (65535 // 9) # Maps 0-9 to 0-65535
# Update LED intensities
update_led_intensity(red_value, green_pwm_value, blue_pwm_value)
else:
print("Exiting...")
running = False
# Sleep to give time for the program to run
time.sleep(0.1)
# Turn off LEDs before exiting
red_pwm.duty_u16(0)
green_pwm.duty_u16(0)
blue_pwm.duty_u16(0)