import machine
import time
from neopixel import NeoPixel
# Define GPIO pins
LED_PINS = [12, 14, 27, 26]
NEOPIXEL_PIN = 13
SWITCH_PIN = 4
BUTTON_PIN = 5
TIMER_SWITCH_PIN = 2
POTENTIOMETER_PIN = 34
LED_PIN = 14 # GPIO pin connected to the LED
# Initialize GPIO pins
leds = [machine.Pin(pin, machine.Pin.OUT) for pin in LED_PINS]
switch = machine.Pin(SWITCH_PIN, machine.Pin.IN)
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
timer_switch = machine.Pin(TIMER_SWITCH_PIN, machine.Pin.IN)
potentiometer = machine.ADC(machine.Pin(POTENTIOMETER_PIN))
led = machine.Pin(LED_PIN, machine.Pin.OUT)
# Initialize NeoPixel
np = NeoPixel(machine.Pin(NEOPIXEL_PIN), 12)
# Button state variables
last_button_state = 1
button_pressed = False
# Timer state variables
timer_duration = 0
start_time = 0
pause_time = 0 # Variable to store the time when the timer is paused
# Player selection state variables
current_led_index = 0
# Debounce function
def debounce():
time.sleep_ms(20) # Adjust the delay based on the button characteristics
def startup_animation():
try:
# Blink LEDs separately
for led in leds:
led.on()
time.sleep(0.5)
led.off()
time.sleep(0.5)
# Neopixel full wave animation
for i in range(12):
np[i] = (255, 255, 255)
np.write()
time.sleep(0.1)
for i in range(12):
np[i] = (0, 0, 0)
np.write()
except Exception as e:
print("Error during startup animation:", e)
def check_button_state():
global last_button_state, button_pressed
try:
button_state = button.value()
if button_state != last_button_state:
if button_state == 0:
button_pressed = True
last_button_state = button_state
except Exception as e:
print("Error while checking button state:", e)
def select_player():
global button_pressed, current_led_index
try:
if button_pressed:
# Turn on the next LED
for led in leds:
led.off()
current_led_index = (current_led_index + 1) % len(leds)
leds[current_led_index].on()
# Reset button state
button_pressed = False
except Exception as e:
print("Error during player selection:", e)
def countdown_timer():
global timer_duration, start_time, pause_time
try:
if timer_switch.value() == 1:
if start_time == 0: # Timer is not running
start_time = time.ticks_ms() # Start the timer
elif pause_time != 0: # Timer was paused
# Adjust start time by subtracting the elapsed pause time
start_time += (time.ticks_ms() - pause_time)
pause_time = 0 # Reset pause time
else:
if start_time != 0: # Timer is running
# Pause the timer and record the pause time
pause_time = time.ticks_ms()
start_time = 0 # Reset start time
if start_time != 0: # Timer is running
elapsed_time = time.ticks_diff(time.ticks_ms(), start_time) // 1000
remaining_time = timer_duration - elapsed_time
if remaining_time <= 0:
# Reset the timer
start_time = time.ticks_ms()
else:
# Update Neopixel ring based on remaining time
for i in range(12):
if i < remaining_time // 5: # Each LED represents 5 seconds
np[i] = (255, 255, 255) # White color
elif i < remaining_time // 5 + 3: # Quarter of the time left
np[i] = (255, 0, 0) # Red color
else:
np[i] = (0, 0, 0) # Turn off LED
np.write()
except Exception as e:
print("Error during countdown timer:", e)
def read_potentiometer():
try:
pot_value = potentiometer.read()
if pot_value <= 1365: # Lower third of potentiometer range
return 30
elif pot_value <= 2730: # Middle third of potentiometer range
return 60
else: # Upper third of potentiometer range
return 120
except Exception as e:
print("Error while reading potentiometer:", e)
return 60 # Default value
# Main loop
startup_done = False
while True:
try:
# Check if the switch on port 4 is turned on before proceeding
if switch.value() == 1:
if not startup_done:
# Run startup animation only once
startup_animation()
startup_done = True
# Check if timer switch is on and run countdown timer if it's on
if timer_switch.value() == 1:
countdown_timer()
# Check player select button
check_button_state()
select_player()
# Read potentiometer for timer duration
timer_duration = read_potentiometer()
else:
startup_done = False # Reset startup flag if the switch is turned off
# Reset timer when the switch is turned off
start_time = 0
pause_time = 0
time.sleep(0.5) # Wait for the switch to be turned on
except KeyboardInterrupt:
print("Program interrupted by user.")
break
except Exception as e:
print("Unexpected error:", e)