import machine
import utime
# GPIO setup for LEDs
led_pins = [machine.Pin(i, machine.Pin.OUT) for i in range(0, 10)] # GPIO 2 to 9
potentiometer = machine.ADC(28) # Analog pin A0 (GPIO 26)
# Function to clear all LEDs
def clear_leds():
for led in led_pins:
led.value(0)
# Function for back-and-forth running lights
def running_lights_back_and_forth():
direction = 1 # 1 for forward, -1 for reverse
index = 0 # Current LED index
while True:
# Read potentiometer value and map to delay (0.01 to 0.5 seconds)
pot_value = potentiometer.read_u16()
delay = 0.05 + (pot_value / 65535.0) * 0.5
# Turn on the current LED
led_pins[index].value(1)
utime.sleep(delay)
# Turn off the current LED
led_pins[index].value(0)
# Update index based on direction
index += direction
# Reverse direction if at either end
if index == len(led_pins) - 1: # Reached the last LED
direction = -1 # Change direction to reverse
elif index == 0: # Reached the first LED
direction = 1 # Change direction to forward
try:
running_lights_back_and_forth()
except KeyboardInterrupt:
clear_leds()
print("Program stopped.")