from machine import Pin,
import time
from neopixel import NeoPixel
# Define the pins for the LEDs, NeoPixel, switches, and push button
led_pins = [12, 14, 26, 27] # Change these pins as per your ESP32 board's pinout
switch_game_pin = 4 # Change this pin to the one where your game switch is connected
switch_timer_pin = 2 # Change this pin to the one where your timer switch is connected
button_pin = 5 # Change this pin to the one where your push button is connected
neopixel_pin = 13 # Change this pin to the one where your NeoPixel is connected
# Initialize LED objects
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
current_led = 0
# Initialize NeoPixel
num_pixels = 12 # Change this to match the number of pixels in your NeoPixel ring
np = NeoPixel(Pin(neopixel_pin), num_pixels)
# Initialize switches
switch_game = Pin(switch_game_pin, Pin.IN)
switch_timer = Pin(switch_timer_pin, Pin.IN)
# Initialize push button
button = Pin(button_pin, Pin.IN, Pin.PULL_UP)
def button_callback():
global current_led
leds[current_led].on()
time.sleep(0.5)
if not button.value():
time.sleep_ms(20)
while not button.value():
start_time = time.ticks_ms()
print("knop 1")
pass # Wait for button release
end_time = time.ticks_ms()
duration = time.ticks_diff(end_time, start_time)
if duration in range(1,3001): # Short press
leds[current_led].off()
current_led = (current_led + 1) % 4
print(f"Switched to LED {current_led}")
leds[current_led].on()
time.sleep(0.5)
else: # Long press
print(f"Selected LED {current_led}")
return
# Function to turn on all LEDs
def turn_on_all_leds():
for led in leds:
led.value(1) # Turn on LED
# Function to turn off all LEDs
def turn_off_all_leds():
for led in leds:
led.value(0) # Turn off LED
# Function to turn on NeoPixel
def turn_on_neopixel():
for i in range(num_pixels):
np[i] = (255, 255, 255) # White color for NeoPixel
np.write()
# Function to turn off NeoPixel
def turn_off_neopixel():
for i in range(num_pixels):
np[i] = (0, 0, 0) # Turn off NeoPixel
np.write()
# Function to make NeoPixel do a wave effect
def neopixel_wave():
for _ in range(2): # Repeat the wave effect two times
for i in range(num_pixels * 2):
for j in range(num_pixels):
if (i + j) % num_pixels == 0:
np[j] = (255, 255, 255) # Set NeoPixel to white color
else:
np[j] = (0, 0, 0) # Turn off NeoPixel
np.write()
time.sleep(0.1) # Adjust speed of wave effect
# Function to blink each LED three times consecutively
def blink_leds():
for led in leds:
for _ in range(3): # Blink three times
led.value(1) # Turn on LED
time.sleep(0.2)
led.value(0) # Turn off LED
time.sleep(0.2)
# Function to start or continue the 60-second timer
def start_timer(remaining_time):
while True:
if switch_timer.value() == 0 or switch_game.value() == 0:
print("Timer paused")
return remaining_time
print("Time remaining: {} seconds".format(remaining_time))
# Calculate number of LEDs to light up based on remaining time
leds_to_light = min(remaining_time // 5, num_pixels)
for i in range(num_pixels):
if i < leds_to_light:
np[i] = (255, 255, 255) # White color for NeoPixel
else:
np[i] = (0, 0, 0) # Turn off NeoPixel
np.write()
remaining_time -= 1
if remaining_time < 0:
remaining_time = 60 # Reset remaining time to 60 seconds
time.sleep(1) # Wait for 1 second
# Main loop
try:
switch_prev_state = 0 # Previous state of the game switch
timer_running = False # Timer status flag
remaining_time = 60 # Initialize remaining time to 60 seconds
button_pressed_time = 0 # Initialize button press time
while True:
# Check the game switch
switch_state = switch_game.value() # Current state of the game switch
# Check if the game switch transitions from off to on
if switch_state == 1 and switch_prev_state == 0:
# Blink each LED three times consecutively
blink_leds()
# Turn on NeoPixel
turn_on_neopixel()
# Do a wave effect with NeoPixel
neopixel_wave()
# Turn off NeoPixel
turn_off_neopixel()
# Check if the game switch transitions from on to off
elif switch_state == 0 and switch_prev_state == 1:
# Turn off all LEDs immediately
turn_off_all_leds()
# Turn off NeoPixel
turn_off_neopixel()
button_callback()
# Update previous state of the game switch
switch_prev_state = switch_state
# Check if switch 4 is turned on
if switch_game.value() == 1:
# Check the timer switch
timer_switch_state = switch_timer.value() # Current state of the timer switch
if timer_switch_state == 1:
# Start or continue the timer
if not timer_running:
remaining_time = start_timer(remaining_time)
timer_running = True
else:
# Pause the timer
timer_running = False
print("Timer paused 2")
else:
# Switch 4 is turned off, so stop the timer
timer_running = False
print("Game is turned off. Timer stopped.")
except KeyboardInterrupt:
# Turn off LEDs and NeoPixel before exiting
turn_off_all_leds()
turn_off_neopixel()
#als knop 1 uit staat, staan alle leds uit, als aan staat, staan alle leds actief
#als knop 2 uit staat, staat het spel op pauze, als aan staat, start het spel/gaat spel verder
#bij begin spel op de drukknop drukken is speler kiezen, 3 seconde ingedrukt houden om 1e speler te bevestigen, 6 seconde voor random
#led van gekozen speler blinkt 5 keer in 2 seconde
#neopixel ring maakt 3 omtrekken van kleur van gekozen speler
#loop
#neopixel start vol en tikt af
#als drukknop 1x wordt ingedrukt dan begin volgende speler
#anders als neopixel leeg is begin volgende speler
#