import machine
import time
# --- Setup LED bar graph output pins ---
# Map Arduino Mega digital pins 2-11 to Pico GP2-GP11.
led_pins = {}
for arduino_pin in range(2, 12): # 2 to 11 inclusive
pico_pin = getattr(machine.Pin, 'GPIO') # not used directly, we instead build the string below
# Here we assume a simple mapping: Arduino pin 'n' becomes Pico GPn.
led_pins[arduino_pin] = machine.Pin("GP" + str(arduino_pin), machine.Pin.OUT)
led_pins[arduino_pin].value(0) # initialize LOW
# --- Setup ADC for slide potentiometers ---
# Pico analog inputs: GP26, GP27, GP28 correspond to ADC channels 0, 1, 2 respectively.
pot1_adc = machine.ADC(26) # pot1 connected to GP26
pot2_adc = machine.ADC(27) # pot2 connected to GP27
pot3_adc = machine.ADC(28) # pot3 connected to GP28
# --- Helper function: map value from one range to another ---
def map_value(x, in_min, in_max, out_min, out_max):
# Note: using integer math similar to Arduino's map function.
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
# --- Function to update an LED section ---
def update_section(start_pin, end_pin, active_leds):
# For the given section (Arduino numbering: start_pin to end_pin)
# turn on the first 'active_leds' outputs, and turn off the rest.
for pin in range(start_pin, end_pin + 1):
# Calculate whether this led should be ON or OFF.
# (pin - start_pin) < active_leds means it is within the active count.
if (pin - start_pin) < active_leds:
led_pins[pin].value(1)
else:
led_pins[pin].value(0)
# --- Main loop ---
while True:
# Read each potentiometer; convert 16-bit ADC value to a 10-bit equivalent (0-1023)
pot1_value = pot1_adc.read_u16() >> 6 # Right shift by 6 bits -> 16-bit to ~10-bit resolution
pot2_value = pot2_adc.read_u16() >> 6
pot3_value = pot3_adc.read_u16() >> 6
# Map potentiometer value into number of LEDs for each section.
# Section 1 and 2: map 0-1023 to range 0-3.
leds_section1 = map_value(pot1_value, 0, 1023, 0, 3)
leds_section2 = map_value(pot2_value, 0, 1023, 0, 3)
# Section 3: map 0-1023 to range 0-4.
leds_section3 = map_value(pot3_value, 0, 1023, 0, 4)
# Update LED bar graph sections.
# Section 1 corresponds to Arduino pins 2 to 4 (Pico GP2-GP4)
update_section(2, 4, leds_section1)
# Section 2 corresponds to Arduino pins 5 to 7 (Pico GP5-GP7)
update_section(5, 7, leds_section2)
# Section 3 corresponds to Arduino pins 8 to 11 (Pico GP8-GP11)
update_section(8, 11, leds_section3)
time.sleep(0.05) # Add a small delay to ease CPU utilization