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)
]
# Mode settings
SET_PLAYER_COUNT = 0
SET_TIMER_DURATION = 1
TIMER_RUNNING = 2
# Initial settings
mode = SET_PLAYER_COUNT
player_count = 2
timer_duration = 10 # Default to 10 seconds
# Timer states
RUNNING = 1
PAUSED = 0
state = PAUSED
def get_player_count():
pot_value = pot.read()
return max(2, round((pot_value / POT_MAX_VALUE) * (len(SEGMENT_COLORS) - 1)) + 1)
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 get_timer_duration():
pot_value = pot.read()
return max(10, round((pot_value / POT_MAX_VALUE) * 60)) # 10 to 60 seconds
def update_leds_for_duration(duration):
led_count = round((duration - 10) / 50 * (LED_COUNT - 1) + 1) # Map duration to LEDs
for i in range(LED_COUNT):
if i < led_count:
np[i] = (255, 255, 255) # White when active
else:
np[i] = (0, 0, 0) # Turn off the rest
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 update_timer():
global last_update_time, leds_on, state
current_time = time.ticks_ms()
# Calculate the interval for each LED based on the total duration and the number of LEDs
interval = (timer_duration * 1000) // LED_COUNT
if state == RUNNING and time.ticks_diff(current_time, last_update_time) >= interval:
leds_on -= 1 # Decrease the number of LEDs on
# Update LEDs to reflect the remaining time
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 or trigger an event after the 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) # Show the selected player count
mode = SET_TIMER_DURATION # Change mode to setting the timer
elif mode == SET_TIMER_DURATION:
timer_duration = get_timer_duration()
update_leds_for_duration(timer_duration) # Display the timer setting
mode = TIMER_RUNNING # Ready to start the timer
elif mode == TIMER_RUNNING:
start_timer() # Start the timer
# Display initial player count
player_count = get_player_count()
display_segments(player_count)
while True:
button_press()
if mode == SET_PLAYER_COUNT:
current_player_count = get_player_count()
if current_player_count != player_count:
player_count = current_player_count
display_segments(player_count)
elif mode == SET_TIMER_DURATION:
current_duration = get_timer_duration()
if current_duration != timer_duration:
timer_duration = current_duration
update_leds_for_duration(timer_duration)
if mode == TIMER_RUNNING:
update_timer()
time.sleep(0.01) # Sleep to prevent excessive CPU usage