Timing gate


Latest news

There are new versions of the Timing gate software.
We could use the new seven segment display to show the time at the end of a race
see Here,
for usefull stuff on sending and receiving data by Radio
The seven segment display microbit will have to use the same group as the timing gate:
import radio
radio.config(group=17)   # receiver has to have same group
      

We have two Microbit timing gates that can be used as "start" or "finish" timing gates,
So we can time a drag race, or even two line following circuits simultaneously.
The gates have a switch which controls whether it's a start gate of a fimish gate.
For full instructions see here
The program is available here: view download

Purpose

This is to measure the time to run a line follower circuit, or a Drag race run.
We can use it in 3 ways-
- Single-gate Line follower
- Single-gate Drag race
- Double-gate Drag race
See below for details

Hardware

We will be using a Microbit to detect the robot finishing, and to display the time.

How we will use it

It will be a gate through which the robot passes.
It can detect a robot passing through.
It will be started by a "timing gate operator" pressing a button
This will start the timer, and also signal to the robot handler when to start the robot,
  by a symbol on the display
It will display time increasing (in seconds) until it sees the robot passing underneath
After the race it will display the finishing time in milliseconds,
  and continue doing so until switched off.

Accuracy

It will measure time in hundredths of a second using the python system clock functions.
These are time.ticks_ms(), and time.ticks_diff(),
Which can give the elapsed time measurement.

Programming

We will be using Version 1 Microbits, which has:
  • A 5 led by 5led display,
  • two pushbuttons A and B
  • A number of digital inputs
  • Radio communication with other Microbits
It can be programmed in "MakeCode" or Python.
We shall be using Python.
The Microbit needs to be programmed to respond to the inputs.

Example programs

Here is a simple starter program: view download It uses button A to start, when it displays "X" to let the driver know,

The end of race is detected by pressing button B (there is no sensor used)
It counts time by counting loops with an inner delay of 10ms
It is not accurate!
It displays seconds, tenths and hundredths with individual display commands

A later program is here view download
It records time by counting loops and also by using time.ticks_diff()
It displays both times. See the difference!

Reading buttons

There are two button object that you import from microbit
- "button_a" and "button_b".
Use the method "is_pressed()" which returns True or False
	
from microbit import *
import time
# wait for button A. Start of race
while button_a.is_pressed()==False:
    time.sleep(0.01)

Detecting robots crossing line

The output from the sensor is connected to Pin 1
The Microbit can detect the state of the light sensor with the following method:
    pin1.read_digital()
This gives a True return value if there is light detected (3.3v input) and False if not.
So to wait for a robot crossing the line use:
	
from microbit import *
import time
# wait for robot to cross line
while pin1.read_digital():  # while True (light present)
    time.sleep(0.01)

Recording time

The way to read time in Python is to use the method "time.ticks_ms()".
Record the start time with:
    tstart = time.ticks_ms()       # record start time 
- and record the finish time with
    tend = time.ticks_ms()         # record finish time
This records the time in milliseconds.

You could calculate the race time by subtracting start from finish,
but there are circumstance when this is incorrect. Instead, you should use:
    elapsed =  time.ticks_diff(tend,tstart)  # Calculate elapsed time in milliseconds
This takes care of cases when the "clock" goes past the highest value (like 12:00 on a normal clock)

Displaying time in digits

The display.show() method shows all digits of a number
display.show(elapsed/1000)      # display elapsed time in seconds

Sending and receiving messages by radio

Before you can use the radio you need to import the radio module,
set the group up (to be different from other microbits in the area,
and turn it on,
  
    import radio
    radio.config(group=17) # set up a group number
    radio.on()             # turn the rdio on
To send a message use this:
    radio.send('3')   # send a single-character message
To receive a message:
    
    msg = radio.receive() # attempt to read a message
    if message:           # this is true if one has arrived
        display.show(msg) # it can be a string or a single character

Displaying time in binary

The following function displays a digit in binary on the specified row of the display:
# show value 0-15 on given row
def showbin(row,value):
    bit = (value%2)*9               # read the right-hand bit of the value
    value = int(value/2)            # shift all bits 1 place to the right
    display.set_pixel(4, row, bit)  # column, row, brightness
    
    bit = (value%2)*9               # read the next bit - value 2
    value = int(value/2)            # etc.etc
    display.set_pixel(3, row, bit)  #   
    
    bit = (value%2)*9               # read the next bit - value 4
    value = int(value/2)
    display.set_pixel(2, row, bit)  #  
    
    bit = (value%2)*9               # value 8
    display.set_pixel(1, row, bit)  #    

Single-gate Line follower

Here we have just one gate on the line-follower track, and it measures the time between 2 interruptions of the light beam.

The program should do the following after switch-on:
1 -   Wait for Button A to be pressed,
      While waiting, it should display "0" if the beam is interrupted, and "1" if not
      this allows the gate to be checked before use.

2 -   Wait for the beam to be interrupted,
      When interrupted record the start time

3 -   Wait for 1 second, this will allow the robot to leave the gate

4 -   Wait for the beam to be interrupted again,
      When interrupted record the finish time

5 -   Calculate the elapsed time, as seconds with 2 decimal digits (hundredths)
      Display the time and continue displaying until switched off.

Single-gate Drag race

Here we have just one gate on the drag race track,
and it measures the time between pressing the start button and interruption of the light beam.

The program should do the following after switch-on:

1 -   Wait for Button A to be pressed,
      While waiting, it should display "0" if the beam is interrupted, and "1" if not
      this allows the gate to be checked before use.

2 -   When button A is pressed
      Immediately record the start time
      Display the start image to tell the driver to start,

3 -   Wait for the beam to be interrupted,
      When interrupted record the finish time

4 -   Calculate the elapsed time, as seconds with 2 decimal digits (hundredths)
      Display the time and continue displaying until switched off.

Double-gate Drag race


Here we have two gates on the drag race track,
The finish gate measures the time between pressing the start button and interruption of its light beam.
The start gate just detects interruption of its light beam, and sends "on" and "off" messages to the finish gate.
The finish gate checks that there are no start gate interruptions before the race starts,
but checks that there is one after the start

The Finish gate program should do the following after switch-on:

1 -   Wait for Button B to be pressed, or a radio message from the start gate.
      While waiting, it should display "0" if the beam is interrupted, and "1" if not
      Also, it should display "X" if the start gate sends an interrupt message, and a "Z" if start gate is cleared
      this allows both gates to be checked before use.

2 -   When button B is pressed, Wait for Button A to be pressed, or a radio message from the start gate.
      If a start gate message is received before button A,
      Display an image to indicate false start,

3 -   When button A is pressed
      Immediately record the start time
      Display the start image to tell the driver to start,

3 -   When a start gate message is received after button A
      Immediately record the reaction time

3 -   Wait for the beam to be interrupted,
      When interrupted record the finish time

5 -   Calculate the elapsed time, as seconds with 2 decimal digits (hundredths)
      Display the time and continue displaying until switched off.
The Start gate program should do the following after switch-on: