Hi guys. I've been fiddling with the Pico/Pico W lately and I am building a simple game of "Simon" using 3 LEDs and 3 buttons. Everything works fine aside from threading. I'm trying to create a separate thread to check if the machine should go do sleep and conserve energy. I haven't even gotten to waking it up though because _thread causes this weird OSError "TinyUSB callback can't recurse". It also says "FATAL: uncaught exception 2001a6e0" I'm not even using USB, which really has me confused.
Here is the code I have:Is there a different threading library I should be using? Is there something stupid wrong with the code that I'm overlooking as a newbie?
Part of me wonders if the Pico has gone bad. When the gameOver() function is called, the text "GAME OVER" doesn't get printed until AFTER the LEDs flash, despite the fact that the print is literally at the beginning of the function.
Here is the code I have:
Code:
import machinefrom machine import Pinimport timefrom time import sleepimport picozerofrom picozero import LED, Buttonimport randomimport _threadonboardLED = machine.Pin("LED",Pin.OUT)round = 1roundData = []myInput = []sleepTime = 0redButton = Button(10)greenButton = Button(11)yellowButton = Button(12)redLED = LED(21)greenLED = LED(20)yellowLED = LED(19)def sleepTimer(): global sleepTime while sleepTime < 10: print(sleepTime) sleepTime = sleepTime + 1 sleep(1) print('Going to sleep...') sleep(1) #machine.deepsleep()def init(): x = 0 while x < 3: redLED.off() greenLED.off() yellowLED.off() redLED.on() sleep(0.5) redLED.off() greenLED.on() sleep(0.5) greenLED.off() yellowLED.on() sleep(0.5) yellowLED.off() x = x + 1 startRound() def play(i): global roundData global myInput global round global sleepTime sleepTime = 0 #print('Sleep time reset (button press)') redLED.off() greenLED.off() yellowLED.off() myInput.append(i) if len(myInput) < len(roundData): #check corectness of each press instead of waiting until lengths are equal a = 0; while a < len(myInput): if myInput[a] != roundData[a]: gameOver() a = a + 1 elif len(myInput) == len(roundData): if myInput == roundData: print("Win!") sleep(1.5) round = round + 1 startRound() else: gameOver()def restart(): global round global roundData global myInput round = 1 roundData = [] myInput = [] init()def gameOver(): print("GAME OVER") redButton.when_released = restart greenButton.when_released = restart yellowButton.when_released = restart x = 0 try: while x < 30: sleep(0.0625) redLED.on() greenLED.on() yellowLED.on() sleep(0.0625) redLED.off() greenLED.off() yellowLED.off() x = x + 1 except KeyboardInterrupt: print("DONE") def startRound(): global roundData global myInput global round global sleepTime myInput = [] while len(roundData) < round: roundData.append(random.randint(0,2)) for z in roundData: sleepTime = 0 #print("Sleep time reset (start of round)") if z == 0: sleep(0.75) redLED.on() sleep(0.75) redLED.off() elif z == 1: sleep(0.75) greenLED.on() sleep(0.75) greenLED.off() elif z == 2: sleep(0.75) yellowLED.on() sleep(0.75) yellowLED.off() redButton.when_released = lambda : play(0) greenButton.when_released = lambda : play(1) yellowButton.when_released = lambda : play(2) redButton.when_pressed = lambda : redLED.on() greenButton.when_pressed = lambda : greenLED.on() yellowButton.when_pressed = lambda : yellowLED.on()init()sleepThread = _thread.start_new_thread(sleepTimer,())
Part of me wonders if the Pico has gone bad. When the gameOver() function is called, the text "GAME OVER" doesn't get printed until AFTER the LEDs flash, despite the fact that the print is literally at the beginning of the function.
Statistics: Posted by RYoder97 — Mon Nov 04, 2024 5:30 pm