I was originally trying to use uasync to multi-thread the countdown, but had difficulty implementing it. This is my first real attempt at Python, minus a shell script or two.
Is there a way to have one of the onboard timers handle the countdown in a non-blocking fashion, so I can check the other sensors reliably. Eventually, I want to add a relay and control the fan, starting up with game start and turning it off at 0:00.
Code: Select all
from machine import Pin, Timer
import tm1637
import utime as t
#i2c for tm1637
Y1 = pyb.Pin.board.Y1
Y2 = pyb.Pin.board.Y2
tm = tm1637.TM1637(clk = Y1, dio = Y2)
#select button
X1 = pyb.Pin.board.X1
button = pyb.Pin(X1, pyb.Pin.IN)
#goal lines (use ADC to limit photoresistors to 64k)
X2 = pyb.Pin.board.X2
X3 = pyb.Pin.board.X3
goal1 = machine.ADC(X2)
goal2 = machine.ADC(X3)
# minimum ambient sensor value for goal detection (<64k)
g1threshold = 64*1024
g2threshold = 64*1024
# number of seconds to not allow goals, to avoid false positives
goalDelay = 3
goalTime = 0
# clock
startClock = False
min = 0
sec = 5
enableColon = True
while True:
print("waiting for button press")
if not button.value() and not startClock:
endTime = 60 * min + sec + t.time()
print("Button press")
startClock = True
# set thresholds at 10% drop.
g1threshold = int(goal1.read_u16() * .90)
if (g1threshold == 0):
showError("ERR INIT 1")
g2threshold = int(goal2.read_u16() * .90)
if (g2threshold == 0):
showError("ERR INIT 2")
break
t.sleep_ms(250)
while True:
score1 = score2 = 0
while startClock:
# update the 4x7seg with the time remaining
min = abs(int((endTime - t.time()) / 60))
sec = (endTime - t.time()) % 60
if (sec % 5 == 3):
print("Update Scoreboard")
tm.numbers(score1, score2)
print(str(min), str(sec), sep=':' )
enableColon = not enableColon # alternately blink the colon
tm.numbers(min, sec, colon = enableColon)
#### Rethink this - don't score if just scored
if (int(goal1.read_u16()) < g1threshold)
# prevent false positive if both sensors trigger or either report zero
if (int(goal2.read_u16()) < g2threshold):
showError("S2 PRB")
if (int(goal1.read_u16() == 0):
showError("S1 ERR 0")
if (int(goal2.read_u16() == 0):
showError("S2 ERR 0")
if t.time() > (goalTime + goalDelay):
goalTime = t.time()
print("Player 1 Scored")
score1 += 1
if (int(goal2.read_u16()) < g2threshold):
# prevent false positive if both sensors trigger or either report zero
if (int(goal1.read_u16()) < g1threshold):
showError("S1 PRB")
if (int(goal1.read_u16() == 0):
showError("S1 ERR 0")
if (int(goal2.read_u16() == 0):
showError("S2 ERR 0")
if t.time() > (goalTime + goalDelay):
goalTime = t.time()
print("Player 2 Scored")
score2 += 1
if (min + sec == 0): # once both reach zero, turn off relay & break
tm.numbers(score1, score2)
t.sleep(3)
tm.numbers(0, 00)
print("**************")
print(score1, score2, sep=':')
print("GAME OVER")
startClock = False
break
t.sleep_us(100)
break
def showError(string)
print(string)
tm.scroll(string, delay=250)
exit()