"""Raspberry Pi Pico W LESSON 7: Controlling 3 LED with a Potentiometer in Micropythonhttps://www.youtube.com/watch?v=YqvcSnGd_HQ&list=PLGs0VKk2DiYz8js1SJog21cDhkBqyAhC5&index=7Raspberry Pi Pico W LESSON 8: Compound Conditionals and If Statements in MicroPythonhttps://www.youtube.com/watch?v=uTwm3ydI69w&list=PLGs0VKk2DiYz8js1SJog21cDhkBqyAhC5&index=8"""fromtimeimportsleepfrommachineimportPin,ADC# pylint: disable=import-error# Mapping pins by color/namePins:dict={'READ':28,# Potentiometer'GREEN':10,# Green LED'YELLOW':11,# Yellow LED'RED':12,# Red LED}# Mapping colors to potentiometer valuesCOLORS:dict={'GREEN':range(0,80),'YELLOW':range(80,95),'RED':range(95,101),}# Set up pin for potentiometerpotentiometer:ADC=ADC(Pins['READ'])
[docs]defall_led_off()->None:""" Turns all leds off :return: """forcolorinCOLORS:pin=Pin(Pins[color],Pin.OUT)pin.value(0)
[docs]defvalue_to_color(v_value)->str:""" Mapping pin name/color by potentiometer number COLORS[color] => range of integers :param v_value: :return: """new_color:str=''forcolor,valuesinCOLORS.items():ifv_valueinvalues:new_color=colorbreakreturnnew_color
[docs]defturn_led_on(color)->None:""" Turns LED on based on color/name :param color: :return: """pin:Pin=Pin(Pins[color],Pin.OUT)pin.value(1)
[docs]defconverter(read_value)->int:""" Converts potentiometer value to integer between 0 and 100 x min = 0, x max = 65535 y min = 0, y max = 100 :param read_value: :return: """slope:float=(100-0)/(65535-0)# calculate slopereturnint(slope*(read_value-0))# calculate Y and converted to integer value
if__name__=='__main__':# Main loopwhileTrue:all_led_off()# Turn off all LEDs# Read potentiometer value -> vV:int=potentiometer.read_u16()# pylint: disable=E1111# Convert potentiometer value into integer between 0 and 100VALUE:int=converter(V)LED_COLOR:str=value_to_color(VALUE)# Get color based on converted valueturn_led_on(LED_COLOR)# Turn ON corresponding LEDprint(f'value: {VALUE}, LED: {LED_COLOR}')# DEBUG outputsleep(0.25)# Sleep 0.25 seconds