from machine import Pin
from time import sleep
from neopixel import NeoPixel
import _thread
import json
f = open("diagram.json")
json_config = json.load(f)
NUM_LED = 0
for part in json_config["parts"]:
if part["type"] == "wokwi-led-ring" and "pixels" in part["attrs"]:
NUM_LED = int(part["attrs"]["pixels"])
break
pixel1 = NeoPixel(Pin(13),NUM_LED)
pixel2 = NeoPixel(Pin(15),NUM_LED)
g1 = Pin(12, Pin.IN, Pin.PULL_UP)
g2 = Pin(2, Pin.IN, Pin.PULL_UP)
def player1():
while True:
i = 0
up = True
while i < NUM_LED:
if g1.value() == False and up:
pixel1[i] = (255,0,0)
pixel1.write()
i += 1
up = False
if g1.value() == True:
up = True
sleep(0.01)
print("Player 1")
sleep(1)
pixel1.fill((0,0,0))
pixel1.write()
def player2():
while True:
i = 0
up = True
while i < NUM_LED:
if g2.value() == False and up:
pixel2[i] = (0,255,0)
pixel2.write()
i += 1
up = False
if g2.value() == True:
up = True
sleep(0.01)
print("Player 2")
sleep(1)
pixel2.fill((0,0,0))
pixel2.write()
t1 = _thread.start_new_thread(player1, ())
t2 = _thread.start_new_thread(player2, ())
Player 1
Player 2