The software was written in C using Microchip’s
MLAB IDE (integrated development environment) and the
Hi Tech PIC lite C compiler. The source code is available at
the article link. Essentially, the program uses the random
number generator in C, rand(), to provide an ever-changing light show by randomly choosing from 15
different patterns and two speeds. The program is simple:
First, the pattern and speed are chosen, then the pattern is
executed a set number of times. It then loops back to pick
a new pattern and speed, and executes the new pattern.
for operation at a household mains potential. With the
SSR board operating at five volts and inputs spaced at
0.2”, the interface to the microcontroller was quite easy.
(Refer to the sidebar).
The maximum current for each channel is two amps,
but with LED light strings you will be pushing much less
current through each channel. In fact, during initial testing,
I was concerned that one LED light string would not
present a large enough load for the triac to fully turn on
and off; fortunately, the switching worked well. I
recommend using LED light strings because not only is
their power consumption low, the colors that LEDs
produce are more brilliant than incandescent lights shining
through tinted glass.
Numbers in C
In C, rand() uses an algorithm to provide a series of
pseudo random integers between 0 and RAND_MAX (a
large number defined by the library). The algorithm is
started by rand() and uses a seed generated by another
function, srand(int s). By using different values of ‘s’ in
srand(), the series of integers generated by rand() are
different. Likewise, using the same values of ‘s’ in srand()
will produce the same series of integers.
The tree that is
used with this
controller has a
center pole from
which eight LED
light strings are
hung, flaring out
to form a tree-like
structure. Each LED
light string makes
two legs, giving a total
of 16 legs. A pleasing
proportion is for the
height of the center
pole to be twice the
diameter of the base
of the tree. I used an
eight foot pole with a
base diameter of four
feet. The middle of
each light string is at
the top of the pole,
with the ends at the
base. Each light string
is plugged into
controller sits on the
base of the tree.
You can see this for yourself by writing a simple
program that prints out the sequence of integers produced
by rand(). In this project, we ensure that the generated
series is different each time the controller is powered up
by adding one to the previously used seed which was
stored in the microcontroller’s EEPROM. The new seed is
then stored in EEPROM so it can be incremented at the
■ FIGURE A. Drawing of the
tree as viewed from the top.
A quick Web search on rand() will bring up several
discussions on the ‘randomness’ of it — especially when
restricted in a small range as used in this program. If you
are generating random numbers for encryption, unique
identifications, or other security sensitive applications, this
is a concern. In this application, by changing the seed
each time the light controller is powered up, the
randomness is good enough.
Eight sets of LED lights
result in 16 legs.
As noted earlier, rand() will generate a very wide
range of integers from 0 to RAND_MAX. What if you only
want a smaller range of integers (for example, 0 to 7) that
is used extensively in this project? The modulus — the
remainder after division — is used to restrict the range as
shown in this code for the RandomNumber function:
char RandomNumber(unsigned char range)
n = rand();
char rnmbr = n range;
■ FIGURE B.
the side view.
The legs are
the LED light
of an 8'
Here, the variable ‘range’ is (n+1) which restricts the
generated random integers to between 0 and n inclusive.
For example, for range = 8, the returned integer will always
be between 0 and 7 since you cannot have a remainder
greater than 7 when you divide by 8. For example, if the
remainder is 8, then the number is evenly divided by 8
giving the modulus of 0. In the code, we start each cycle by
choosing between two speeds by using RandomNumber( 2)
returning either a 0 or 1, and then choosing a pattern (or
46 December 2014