microbit Lap Counter For Scalextric Like Track:Racing Scalextric cars is much more fun if you can keep a count of the laps. Have a fastest time to beat and get caught out if you jump the gun. So here is a microbit script to do just that.
Features:This lap counter uses a BBC microbit to:
- Count the laps of a race for each of two cars in a race from 5 to 35 laps long (6 options).
- Time the fastest car. Display the winning car and its time.
- Start the race with a Formula 1 style start with five LEDs lighting up in sequence. The race starts when all the lights go out.
- Catch and indicate any car that jumps the gun. That car has to do a Stop/Go penalty to reset their LED or be disqualified.
- On start up - select the number of laps in a race by tipping the microbit.
History:These functions were first designed and implemented using 16 Dual In Line (DIL) components on three circuit boards with numerous resistors, capacitors, two 7 segment displays and a hacked sports stopwatch, taking months to build and test and totally inflexible. In contrast, today it can all be done by one microbit, two sensors and 16 components (max) and took 3 weeks to design and build. What is more, it can be improved just by re-flashing. And this is why microprocessors have taken over the world, to be found in every product from a washing machine to the many used in automobiles and industrial plant.
- BBC microbit with its battery or source of power.
- Edge Connector Breakout Board for BBC microbit - Pre-built.
- 10 M/F jumper wires.
- QRD1114 Reflective Object Sensors or equivalent x 2
- 3.3V Zener Diodes. x 2
- 1N4001 silicon diodes or 1N4148 (smaller) or equivalent silicon diodes. x 2
- Each of the following; 120 Ω resistors, 10 kΩ resistors. x 2
- Components that may vary depending on your track include; two 100 µF 25V electrolytic capacitors, two resistors of 4k7 or 1k2 depending on the maximum voltage of your track and two optional resistors of about 10k.
- MDF 3mm sheet to make a simple bridge over the track to hold the proximity sensors, see dimensions below under the section Construction plus 3 off, M3 nuts, bolts about 2cm long and washers.
- Various coloured wire.
- Strip Board or small prototype breadboard PBU301.
- Scalextric or similar rail-car set.
Description:(Note: The MakeCode editor does not show the working of the.hex code correctly.) The lap counter works by shining an infrared LED light onto the top of a racing car (best if it is a white top) and getting the reflected light to make a transistor conduct like a switch. There should be about 3-5 mm between the detector and the top of the car. Beware however that if the racing track is in strong sunlight the sensors will pick up the sun's infrared and saturate. You will be able to tell if this happens; if after the start sequence LEDs at x = 1 y = 0 (1, 0) and x = 3, y = 0 (3, 0) come on and stay on and the count does not progress as cars pass under the sensor. So just shade the sensors.
False Starts:A False Start is detected by measuring the track voltage during the start sequence and feeding that to pin P2 for car A and P8 for car B. Since the maximum track voltage can be from 5 V to 12 V a 3.3V Zener diode must be used to protect the microbit and the leads to pin P2 and P8 must be tested with maximum input voltage before they are connected to the microbit pins. As soon as the voltage on pin P2 (or P8) drops below about 2V the LED goes out. So assuming the car is at full speed = 5V for some tracks or 12V for Scalextric then the car voltage has to be significantly reduced to reset the False Start LED which can only be extinguished by stopping the car for about 1 second determined by components, shown in the circuit diagram below and explained later. This is a Stop – Go penalty.
Initial testing before construction work:Load the script into the MakeCode editor and see the documentation attached to many of the blocks. The MaekCode editor microbit emulation shows the Start Up correctly but when button A is pressed it fails to count touches to pins P0, P1, P2 and P8. So it is best to see its action by flashing the code onto a microbit and use a flying lead from the +3V tab and touch the pins P0, P1 to see the lap count work and clip onto pin P2 to see the false start work. Load the Script’s .hex file onto your microbit. When a source of power is applied to the microbit an ON START script asks you to set the maximum number of laps in a race. You do this by tipping the microbit to the right (positive x acceleration). LED’s are lit to indicate laps for car A as stated above (B gets the same length race automatically). Column x =1 (the second column) will always light up with 5 LEDs indicating 5 laps. You then add an extra 6 laps at a time for each LED lit in column x = 0, by tipping the microbit to the right. Use the formula max laps = (n * 6) + 5 where n is number of LEDs in column x = 0.
Testing The Script:Put a banana plug (or crocodile clip) and red lead onto the 3V tab to test the script. You will use the other end to simulate laps and false starts. Press Button A to see the top five LEDs come on at 1-second intervals and then they go out together after a random interval to indicate the start of the race, like Formula 1 starts. Use the other end of the 3 V lead to touch P0 and see the lap count for car A increase by one (more than one LED may come on if your contact is intermittent). Try the same with pin P1 (car B). Keep touching the pins until all 5 LEDs in column x = 1 are lit for one of the cars and, assuming you selected a 5 lap race, you should get a message saying which car has won and how many seconds it took. Now clip the other end of the 3V lead to pin P2 simulating a false start and press button A. After the Start LEDs go out, the LED at (2, 0) lights to indicate that car A jumped the start. Remove the 3V from pin 2 and the LED at (2, 0) goes out since the car has been stopped, a Stop/Go penalty. If you have access to pin P8 through a breakout board, test Car B using pin 8 as above. Failure to stop causes a disqualification to be indicated by a X.
Circuit Diagram:Fig 2 shows the total circuit in three parts. The lap counter uses two Fairchild QRD114 (see the specification sheet). Be very careful when reading the physical configuration to identify the correct pins as the sheet shows pins from above and below the component without indicating which is which. Also, other proximity sensors may have different pin configurations. The transistor and LED are push fits into their case and can come out. Make sure they are both flush with the case top when in use. The second part of the circuit detects false starts. Before coupling this circuit to pins P2 and P8 be sure to test the voltage across both 3.3V Zener diodes to make sure that when the full car supply voltage is applied to the track the voltage across each Zener does not exceed 3.3V relative to GND. If it does it could burn out the microbit which is specified at 3.6V maximum. It may only be 3V which is fine. If the voltage does exceed 3.3V try another Zener. The capacitor C1 serves two functions. Firstly it smoothes out the erratic voltage from the track that could easily look like the car had stopped when it had not. But more importantly, along with resistor R3, it determines how long the car has to stop before its False Start LED goes out. On my Scalextric track, a 100 µF electrolytic capacitor with no resistor R3 forced the car to stop for just over a second, but a lot depends on how your car controller is wired up, so experiment. The time is determined by (but not exactly) R3 in ohms x C1 in Farads (that’s the micro Farad number divided by 1000,000) and is called a time constant. E.g. 5k6 x 100µF = 0.56 seconds. This circuit has a diode D1 (1N4001 or 1N4148 or equivalent) to help protect the Micro:bit if you get the track voltage the wrong way round. The third part of the circuit is your choice of racing car track, controllers and cars and not part of this design.
Making A Bridge:If you have to make a bridge, Fig 4 a & b, is the simplest and quickest way to do it. This can be knocked up in 20 minutes from a small sheet of 3mm MDF and the dimensions are given in fig 5. The pieces do not even need to be glued, if you are careful, as the three bolts secure the sensors and vertical pillars making it very easy to adjust the sensor height. Just make sure my dimensions are wide enough for your track.
The Stop/Go Circuit:
Operation:Now test the setup. Plug a battery into the microbit. Flash the Lap Counter hex file if not already done. Set the maximum race length to say, 5 laps for testing purposes. Then plug the microbit into the Kitronik pre-built breakout board. Check that the cars run around the track and pass freely under the bridge. Press button A on the microbit and wait for the 5 LEDs to go out and start racing. Hopefully, the counters will work. When the correct number of laps have been completed the winner and its time will scroll out. Now try the False Start. Press button A and before the lights go out start car A racing. The LED at (2, 0) should come on to show the false start. So stop the car and the LED should go out. Then continue racing. Check car B in the same way.
- Download the zip file here.
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.