# NeoPixels Rainbow on MicroPython
# Wokwi Example https://wokwi.com/arduino/projects/305569065545499202
from machine import Pin
import neopixel
import time
import urandom
# Define the pins for the LEDs, NeoPixel, switches, and push button
led_pins = [12, 13, 14, 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 = 15 # Change this pin to the one where your NeoPixel is connected
# Initialize LED objects
leds = [Pin(pin_num, Pin.OUT) for pin_num in led_pins]
# Initialize NeoPixel
num_pixels = 12 # Change this to match the number of pixels in your NeoPixel ring
np = neopixel.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_DOWN)
# 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
current_led = 0 # Initialize current LED index
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()
# 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")
else:
# Switch 4 is turned off, so stop the timer
timer_running = False
print("Game is turned off. Timer stopped.")
# Check if the button is pressed
if button.value() == 1:
# Button is pressed
button_pressed_time = time.ticks_ms()
# Check if the button is released and pressed for 0.5 seconds
if button.value() == 0 and button_pressed_time != 0:
pressed_duration = time.ticks_diff(time.ticks_ms(), button_pressed_time)
if pressed_duration >= 500: # Button pressed for at least 0.5 seconds
# Toggle LED state and turn on/off LED12
led_state = leds[12].value()
if led_state == 0:
leds[12].value(1)
else:
current_led = (current_led + 1) % len(leds)
leds[current_led].value(1)
if current_led != 0:
leds[current_led - 1].value(0)
# Reset button press time
button_pressed_time = 0
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
#