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)
]
# Timer states
RUNNING = 1
PAUSED = 0
state = PAUSED
# Previous timer values
previous_millis = 0
previous_pause_time = 0
interval = 0
# State variables
current_player = 0
timer_running = 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 start_timer():
global state, previous_millis, interval
state = RUNNING
previous_millis = time.ticks_ms()
interval = timer_duration * 1000 / LED_COUNT
print("Timer started")
# Initialize all LEDs to green at the start
np.fill((0, 255, 0)) # Green
np.write()
def pause_timer():
global state, paused_led_state
if state == RUNNING:
state = PAUSED
# Store the current state of LEDs
paused_led_state = [np[i] for i in range(LED_COUNT)]
print("Timer paused")
def resume_timer():
global state, paused_led_state
if state == PAUSED:
state = RUNNING
# Restore the LEDs to the paused state
for i in range(LED_COUNT):
np[i] = paused_led_state[i]
np.write()
print("Timer resumed")
def run_timer():
global current_player
for i in range(LED_COUNT):
if state == RUNNING:
np[i] = (0, 0, 0) # Turn off the LED to black
np.write()
time.sleep_ms(int(interval))
elif state == PAUSED:
while state == PAUSED:
time.sleep(0.1)
# 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
start_timer()
timer_running = True
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 state == RUNNING:
pause_timer()
elif state == PAUSED:
resume_timer()