import machine
import neopixel
import time
# Pin configurations
PIN = 5
LED_COUNT = 16
POTENTIOMETER_PIN = 2
BUTTON_PIN = 13
POT_MAX_VALUE = 4095
# Initialization
np = neopixel.NeoPixel(machine.Pin(PIN), LED_COUNT)
pot = machine.ADC(machine.Pin(POTENTIOMETER_PIN))
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
# Define the colors for up to 8 players
SEGMENT_COLORS = [
(0, 255, 0), (0, 0, 255), (255, 165, 0), (255, 0, 0),
(255, 255, 0), (75, 0, 130), (255, 20, 147), (0, 255, 255)
]
# State variables
current_player = 0
timer_running = False
paused = False
mode = 0 # 0: Set player count, 1: Set timer duration, 2: Running timer
player_count = 2
timer_duration = 10 # Default to 10 seconds
def display_player_count():
player_count = get_player_count()
display_segments(player_count)
def display_timer_duration():
timer_duration = get_timer_duration()
update_leds_for_duration(timer_duration)
def get_player_count():
pot_value = pot.read()
count = max(2, round((pot_value / POT_MAX_VALUE) * (len(SEGMENT_COLORS) - 1)) + 1)
return count
def get_timer_duration():
pot_value = pot.read()
duration = max(10, round((pot_value / POT_MAX_VALUE) * 60)) # 10 to 60 seconds
return duration
def display_segments(player_count):
segment_length = LED_COUNT // player_count
extra_leds = LED_COUNT % player_count
led_index = 0
for i in range(player_count):
for _ in range(segment_length + (1 if i < extra_leds else 0)):
np[led_index] = SEGMENT_COLORS[i % len(SEGMENT_COLORS)]
led_index += 1
for i in range(led_index, LED_COUNT):
np[i] = (0, 0, 0)
np.write()
def update_leds_for_duration(duration):
led_count = round(duration / 60 * LED_COUNT)
for i in range(LED_COUNT):
if i < led_count:
np[i] = (255, 255, 255) # White when active
else:
np[i] = (0, 0, 0)
np.write()
def run_timer(player_color, duration):
global timer_running, paused
timer_running = True
paused = False
np.fill(player_color)
np.write()
led_interval = duration / LED_COUNT
for i in reversed(range(LED_COUNT)):
if not paused:
np[i] = (0, 0, 0)
np.write()
time.sleep(led_interval)
else:
while paused:
time.sleep(0.1) # Check every 0.1 second for pause flag
if not timer_running: # If timer was stopped during pause
return
timer_running = False
np.fill((0, 0, 0))
np.write()
# Initial display for player count
display_player_count()
while True:
if not timer_running:
if mode == 0:
display_player_count()
elif mode == 1:
display_timer_duration()
if button.value() == 0:
time.sleep(0.05) # Debounce
if button.value() == 0:
while button.value() == 0:
time.sleep(0.01) # Wait for button release
mode += 1
if mode > 2:
mode = 0
if mode == 2: # Start timer
run_timer(SEGMENT_COLORS[current_player], timer_duration)
current_player = (current_player + 1) % player_count
else: # Timer is running
if button.value() == 0:
time.sleep(0.05) # Debounce
if button.value() == 0:
while button.value() == 0:
time.sleep(0.01) # Wait for button release
if paused:
paused = False
else:
paused = True