from machine import Pin
import time
# Define the pins for the LEDs and the button
led_pins = [4, 5, 18, 19, 21, 22, 23]
button_pin = 15
# Set the pins to output mode and turn all LEDs off initially
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
for led in leds:
led.off()
# Set the button pin to input mode with pull-up resistor
button = Pin(button_pin, Pin.IN, Pin.PULL_UP)
# Define the colors for the LEDs
green = (0, 255, 0)
red = (255, 0, 0)
# Define the animation pattern
pattern = [green] * 3 + [red] + [green] * 3
# Define the starting pace and pace increment
pace = 0.5
pace_increment = 0.01
# Define the score variable and starting direction
score = 0
direction = 1 # 1 for left to right, -1 for right to left
# Define a function to delay without using time.sleep
def delay(ms):
start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) < ms:
pass
# Run the animation for a maximum of 40 cycles
for i in range(40):
# Update the pace if we've gone back and forth
if i > 0 and i % 2 == 0:
pace -= pace_increment
# Loop through the LEDs based on the direction
for j in range(7)[::direction]:
# Determine the color of the current LED
color = pattern[j % 4]
# Turn on the current LED
leds[j].on()
delay(int(pace * 1000))
# Check if the button was pressed during the green LED
if j % 4 == 0 and button.value() == 0:
score += 1
print("Score:", score)
# Turn off the current LED
leds[j].off()
delay(int(pace * 1000))
# Reverse the direction
direction *= -1