■ FIGURE 7. LED and resistor. ■ FIGURE 6. Modified LED.
prop or set. Figure 9 shows a small candle created with
my LED wick and a piece of 1/2” PVC pipe.
Programming a Jack-o-Lantern
Even if you’re fairly new to using Spin, you should find my
demo program easy to modify after I’ve run you through the
explanation. For those that are using the Propeller for the first
time, I think you’ll find its behavior and programming interesting.
The program is composed of three elements: 1) the
main code; 2) a dimmer module to control LED
brightness; and 3) a flame effect generator.
Each of these modules runs in their own cog
(processor) inside the Propeller so we don’t have to worry
about managing interrupts (the Propeller doesn’t have
them) or other time-slicing strategies; trust me, multiple
processors on one slab of silicon makes life very pleasant.
When the Propeller boots up, it will load the Spin
interpreter into Cog 0 and run our main program. Inside
that program, we will instantiate the other cogs. The first is
a dimmer cog that runs jm_bam8; this is used to modulate
the eight LED outputs using a process called Bit Angle
Modulation (for details on how BAM works in the Propeller,
see The Spin Zone column in the November ‘09 issue of Nuts
■ FIGURE 8.
Final LED wick.
■ FIGURE 9.
& Volts). We don’t
have to understand
how BAM works to
use it, just what to do
with the object.
The first thing we
need to do is include
it in the code. This is
adding it to the objects
list in the main program:
leds : “jm_bam8”
This includes the
file jm_bam8.spin in
the compilation and
creates an object called leds. Most objects that deal with I/O
will require some sort of setup; usually with a method called
start() or init() — the latter is what I tend to use in my objects.
To instantiate leds, we’ll call its init() method with the first
pin of an eight-pin group; in my program, there is a constant
called WICK1 that is that pin (0 for the hardware we just built):
This line of code launches the BAM processor into
Cog 1 and will continue to run until we stop it with the
finalize() method. To change the brightness of any of the
LED wick outputs, we can use the set() method with a
value from 0 (off) to 255 (full brightness). Having just
discussed the BAM object and knowing that our program
needs a flame generator, you may be wondering why that
object is not in the object list above. Let me show you.
Here’s the main loop that runs my Jack-o-Lantern:
pub main | ch, bright, delay
ITEM DESCRIPTION SOURCE/PART#
LED1-LED8 Amber, 5 mm NTE30039
Q1 2N3904, NPN Mouser.com 610-2N3904
R1-R8, R19 470 ohm Mouser 291-470-RC
R9-R16 330 ohm Mouser 291-330-RC
R17 2.2K ohm Mouser 291-2.2K-RC
R18 10K ohm Mouser 291-10K-RC
S-S1 Socket, 18-pin Mouser 571-1-390261-5
TB1-TB7 Term block Mouser 571-2828362
U1 ULN2803A Mouser 511-ULN2803A
X1, X2, JP1 0.1 M-STRT Mouser 517-6111TG
JMPR 0.1 shunt Mouser 517-950-00
Propeller Proto Board Parallax.com 32212
Parallax 32812 (USB)
Heat shrink tubing
Clear (bathroom) silicone
Acrylic paint (yellow or orange)
lottery := cnt
bytefill(@level, 0, 8)
repeat ch from 0 to 2
repeat bright from 0 to 255
level[ch] := leds.ezlog(bright)
repeat bright from 255 to 64
flickerdelay := (255-bright) 52 / 10
delay := (||lottery//8001) + 2000
flickerdelay := 0
repeat ch from 0 to 2
level[ch] := 255