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 each player segment
SEGMENT_COLORS = [(0, 255, 0), (0, 0, 255), (255, 165, 0), (255, 0, 0)] # Green, Blue, Orange, Red
# State variables
current_player = 0
timer_running = False
paused = False
timer_finished = False
timer_duration = 10
setting_duration = False
def get_player_count():
pot_value = pot.read()
player_count = round(pot_value / POT_MAX_VALUE * (len(SEGMENT_COLORS) - 1)) + 1
return player_count
def display_segments(player_count):
segment_length = LED_COUNT // len(SEGMENT_COLORS)
for i in range(LED_COUNT):
segment_index = i // segment_length
np[i] = SEGMENT_COLORS[segment_index] if segment_index < player_count else (0, 0, 0)
np.write()
def scale_pot_value_to_led_count():
pot_value = pot.read()
timer_duration = round((pot_value / POT_MAX_VALUE) * 50) + 10 # 10 to 60 seconds
led_count = round(((timer_duration - 10) / 50) * LED_COUNT)
return led_count, timer_duration
def update_leds_for_duration_setting():
led_count, _ = scale_pot_value_to_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):
global timer_running, paused, timer_duration, current_player, timer_finished
_, timer_duration = scale_pot_value_to_led_count()
timer_running = True
paused = False
timer_finished = False
np.fill(player_color)
np.write()
led_interval = timer_duration / LED_COUNT
for i in reversed(range(LED_COUNT)):
np[i] = (0, 0, 0)
np.write()
time.sleep(led_interval)
# Check for button press to pause/resume
if button.value() == 0:
time.sleep(0.01) # Debounce for 10ms
if button.value() == 0:
paused = not paused
while button.value() == 0:
time.sleep(0.01) # Wait for button release
while paused:
time.sleep(0.01) # Check button state every 10ms to resume
if button.value() == 0:
time.sleep(0.01)
if button.value() == 0:
paused = not paused
while button.value() == 0:
time.sleep(0.01)
timer_running = False
np.fill((0, 0, 0))
np.write()
timer_finished = True
# Initial setup for player count and duration
player_count = get_player_count()
display_segments(player_count)
while True:
if not timer_running and not setting_duration:
if button.value() == 0:
time.sleep(0.05) # Debounce for 50ms
if button.value() == 0:
setting_duration = True
if setting_duration:
update_leds_for_duration_setting()
if button.value() == 0:
time.sleep(0.05) # Debounce for 50ms
if button.value() == 0:
while button.value() == 0:
time.sleep(0.01)
setting_duration = False
player_color = SEGMENT_COLORS[current_player]
run_timer(player_color)
if timer_finished:
# Display the next player's color
current_player = (current_player + 1) % len(SEGMENT_COLORS)
player_color = SEGMENT_COLORS[current_player]
np.fill(player_color)
np.write()
timer_finished = False
# Wait for button press to start the next timer