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)
initial_player_count = get_player_count()
display_segments(initial_player_count)
# 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)
]
SET_PLAYER_COUNT = 0
SET_TIMER_DURATION = 1
TIMER_RUNNING = 2
mode = SET_PLAYER_COUNT # Start with setting player count
player_count = 2
timer_duration = 10 # Default to 10 seconds
# 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
mode = SET_PLAYER_COUNT # Start with setting player count
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):
np.fill((0, 0, 0)) # Clear the LEDs first
segment_length = LED_COUNT // player_count
extra_leds = LED_COUNT % player_count
led_index = 0
for i in range(player_count):
color = SEGMENT_COLORS[i % len(SEGMENT_COLORS)]
for _ in range(segment_length + (1 if i < extra_leds else 0)):
np[led_index] = color
led_index += 1
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, last_update_time, leds_on
state = RUNNING
leds_on = LED_COUNT # Reset LEDs to full on start
last_update_time = time.ticks_ms()
np.fill((0, 255, 0)) # Set all LEDs to green
np.write()
print("Timer started")
def pause_timer():
global state
if state == RUNNING:
state = PAUSED
print("Timer paused")
def resume_timer():
global state
if state == PAUSED:
state = RUNNING
print("Timer resumed")
def update_timer():
global last_update_time, leds_on, state
current_time = time.ticks_ms()
if state == RUNNING and time.ticks_diff(current_time, last_update_time) >= (timer_duration * 1000 // LED_COUNT):
leds_on -= 1 # Decrease the number of LEDs on
for i in range(leds_on):
np[i] = (0, 255, 0) # Green for active LEDs
for i in range(leds_on, LED_COUNT):
np[i] = (0, 0, 0) # Turn off the rest
np.write()
last_update_time = current_time
if leds_on == 0:
state = PAUSED
print("Timer finished")
# Reset timer or trigger an event after finish
def button_press():
global mode, player_count, timer_duration
if not button.value(): # Check if the button is pressed
time.sleep(0.05) # Debounce delay
if not button.value(): # Confirm button press
while not button.value(): # Wait until the button is released
time.sleep(0.01)
if mode == SET_PLAYER_COUNT:
player_count = get_player_count()
display_segments(player_count) # Update the display
mode = SET_TIMER_DURATION # Move to next mode
elif mode == SET_TIMER_DURATION:
timer_duration = get_timer_duration()
start_timer()
mode = TIMER_RUNNING # Start the timer
elif mode == TIMER_RUNNING:
# Additional timer management logic can be added here
pass
display_segments(get_player_count())
while True:
button_press()
if mode == SET_PLAYER_COUNT:
current_count = get_player_count()
if current_count != player_count:
player_count = current_count
display_segments(player_count)
time.sleep(0.01) # Sleep to prevent excessive CPU usage