from machine import Pin
import utime
# configure pins for LEDs and button
led_pins = [Pin(5, Pin.OUT), Pin(18, Pin.OUT), Pin(19, Pin.OUT), Pin(21, Pin.OUT), Pin(22, Pin.OUT), Pin(23, Pin.OUT), Pin(25, Pin.OUT)]
button_pin = Pin(4, Pin.IN, Pin.PULL_UP)
# set middle LED to red
led_pins[3].value(1)
led_colors = [0, 0, 0, 1, 0, 0, 0]
# define delay function without sleep
def delay(ms):
start = utime.ticks_ms()
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
pass
# initialize animation variables
direction = 1
delay_ms = 500
score = 0
# main animation loop
for i in range(40):
# turn on LEDs in sequence
for j in range(7):
led_pins[j].value(led_colors[j])
delay(delay_ms)
# check button press during delay
if button_pin.value() == 1 and led_colors[j] == 0:
score += 1
print("Score:", score)
# reverse direction after reaching end of sequence
if direction == 1 and j == 6:
direction = -1
delay_ms -= 10
elif direction == -1 and j == 0:
direction = 1
delay_ms -= 10
# update LED colors for next iteration
led_colors = [0] + led_colors[:-1] if direction == 1 else led_colors[1:] + [0]