# Timing Gate Code # File TimingGate7.py # 26/10/22 this program is for the both gates # if function switch Pin2 is Up (0 / False) it is the Start gate, which just outputs to Radio # if it is Down (1, True) then Finish Gate which displays time. from microbit import * import time import random import radio radio.config(group=17) ############################ # # # StartGate # # # ############################ def StartGate(): display.show('S') radio.on() localstate = 1 # assume beam not interrupted time.sleep(1) while True: if localstate == 0: if pin1.read_digital(): # if not interrupted display.show(3) radio.send('3') localstate = 1 else: if (pin1.read_digital() ==False): # interrupted display.show(2) radio.send('2') localstate = 0 if button_a.is_pressed(): display.show('A') radio.send('A') time.sleep(0.5) ############################ # # # FinishGate # # # ############################ def FinishGate(): display.show('F') radio.on() localstate = 1 # assume beam not interrupted time.sleep(1) while True: if localstate == 0: if pin1.read_digital(): display.show(1) localstate = 1 else: # localstate 1 if pin1.read_digital() ==False: display.show(0) localstate = 0 message = radio.receive() if message: display.show(message) if message == 'A': break if button_a.is_pressed(): break display.clear() # Await start signal (local or via radio)# while True: if pin1.read_digital() ==False: break message = radio.receive() if message == '2': break ##### start of race t1 = time.ticks_ms() display.show(Image('99999:' '99999:' '99999:' '99999:' '99999')) while pin1.read_digital()==False: # while beam not interrupted time.sleep(0.5) ##### await passing under finish gate# x = 0 while pin1.read_digital(): # while beam not interrupted x += 1 if x == 100: x = 0 dif=time.ticks_diff(time.ticks_ms(),t1)/1000 # sesonds display.show(int(dif)) time.sleep(0.01) t2 = time.ticks_ms() # display result display.clear() l=time.ticks_diff(t2,t1)/1000 while True: display.show(l, delay=1000) display.clear() time.sleep(1) # end of function FinishGate() # Main program... ############################ # # # Main # # # ############################ while True: if pin2.read_digital(): # true if 1: Finish gate FinishGate() else: StartGate() # run start gate