SlotCarDuino : Create a lap timing system. Minutes, Seconds, tenths of a second to be displayed with 7-segment LED digits. Race starter lights being an optional extra.
Here’s how it turned out :
Read more after the break…
For Christmas 2009, as part of the fund-raising activities, the local school holds a fair in which games are played, gifts purchased, face-painting, tumbola … etc. You must know the thing. Lots of noise, kids spending pocket-money on sweets … etc. Overall, it’s a fairly-large fund-raiser for things like extra musical instruments, or extra nice-to-have facilities at the school.
Why write about this here ? Well, Mrs techcobweb and I have been asked to create a game based on slot-car racing. Set up a track, charge for a go racing the cars around our track. The idea is to time the laps, and record “top-gear style” the best times. Best lap time from 3 laps or so. Prizes for the best in age-range (keys stage 1, key stage 2 and “the rest”).
The hope is that competition to be the best will spur people to play multiple times to get their lap time higher up on the score-board, thus earning the school association more cash.
I want to accurately time laps, so I need something cool to do it. I didn’t want to buy any commercially-available timing unit specifically designed for the purpose, that’s too easy, no fun at all, and I wouldn’t learn anything as a result.
Instead, I thought I’d use it as another excuse to learn some more about simple electronics.
First thing, make some decisions to limit the scope of the project :
- I’d ignore the “digital” scalextric system. We would not allow cars to switch lanes, so I only need to detect whether a car passes in a certain lane, rather than which car passed in which lane.
- I’d use an infra-red beam, mounted vertically, which is broken by a car passing the finish-line to detect the start/end of laps.
- I’d use an arduino board as the microprocessor (or freeduino in this case, as I had one already).
Step 1 : Infra-red beam breakage
I got hold of a simple IR detector, and IR-emitting LED. Put a current through the detector, and read the voltage it lets through using analogRead() on the arduino, and you can tell if the IR light beam is intact or not.
Tip : You can’t see an IR beam with the naked eye, but if you use your mobile camera, where you can see a preview of what you’re pointing at, you can see it.
The IR emitters need to be always-on. So they only need a ground and +5v connection, and a resistor. I used a 55ohm resistor for this, safe enough I hope given the data sheet for the emitters.
These are mounted in a “gantry” above the track surface.
The track I used was one piece from the 2-piece pack of track interchange. It seems that scalextric track changed from the old system to the new system of connecting stretches of track together about 4 years ago. My thinking was that if I did the electronics timers in one of these pieces, then it can be connected to the old or the new track systems.
IR detectors are mounted on a small piece of vero board beneath holes cut into the track surface. I was surprised how soft the track was to cut, so I could do it easily with a simple craft knife.
The detector cable from the IR sensor plugs into an analog pin on the arduino. High readings when there is lots of IR light around, low when the car passes bye. Of course, everything is doubled-up to allow timing for two lanes of racing traffic. Software wise, I found that a single reading is not highly reliable, so I ended up taking the last 3 readings and averaging them to get the value to use.
Step 2 :Race starter lights
These are bi-color red-green LEDs, 2 anodes (one for each colour), and a common cathode. 2 wires connect these to digital pins on the arduino so they can be switched red, green or off on demand.
The lights themselves are then mounted in the same gantry ducting that houses the IR-beam emitters.
Step 3 : LED segments showing the timers
The “arduino playground” has lots written about how to control 7-segment LEDs. After reading these, it looked like Maxim 7219 chips would do what I wanted. Couldn’t find any suppliers except RS Components. They have fairly high delivery rates, but as I couldn’t find that part anywhere else there was little choice.
The LEDs themselves and all other parts came from BitsBox, an excellent site which only charges £1.50 per order.
Laying out a breadboard circuit using the 7219 chip, and 8 digits of 7-segment LEDs wasn’t easy. It took nearly 2 hours to cut all the wire and “weave” it together. By far the most complex things I’ve done electronically so far. Anyway, it seemed to work. I used a simple sketch to show the time since the board was last reset.
This amount of wires took 2 hours to lay out neatly on the breadboard. More like sewing than anything I’ve done since I made a cushion cover at school.
Step 4 : Software, controlling inputs and outputs
Now things are getting exciting. Connecting together the input sensors with the output starter-lights on the gantry and the two 4-digit timers was straightforward. I added a switch to control the selection of “numbers of laps”, “number of players”, and “which lane if there is only one player”, then I needed to write some code to control it all.
As I’m not using interrupts to latch the detection of a broken beam, I need all the code to be non-blocking. The libraries available on the arduino site contain Finite State Machine, and Scheduler, LedControl, Button and LED, all of which are useful for the programming.
After finding a bug in the FSM (Finite State Machine) library I was delighted when the author provided a fix overnight ! (Thanks Alexander Brevig).
Step 5 : Switches and software control
- I opted for 3 switches. Reset (to reset the arduino), Select and Mode.
- The reset switch needs to connected to the Reset line at one end, and the ground at the other. Couldn’t find that written anywhere in the arduino forums. Maybe it’s just obvious to most, but it wasn’t to me, so took some experimenting.
- The other two switches are mounted on a piece of wood, connected to +5 via a 20kOhm resistor at one end, with the connection to the input pin to the arduino at that end of the switch, with ground on the other pole of the switch.
- ie: A pull-up resistor. Someone said that a 20k creates less power drain than a 10k, so I thought I’d try that.
- When the software uses this, it also enables the in-built 20k resistor in the arduino hardware.
- Both the Select and Mode switches are de-bounced using the Button library.
Step 6 : Complete the software
At this point, I had little sketches to exercise all the small pieces of the solution. Now it’s time to pull it all together.
I created a state machine, which had a few states :
1. Setup number of laps state
- Select button adds a lap to the lap count, wrapps from 9 to 1.
- Mode button moves to state 2.
2. Setup number of players and lane used state
- Select button toggles between:
– 2 players, both laps
– 1 player, left lap
– 1 player, right lap.
- Mode button switches to state 3
3. Start race mode
- Flash the red starter-light LED on for a second, then off. for a second. Display “3” on all digits of all timers.
- Do it again, using a “2”
- Do it again, using a “1”
- Blank the timer screens
- Put the green “go” lights on.
- Schedule an event to turn the green light off a bit later, regardless of our state.
- Move to state 4.
4. Timing the race.
- Collect lap-times for each lane.
- When all lanes in use for this race has finished, move to state 5
5. Display the best lap time above each lane in use. Flash this on-off with a second frequency, so the user knows it’s the overall best lap time, rather than just the last one.
- Mode button moves to state 1, so the whole race can start again.
So after a while getting this code nearly right, I was able to throw a post-it note through each lane to pretend that a car was passing over the lane.
Here is the arduino sketch : scalextric_lap_timer_sketch_v1t
Step 7 : First complete physical fitting
- Soldering together a “timer gantry”. I found a piece of plastic “cable shield” was once again useful here. I could just about get a row of LED digits within the channel inside the shielding. It’s sticky-backed, so could easily be added to the original lighting gantry.
- Creating the switch frame, soldering the wires to the central circuit board.
- Soldering-together the central circuit-board, which collects all/most of the wires from the board.
- Fasten the arduino to the board, using risers which accept a screw at both ends.
- Attach the battery to the board, using a Velcro pad. Solder the batter connector to a power plug. (2.2mm, +ve is central terminal).
Step 8 : Hiding the gawp
Add some structure to fill-out the board a bit
Decorate with some “stone wall” features from a previous “plaster mould” project
Cover the electronics kit in a plastic bag for protection of what’s coming up…
Get some raw materials together :
office shreddings, placed on top of the structure, on which strips Mod-rock plaster is placed.
Paint matt black :
Then “dry brush” with grey, then white :
Then add some grassy bits :
Parts List :
- 3 x switches
- 4 x 3mm red LEDs
- 1 x ??uj capacitor
- 1 x ??nj capacitor
- 1 x maxim 7219 c?? chip
- 1 x 16-pin integrated circuit mount
- 1 x 8-pin integrated circuit mount
- 2 x IR emitters
- 2 x IR receivers
- 1 x freeduino
- 2-3m wire
- 10cm x 12cm vero board to cut up
- 1 x 2.2mm power jack
- 60mm of cable ducting
- oddments of wood
- 30cm x 30cm hardboard base
- a dozen or so screws
- wood glue
Someone asked me “how do you know it’ll detect actual cars given that you’ve never hooked it up to a track before”. Well, I wrote a test program which counts how many sensor readings are taken as an object passed over the sensors. Then it got reported over serial. I found I could flick a post-it note through the gantry and have sixty readings as it flew past. Flicking a pencil as fast as I could (width-wise) registered a “broken beam” for 10 readings. So I figured as a car was longer, it would register as breaking the beam for much longer.
I finally got someone to come over and set some track up. A slot car running at full-speed (via a very long straight) registered the beam as being broken for 140 readings.
So I now know it works, and will not be too slow for a car to pass through without being detected.
It doesn’t seem to run for long with a standard 9v battery, as wierd things start to happen (lanes don’t register beam breakage). It’s fine to start with, but when the battery voltage drops to 7.2v the display driver stops working. I’ll use a wall-plug on the day I think, which isn’t a problem, as the scalextric needs a plug anyway.
204 solder joints. Whew !
Estimated cost of around £38. Which is more expensive than the official £26 retail version, but loads better than a small LCD screen, and this one does exactly what we want, and doesn’t look “plastic”…etc.
I enjoyed the hardware debugging phase a fair bit. Learned a bit more about capacitors, wiring layout, physical mounting.
The code I wrote is a mess. Need to get back into C++ to make it more object-oriented instead of using arrays.
Great fun building it.
I’ll update this post with feedback from when it gets used for real.
Post-script : How did it work on the actual event ?
So December came, and the weekend of the school fair arrived. Up fresh and early, with a car full of borrowed track we headed to the school. (No pictures as I didn’t think to get many, and I don’t want to show kids’ faces without permission..etc).
We weren’t short of helpers putting the track together. All the over-30’s were getting misty-eyed at the sight of the old-style cars. All the youngsters just wanted to see the biggest track most of them had ever had a race on.
All the help was rewarded with more “test laps” than absolutely necessary. The track itself had about a 10-second lap time with new magnet-enabled, can hardly drive them too fast off the corners, about 20 seconds with older non-magnet cars. Given that we had a selection of borrowed cars available, I tried them all. I found that the new cars are really dull/boring. They don’t come off easily enough. Completing a lap without skidding off wasn’t the challenge I remember from my 10-year-old days. I tried all the cars from the vintage mini, audi quatro, porche 9-11 and some tyrell F1 car. The most fun was had with these older cars. Get the speed just right, and the back would swing-out and you could “drift” around the corners, power-sliding all the way.
Then the crowds arrived.
It wasn’t long before I was grateful for the new cars. Because they didn’t come off the track very often we didn’t need a legion of helpers to recover cars from crash sites.
Practical point : We should have put a lower-age limit on the stand. When kids too small to understand the difference between “full-on” and “gently squeezed” had the controls every corner was accident-city. Putting crash barrier on the outside of all of the corners really paid off. Another thing we did was to race red and blue cars. We put red/blue stickers around the track so helpers could tell which lane to put cars onto when they came off. This is sometimes confusing, and can lead to situations where 2 cars are on the same lane or the wrong car passing under the gantry in the wrong lane (giving that lane owner a really great time!).
The “driver registration” process on the stand took ages. If anyone does this where the budget is higher, get yourself some RFID detector to register your driver with the machine. Sometimes the track was idle while we took driver details. Eventually we got it sorted.
Overall, the timer worked well. Some glitches (there must be some bugs still left in the code), but overall it worked.
The stall required loads of effort to keep it running, so it was really hard work continuously. As a result I was glad when the call went out to clear up.
I have no idea how much money was made. Not much. Lucky if we covered the cost of the prizes for “best lap time in age-group”. But it was hugely fun, and kids (pre-teens and 30+’s alike) said what a great success it was.
I’ve since been blamed for several kids demanding slot-car sets for Christmas. Especially with the James May “toy stories” episode on slot-cars nearly coinciding with the event. Shame none of them will get a lap timer like this !
Whew ! Wonder what we’ll need for the next event !