This is only a preview of the December 2018 issue of Silicon Chip. You can view 37 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "An incredibly sensitive Magnetometer to build":
Items relevant to "Amazing light display from our LED Christmas tree...":
Articles in this series:
Items relevant to "A Useless Box":
Items relevant to "El cheapo modules, part 21: stamp-sized audio player":
Items relevant to "Low voltage DC Motor and Pump Controller (Part 2)":
Purchase a printed copy of this issue for $10.00. |
by
Tim Blythman
So you’ve built a mammoth version of our LED Christmas Tree project from
last month (or at least you’re thinking seriously about doing so!). It’s huge and
has hundreds of LEDs. You want to make the tree do more than twinkle; you
want it to really attract attention! Here is how you can make the most of the
hardware, with some clever software to control it.
Y
ou would have seen our incredible stackable Christmas Tree
project last month. It cleverly
combines many small, low-cost boards
with eight LEDs each to form an illuminated tree of just about any size.
If you’re enthusiastic, it could easily
turn into the biggest SILICON
CHIP project you’ve built, so
we’ve created a program and
some more sample Arduino
code to help you achieve
that and get the best out of it.
The software presented
here allows you to experiment with your tree layout
without having to do any
soldering at all. It will show
you what your tree will look
like (up to a maximum of
ninety-nine boards), and
also tell you the order in
which their shift registers
are addressed.
This program also allows
you to generate Arduino
code (which is of course C/
C++ compatible) to create
patterns based on the phys38
Silicon Chip
ical and logical locations of the LED
boards and individual LEDs within
the overall tree. This means you can
generate patterns such as light radiating up the tree from the base, moving
side-to-side, star bursts or various other geometric patterns.
But wait, there’s more! No, you don’t
get a free set of steak knives. But the
software presented here can also interface to the Christmas LED Tree via the
Digital Interface Module and send it
commands to control an attached tree.
You can click on individual LEDs and
watch them turn off and on in
real time.
You can also use it to generate the commands for a given
illumination pattern, allowing
it to be delivered to the Tree
later, using separate software
(eg, Arduino or BASIC code).
And in case you haven’t
built the Digital Interface Module but have a spare Arduino
board lying around, we’ll present some Arduino code to allow you to emulate some of
its basic features, so you can
use that Arduino to drive your
tree with these more advanced
patterns.
LED Tree Data Map
Program
This application is written
Australia’s electronics magazine
siliconchip.com.au
in the Processing language, which also
happens to be the origin of the Arduino
programming language. If you haven’t
heard of it before, see the panel at right
for more information.
The general idea behind the LED
Tree Data Map Program is that the
graphical interface gives you a virtual
view of your Tree.
You build it by adding tree boards
on top of existing boards, by clicking
on the branch location where you want
to add them.
This is useful both for experimenting to see what size and shape you
want to make your tree but also, once
you have built it, you can create an
identical tree in the software, which
then produces the data you need to
drive its LEDs in various patterns.
Installing the software
We’ve created pre-compiled Windows and Linux versions of the LED
Tree Data Map Program. The Linux
version has been complied for three
platforms: x86 (32-bit), x64 (64-bit)
and Raspberry Pi. All four versions
are available for download from the
SILICON CHIP website.
There is no installation as such; you
just need to extract the relevant executable from the ZIP archive to a folder
on your computer and then run it. But
since Processing is based on Java, you
need to have the Java Runtime Environment installed on your PC to run
the compiled programs.
The easiest way to ensure that you
have an appropriate version of Java
installed is to download and install
Processing.
You can get it from:
https://processing.org/download
We haven’t provided a compiled
Mac version of the software since we
don’t have the hardware to do so. But
if you have a Mac, you can use the Processing software to compile the supplied source code.
You can also use the Processing software to make changes to our software
and re-compile it if necessary. We used
Processing version 3.37 to create and
test the program.
What is “Processing”?
The clever little program we have put
together as part of this article has been
written in a language called Processing.
You may not have heard of it, so let us
explain. . .
Processing is a programming language
which is designed to allow people to easily create visual content. It is an opensource, cross-platform project, meaning
that anyone can get a copy of the source
code and it’s designed to run on a variety
of different operating systems. It can even
run on the Raspberry Pi and some other
single board computers.
If you want to create an app for your
phone or tablet, there’s even an Androidcompatible mode, although we haven’t
tried it ourselves.
We have never really needed to use
its particular features before. But in this
case, being able to create a graphically
interactive and intuitive program was the
deciding factor.
The Processing website at https://
processing.org/ says that it is designed
“for learning how to code within the context of the visual arts”.
While that might seem a poor fit for an
electronics magazine, it happens to suit
us very well since it means that we can
easily depict and manipulate the physical
layout of hardware on-screen.
By the way, the language used in the
Arduino IDE is called Wiring and is built on
the Processing language. If you are familiar
with Arduino programming, you will find
that the Processing IDE (Integrated Development Environment) is nearly identical to the Arduino IDE, apart from the colour scheme.
So it seems Processing has a similar
role in teaching graphical programming
as the Arduino does for teaching embedded programming.
Processing is written in Java and when
it compiles projects into stand-alone executables, they run on the Java platform as
well. This was another reason we chose
processing. While the majority of our readers run Windows, we don’t want to exclude
those who have a Mac or run Linux.
And since we could create a stand-alone
version of our program, you don’t need to
install the IDE to use it.
The language used is Java, which is
similar to C/C++, so programmers familiar
with those languages (or the many similar procedural languages which have been
inspired by them) should have no trouble
adapting.
There are some some small differences;
for example, the #define and #include “preprocessor directives” are not used, but you
can import Java libraries, which we have
had to do to add clipboard functionality to
our program.
Building a tree
When you first open the program, a
single LED Christmas Tree board appears at the bottom of the window.
When you move the mouse cursor
to a location where clicking will lead
to an action, a circle is shown. A large
siliconchip.com.au
The Processing IDE looks very similar to the Arduino IDE. You can even see
some of the language similarities, eg, the ‘setup()’ function.
Australia’s electronics magazine
December 2018 39
Fig.1 (left): when the software is launched, a
single “root’ board is present. Simply left-click
on the location where you want to add another
board and it will appear. Right-click to
remove it. The white dots also indicate which
LEDs have been toggled on by clicking.
Fig.2 (right) : here’s a representation
of the 38-board version from last
month’s front cover. It only takes a minute
or two to set it up. One useful aspect of this
software is you can see whether any boards
would overlap in your design – and you can
even test a tree up to 99 boards in size to see
how it would look.
green circle appears in places where
you can add another branch to the tree
(see Fig.1). As the tree gets bigger, you
can use the “=” (+) and “-” keys on
your keyboard to zoom in and out and
the arrow keys to resize the window.
The program automatically assigns
a number to each PCB, which is displayed on top of that board. This indicates what order the boards receive
data as it passes through the shift registers on each board.
This assumes of course that any unconnected ends have their DO and DI
pins bridged, as explained in the article last month.
You can remove branches (one at a
time) in reverse order by right-clicking
instead of left-clicking. This allows you
to “backtrack” which is handy if you
make a mistake but also useful if you
are experimenting to see which of various different tree configurations is best.
The program assumes that the
boards are simply butted against each
other rather than being spaced slightly as if they were fitted with headers.
But given that you can plan with
precision how the tree will look using
this software, it is well-suited to creating a permanent arrangement, with
the boards joined by short lengths of
stiff wire.
Fig.2 shows the large tree in the introduction of last month’s constructional article re-created in the LED
Tree Data Map program.
Driving the Tree directly
If you’ve built the Digital Interface
Module and have some LED Christmas Tree boards connected to it, you
can control this combination directly
from the LED Tree Data Map Program.
Press the “,” (<) and “.” (>) keys on
your keyboard to scroll through the
displayed serial ports until the port
corresponding to the LED Tree Control Board appears, then press the “s”
40
Silicon Chip
key to connect to it.
The port name turns green if connection is successful. Assuming the
physical layout matches the layout you
have created in the program, clicking
on one LED on the screen will cause
it to toggle on and off, both on-screen
and on the actual board. Of course, if
the two layouts are different, anything
could happen!
A handy feature is that you can press
the “t” key to copy the current state of
the LEDs to the clipboard, from which
it can be pasted into a text editor for
manipulating. This data is in the form
of hexadecimal digits preceded by a
“v” and followed by a “V” to match
the 9600 baud HEX SPI format of the
LED Tree Control Board.
A simple way to use this data is to
paste it into a serial console program
(such as TeraTerm, PuTTY or even the
Arduino Serial Monitor), which will
then send it on to the Digital Interface
Module and on to the Tree.
You could save a number of these
Tree states to a text file in order and
send them to a serial port using an-
other program, or even the following
command from a Windows command
prompt (assuming your file is called
“test.txt”):
copy test.txt \\.\COM30:
Making a map of the Tree
The software does not have any
functions to save an image of the tree
you have created but you can use your
operating system’s screen capture
function to make a copy of the map
once you have settled on a layout. In
Windows, you can do this by pressing
ALT+PrintScreen and then loading MS
Paint (or another image editing program) and pressing CTRL+V. You can
then save the resulting image to a file.
This is a good idea, so that you will
remember exactly where to wire the
boards when you are building the tree
(if you haven’t already).
Pressing the “c” key on the keyboard
copies the current layout information
to the clipboard, in the form of Arduino code. We’ll now explain how that
can be used.
Controlling the Tree with an
Arduino
In the constructional article last
month, we explained how to use a basic Arduino sketch to make the LEDs
in the tree twinkle. But with the code
created using the “c” key, you can do
much more. Once it’s in the clipboard,
the generated code can be pasted directly into a blank Arduino sketch created in the free Arduino Integrated Development Environment (IDE).
If you haven’t used the Arduino IDE,
you will need to download it from the
#define LED_BOARD_COUNT 4
int led_board_rotation[LED_BOARD_COUNT]={0,-1,0,1};
int led_board_depth[LED_BOARD_COUNT]={0,1,1,1};
int led_board_x_coord[LED_BOARD_COUNT]={320,262,320,378};
int led_board_y_coord[LED_BOARD_COUNT]={639,516,490,516};
#define LED_PIXEL_COUNT 32
int led_pixel_x_coord[LED_PIXEL_COUNT]={
331,333,341,348,320,285,300,309,256,234,222,203,173,154,196,234,
331,333,341,348,320,285,300,309,399,424,448,476,465,436,416,390
};
int led_pixel_y_coord[LED_PIXEL_COUNT]={
619,586,561,527,514,521,565,610,495,470,446,418,429,458,478,504,
470,437,412,378,365,372,416,461,510,488,476,457,427,408,450,488
};
Fig.3: sample data generated from a four-board tree. This includes
information about the position of each board and LED in the tree, which the
Arduino (or other) software can then use to calculate which LEDs should
turn on when, to give particular patterns of light.
Australia’s electronics magazine
siliconchip.com.au
Fig.4: the design for
the small nine-board
tree, used to demonstrate some of the
patterns that our
code is capable
of generating.
The program reports
the board numbers in
logical shift register
order, as well as how
many LEDs and boards
are needed for construction.
following link: www.arduino.cc/en/main/software This
program is used to write “sketches”, as Arduino programs
are known, as well as upload them to an Arduino board.
The web page at www.arduino.cc/en/Guide/HomePage
explains the basic workings of the IDE and Arduino-compatible boards. For the examples below, you just need to
load the sketch file, select the correct board type and port
from the “Tools” menu and then click the “Upload” button to test the sketch.
We’ve created a few sample sketches to show how to use
the data from the LED Tree Data Map Program. Fig.5 shows
the wiring required to connect the Arduino board to your
Tree. We used an Uno clone for our tests but these sketches
should work on just about any Arduino-compatible board.
The connections are as follows:
Arduino
5V
GND
D2
D3
D4
D5
ARDUINO
UNO
‘Root’ Tree board
5V
GND
DI
DO
LT
CK
Fig.3 shows the data generated for a simple case of four
boards, with one sub-board connected to each of the three
branch connections on the root board.
The first line, starting with #define, simply tells the program how many boards are in use. This value is also used
to dimension the following arrays which contain information about the location of each board.
The second line defines the “led_board_rotation” array
which contains an integer value for each board indicating
the orientation of the board. A value of zero means the
board is parallel to the root board, while negative values
indicate anti-clockwise rotation and positive values indicate clockwise rotation, in multiples of 45°. So +1 = 45°
clockwise, +2 = 90° clockwise, -1 = 45° anti-clockwise, etc.
The third line defines the “led_board_depth” array which
indicates how far each board is from the root board. The
root board depth is zero, the boards connected directly to
the root board have depth one and so forth. This is a handy
approximation to the vertical position of each board.
The fourth and fifth lines define arrays named “led_
board_x_coord” and “led_board_y_coord” which give the
cartesian coordinates of the bottom middle of each board.
The root board is always at 320,639 with the values ranging from zero up to 639.
Increased x values indicate boards which are further to
the right while decreased y values indicate boards which
are further up.
While these values could potentially be useful in some
cases, most patterns would use the individual LED coordinates which are what is provided by the remainder of
the code.
The second #define indicates how many total LEDs there
are in the tree (which is always eight times the number of
boards) and the final two arrays contain this many integers. Those integers are the cartesian coordinates of each
LED, in order from first to last in the shift chain, using the
same coordinate system as described above for the boards.
Using some simple calculations, you can create some
amazing patterns by checking the coordinate of each LED
and determining whether or not to turn it on, based
on various geometric patterns.
Example sketches
We have provided five sample sketches, to show off some of the patterns that
it’s possible to generate once you have the
data for your Tree.
These are just the starting point; you could
use them as-is or you could expand on them, to
CHRISTMAS
TREE
make even more interesting and spectacular patPCB
terns. You could even combine them into a single
5V
sketch which can cycle through several different
PIN
patterns
over time.
GND
Our first example sketch is named “9_Board_Tree_
PIN
D1
rotate_scan.ino” and it illuminates each group of
D0
PINS
LEDs
in a tree depending on their rotation.
CK
2-5
LT
Although this won’t strictly give a left to right motion if your tree has loops or reverse curves, it still provides an interesting display.
While as the name suggests, it contains the data to
Fig.5: this shows how you can drive the LED Christmas Tree
suit
a Tree made from nine separate boards (as shown
from an Arduino. While we gave a simple test sketch at the time,
this month we’re also providing a general-purpose interface sketch in Fig.4), it is not limited to being used in this way.
You can use it with a Tree made from any number
which allows this configuration to work with our new software.
42
Silicon Chip
Australia’s electronics magazine
siliconchip.com.au
of boards in any configuration.
You simply need to replace the coordinate definitions at the top of the
file with those generated from
your Tree design, using
the method described
above.
Nine boards were
chosen simply because we felt that four
boards were not enough
to really show off the
features of this software.
You can change
the data to suit your
tree, of any size, in
each of the five examples
provided.
The code will adjust to
suit your Tree, as it determines the minimum and maximum
values at runtime in the setup()
function.
The second example sketches is
called “9_Board_Tree_depth_scan.ino” and
it first illuminates all the LEDs on the root
board, then on all the boards with depth =
1, then depth = 2 etc.
This gives the effect of light shooting up and out
the tree branches. Its code is virtually identical to the
first example, with just one line changed.
The third and fourth examples are called “9_Board_
Tree_x_scan.ino” and “9_Board_Tree_y_scan.ino”. These
work similarly; the former causes LEDs in the tree to light up
from left-to-right, then right-to-left, creating a vertical line
of light which moves across the tree and it repeats forever.
Similarly, the latter causes a horizontal line of LEDs to
light up from bottom-to-top and then top-to-bottom.
The final example is the most complex and this is “9_
Board_Tree_starburst.ino”, which causes the LEDs in the
middle of the tree to light initially, and then the light spreads
outwards in growing circles.
Saving memory
If you have a large tree, you may run out of RAM to store
the resulting large data arrays.
In that case, you would need to add “const PROGMEM”
to the start of each line defining an array. That will cause
them to be stored in flash rather than RAM.
But note that you will also need to make changes to the
way that the program accesses the data; but we won’t go
into detail on that aspect here.
If you want to see some examples of how to access PROGMEM variables, refer to: www.arduino.cc/reference/en/language/variables/utilities/progmem
General purpose Arduino control board
Finally, we’re providing an extra Arduino sketch which
provides an interface between the LED Tree Data Map Program and an LED Christmas Tree, even if you haven’t built
the Digital/SPI Interface Module described last month.
You just need an Arduino board and some jumper wires.
siliconchip.com.au
It only works with the 9600 baud HEX SPI mode
described last month for the Digital Interface
Module, as there isn’t an easy way for
most Arduino boards to detect the
baud rate their hosts are using. But that’s certainly
good enough to do some
testing or maybe even
drive a Tree in a Christmas display.
Connect the tree to the
Arduino using the same
wiring as the previous example (Fig.5)
and load the sketch,
which is called “Arduino_HEXmode_
Tree_emulator.ino”.
Like the other sketches
provided, it should
Fig.6: this
work on most Arduidemonstrates no boards.
that you don’t
The procedure for
need to be
selecting the serial
celebrating Christmas
to build this project – port in the software
you can turn it into a is the same as described above; use the
Hanukkah menorah
“,” (<) and “.” (>) keys
instead!
to change the serial
[See https://en.wikipedia.
org/wiki/Menorah_(Hanukkah)
port, then press “s”
for an explanation of why it has
to connect.
SC
nine branches.]
MUSICAL
CHRISTMAS
STAR
DIY PROJECT
FROM PICOKIT
SC
Create some holiday cheer
with a DIY Musical Christmas
Star project from PicoKit. This soldering kit has
movement sensing to light some colourful LEDs and play
some Christmas carols.
Optionally, you can upload your own songs with the PicoCODER programming cable from PicoKit (just $15 extra if bought
together with the PicoSTAR).
The PicoSTAR kit is supplied with a pre-programmed
PIC12F510 from Microchip Technology and is compatible with
PicoKit’s own ALPHA Code programming software and coding
with C and ASM languages through the MPLAB-X software from
Microchip Technology.
PicoKit - Maker Space Solutions
for
al OffeHrIP
SpeciIC
C
SIL OdNers*
ea $ 17
R
$ 22
P
P&
GST &
PLUS
bsite
*see we ils
for deta
Australia’s electronics magazine
Upper Caboolture 4510, QLD
Phone: 07 5330 3095
www.picokit.com.au
December 2018 43
|