import board
import digitalio
import neopixel
import time
import random
# Ensure correct pin assignments (adjust as per your hardware setup)
button_pin = digitalio.DigitalInOut(board.D1) # Use a different pin for the button
button_pin.direction = digitalio.Direction.INPUT
button_pin.pull = digitalio.Pull.UP
# Define LED pins correctly (assuming these are the pins you want to use)
led_pins = [digitalio.DigitalInOut(pin) for pin in (board.D6, board.D7, board.D0)] # Adjust pins as needed
for led in led_pins:
led.direction = digitalio.Direction.OUTPUT
num_leds = len(led_pins)
target_led_index = 0 # Adjust as needed
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2, auto_write=False) # Use a pin that supports NeoPixel
level = 1 # Starting level
base_delay = 1.0 # Base delay time for level 1 in seconds
def show_neo_pixel_color(color):
pixels[0] = color
pixels.show()
def wheel(pos):
# Your wheel function seems fine; it generates colors
pos = 255 - pos
if pos < 85:
return (255 - pos * 3, 0, pos * 3)
elif pos < 170:
pos -= 85
return (0, pos * 3, 255 - pos * 3)
else:
pos -= 170
return (pos * 3, 255 - pos * 3, 0)
show_neo_pixel_color((0, 0, 255)) # Start with blue color indicating game start
time.sleep(1)
while True:
correct_press_detected = False
display_time = base_delay / level # Adjusted to ensure this is in seconds
led_display_duration = display_time / 2 # In seconds
for i in range(level):
led_to_show = random.randint(0, num_leds - 1)
led_pins[led_to_show].value = True # Turn on the selected LED
start_time = time.monotonic()
while time.monotonic() - start_time < led_display_duration:
if not button_pin.value: # Button press detected
correct_press_detected = (led_to_show == target_led_index)
while not button_pin.value:
pass # Wait for button release
break
led_pins[led_to_show].value = False # Turn off the selected LED
if correct_press_detected:
break
if correct_press_detected:
for color in range(256):
show_neo_pixel_color(wheel(color))
time.sleep(0.005)
level += 1
else:
show_neo_pixel_color((255, 0, 0)) # Indicate wrong press or timeout
time.sleep(2)