import machine
import neopixel
from machine import Pin, ADC,PWM
from time import sleep_ms, ticks_ms
pause_button = Pin(21, Pin.IN, Pin.PULL_UP) # Initialize pause button pin with internal pull-up resistor
paused = False # Flag to track pause state
def toggle_pause(pin):
sleep_ms(100)
global paused
paused = not paused
# Set up interrupt on the pause button pin
pause_button.irq(trigger=Pin.IRQ_FALLING, handler=toggle_pause)
# Define pin numbers
NEOPIXEL_PIN = 12
BUTTON_PIN = 4
SWITCH_PIN_PLAYER = 25
SWITCH_PIN_LED = 23
buzzer_pin_number = 5
# Define colors for player selection mode
selection_colors = [(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(0, 255, 255), # Cyan
(128, 0, 128)] # Purple
# Define brightness (default 0.01)
brightness = 1
# Initialize NeoPixel
np = neopixel.NeoPixel(machine.Pin(NEOPIXEL_PIN), 16)
# Initialize button pin
button = machine.Pin(BUTTON_PIN, machine.Pin.IN,Pin.PULL_UP)
# Initialize switch pins
switch_player = machine.Pin(SWITCH_PIN_PLAYER, machine.Pin.IN,Pin.PULL_UP)
switch_led = machine.Pin(SWITCH_PIN_LED, machine.Pin.IN,Pin.PULL_UP)
# Initialize ADC for potentiometer
adc = ADC(Pin(35))
adc.atten(ADC.ATTN_11DB) # use full range 0 to 3.3V (11DB)
adc.width(ADC.WIDTH_12BIT) # resolution 12 bit
# Variables for debouncing
last_button_state = 1 # Assume the button is initially not pressed
last_button_time = 0
num_leds = 4 # Default number of LEDs for player selection mode
# Function to light up LEDs based on number of LEDs to be turned on
def light_leds(num_leds):
for i in range(num_leds):
color = selection_colors[i % len(selection_colors)]
np[i] = (int(color[0] * brightness),
int(color[1] * brightness),
int(color[2] * brightness))
for i in range(num_leds, len(np)):
np[i] = (0, 0, 0)
np.write()
# Function to toggle LEDs on and off
def toggle_leds(num_leds):
for i in range(num_leds):
if np[i] == (0, 0, 0):
color = selection_colors[i % len(selection_colors)]
np[i] = (int(color[0] * brightness),
int(color[1] * brightness),
int(color[2] * brightness))
else:
np[i] = (0, 0, 0) # Turn off LED
np.write()
# Function to read the switch state
def read_switch(switch_pin):
return switch_pin.value()
# Function to sequentially light LEDs with different colors for a specified duration
def light_led_sequence(duration_ms):
# Initialize buzzer
buzzer_pin = PWM(Pin(buzzer_pin_number))
buzzer_pin.duty(0) # Set initial duty cycle to 0
# LED sequence
for color in selection_colors[:num_leds]:
# Set all LEDs to the current color
for i in range(len(np)):
np[i] = tuple(int(val * brightness) for val in color) # Adjust brightness
np.write()
while True:
if not button.value(): # Check if button is pressed
break # Exit the loop if button is pressed
sleep_ms(50) # Polling interval for button press
# Dim LEDs one by one
for i in range(len(np)):
np[i] = tuple(int(val * brightness) for val in color) # Re-set the current color
np.write()
sleep_ms(200) # Pause to hold the full lit color
if i == len(np) - 1: # If it's the last LED
# Beep the buzzer with a higher frequency
buzzer_pin.freq(1500) # Set higher frequency for the last beep
buzzer_pin.duty(512) # Set duty cycle for volume
sleep_ms(100) # Beep duration
buzzer_pin.duty(0) # Turn off buzzer
elif i >= len(np) - 5: # If it's one of the last 4 LEDs
# Beep the buzzer with the regular frequency
buzzer_pin.freq(1000) # Set regular frequency for the other beeps
buzzer_pin.duty(512) # Set duty cycle for volume
sleep_ms(100) # Beep duration
buzzer_pin.duty(0) # Turn off buzzer
np[i] = (0, 0, 0) # Turn off current LED
np.write()
# Check for pause button press to pause/resume
if not paused:
print("notpaused") # Wait for a fraction of the total duration
else:
while paused:
print("paused")
sleep_ms(100) # Wait while paused
if not button.value(): # Check if pause button is pressed to resume
toggle_pause(None) # Toggle the paused state to resume
if read_switch(switch_led) == 0:# Check ledcycling switch to see if function has to stop
return
if not button.value(): # Check if button is pressed to skip a player
sleep_ms(1000)
break # Exit the loop if button is pressed
sleep_ms(duration_ms // len(np)) # Wait for a fraction of the total duration
# Function to handle button press with debouncing
def handle_button(pin):
global last_button_state, last_button_time, num_leds
# Check the current mode of operation
if read_switch(switch_player) and read_switch(switch_led) == 0: # If in player selection mode
current_time = ticks_ms() # Get current time in milliseconds
# Check if button state has changed and it has been more than 500ms since last change
if pin.value() != last_button_state and current_time - last_button_time > 500:
last_button_state = pin.value() # Update last button state
last_button_time = current_time # Update last button time
num_leds = (num_leds % 6) + 1 # Change the number of LEDs
light_leds(num_leds) # Light up LEDs accordingly
else: # If in LED cycling mode or any other mode
# Your existing handling for LED cycling mode
# This can be added here if necessary
pass
# Attach interrupt to button pin
button.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=handle_button)
# Main loop
while True:
# Check Player selection mode switch
if read_switch(switch_player): # If the switch is ON
print("Player selection mode activated.")
toggle_leds(num_leds) # Toggle LEDs that are currently lit
sleep_ms(200) # Delay for 500 milliseconds
else:
print("Player selection mode deactivated.")
for i in range(len(np)): # Dim all LEDs when the switch is OFF
np[i] = (0, 0, 0)
np.write()
# Check LED cycling switch
if read_switch(switch_led): # If the LED cycling switch is ON
print("LED cycling activated.")
adc_value = adc.read()
voltage = adc_value / 4095.0 * 3.3 # Convert ADC value to voltage
print("The read voltage is:", voltage, "V")
# Map the voltage value to a duration between 1000 ms and 60000 ms (1 second to 30 seconds)
duration_ms = int((voltage / 3.3) * 29000) + 1000
print("Duration of LED sequence:", duration_ms, "ms")
# Sequentially light LEDs with different colors for the calculated duration
light_led_sequence(duration_ms)
else:
print("LED cycling deactivated.")
for i in range(len(np)): # Dim all LEDs when the switch is OFF
np[i] = (0, 0, 0)
np.write()
sleep_ms(200) # Delay to reduce processing load