■ FIGURE 5. The slot car
Slot car sets, cars, and track components:
Circuit components: DigiKey.com.
Programming environment and interface
tools: Microchip Technology, Inc.;
Printed Circuit Boards: expresspcb.com;
Vehicle and Track
sensor, the photo transistor will go from reading 0 volts
DC back to reading 18 volts DC until it passes over the
next sensor. You may need to do some custom fitting to
get the track sensor to fit under the track section so that
the track will lay flat on whatever surface you have it on.
A few minutes with a Dremel tool will work great.
Next, hot glue the track sensor in place and solder the
positive end of the track sensor circuit (the red wire) to
the 18 volt metal strip on the outside lane. Solder the
negative end of the track sensor circuit (the black wire) to
the 18 volt metal strip on the inside lane of the track (see
Figure 4 and Schematic 2).
The photo on the left in Figure 4 shows where to drill
holes in the track so that the IR LEDs can be detected by
the photo transistor on the slot car. The photo on the right
shows the track sensor hot glued to the bottom of the
track section, so that the IR LEDs are directly over the
drilled holes. The track sensor is soldered to the positive
rail of the inside lane and the negative rail of the outside
lane. You may have to lightly sand down some of the
plastic underneath the sensor so it will sit flush under
Next, we are ready to program the
microcontroller. The main program will
consist of an initialization of the microcontroller, the setup of the PWM module, and
the track section counter. The track section
counter will be a basic counter that will start from one to
four every time the slot car passes through a section of track.
So, as the slot car starts from the starting line in Section 1,
the section counter in our source code will start at one.
Then, the code will go through a jump table that will take
it to a specific track section subroutine based upon the value
of the section counter. With the section counter having a
value of one, the source code will jump to track section
subroutine one and execute all instructions within that
subroutine, including incrementing the section counter. Next,
it will return to the main program, where it will go into a
search loop (TEST GP5) continuously looking for a 0 volt
DC drop, a binary zero or low, from the slot car passing
over a track sensor. After reading a track sensor, the program
will go to the jump table and, based upon the value in the
section counter, jump to that track section subroutine. This
process will continue until the program executes the subroutine for Section 4, where it will reset the section counter
back to one and the program starts over (see Figure 5).
Figure 5 shows the overall flow and order of the slot
car program/source code. I structured the program this way
to make it easier to expand the jump table and add subroutines for each new section of track (with
sensors) you may want to add in the future.
As you add more track sections, the number
of track section subroutines in your program
will increase proportionally, and the jump
table will also expand accordingly. Don’t be
discouraged if the slot car’s performance isn’t as
ideal as you expect from the start. You will have
to adjust the program delay times and duty cycle
values in order for the slot car to run at its best.
To compose and debug your microcontroller
source code, you can use the MPLAB® IDE
(Integrated Development Environment) which
■ FIGURE 6. Finished project.