This is only a preview of the February 2022 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
Max’s Cool Beans
By Max the Magnificent
Flashing LEDs and drooling engineers – Part 24
W
hat’s that, you say? You
want to hear a joke? Well, by
some strange quirk of fate, I
happened to hear something humorous
on the radio on the way into work this
morning. A young lad calls his mum
to say: ‘Remember how my teacher
said I’d never be any good at poetry
because I’m dyslexic? Well, I just made
two candlestick holders and a vase,
and they look great!’ Stop groaning.
You’ll be telling this yourself before
the day is out.
40
44
39
38
24
25
26
41
9
18
27
2
10
17
16
1
8
15
37
23
36
7
14
35
22
43
34
3
0
6
5
13
29
4
11
12
20
30
31
42
21
33
28
19
32
Wow! Wireless LEDs
Just when you think you’ve seen it all,
along comes something new to surprise
you. Recently, for example, a friend introduced me to the YouTube channel of
someone who goes under the moniker of
Atomic14, which is sort of clever because
14 is the atomic number of silicon. While
I was meandering around this channel,
as is my wont, I saw a video showing
wireless LEDs: https://bit.ly/3y5HUlX
It has to be said that these little scamps,
which are available for purchase from
AliExpress (https://bit.ly/3IyqpiN), look
rather spiffy (Fig.1.). I can easily imagine using these to create some little gadgets, gizmos, doodads, and doohickeys
to entertain young kids – or even (especially) old kids like myself, now I come
to think about it.
Fig.2. SMAD segment map (left) and board annotated with rings (right).
multiple potentiometers in parallel. So,
you can only imagine my surprise to discover that most of the emails I received
regarding that column asked, ‘But what
about SMADs?’
In fact, while I’m thinking about it, I’m
even receiving emails from someone in
Russia who used our SMAD schematic
diagram to build a giant SMAD about
500mm square out of discrete WS2812
tricolour LEDs. He constructed this with
his daughter, and they are constantly
clamoring for more code examples.
Good grief! It’s not like I’ve forgotten
these bodacious beauties. It’s just that
I’m juggling too many balls and I can’t
juggle. Actually, that’s not strictly true
– I can juggle ten fine china plates, but
only for a very short period of time; that
is, the time it takes for them to go up in
the air and come back down again (hey,
it’s a start).
In previous columns (PE, October and
November 2021), we discussed a variety of simple windmill-type effects and
an alien countdown timer effect. We
Fig.1. Wireless LEDs – I almost believe they were invented specifically for me...
presented these effects on two pseudo
robot heads, each of which is equipped
with two SMADs as eyes. The SMADs on
one of these heads are augmented with
29-segment shells, while the other head
flaunts 45-segment shells. Two columns
ago (PE, December 2021), we had our
heads talking to each other in Morse code.
In fact, we even have a video of this taking
place (https://bit.ly/3oBwemz). Now it’s
time to get a little more adventurous. As
a starting point, we might decide to consider the LEDs on the SMADs as being
presented in the form of five concentric
circles or rings (Fig.2).
Do you recall the way in which we defined the ‘patterns’ in our windmill and
countdown effects as a two-dimensional array called EffectMap[][]? Well,
we’re going to use the same technique
here (Fig.3). The first dimension specifies the number of rows in the array. This
corresponds to the number of patterns in
our effect, which will be the five rings,
in this case.
The second dimension specifies the
LEDs that appear in each ring. Ring 0 in
the center of the SMAD is formed from
just one LED (shown as 0 in Fig.2). Ring
1 is composed of eight LEDs (shown as 1
through 8 in Fig.2). Rings 2 and 3 each
contain 16 LEDs. And Ring 4 boasts only
four LEDs. This means that the maximum
number of LEDs in any of the rings is 16.
The reason we set this dimension in the
array to be 16 + 1 = 17 is because we use
the first element in each row to define the
52
Practical Electronics | February | 2022
I’m a SMAD boy!
We covered a lot of ground in my previous column (PE, January 2022), including toroidal transformations, gas-heated
soldering irons, 1970s oil-wheel disco
lighting effects, the 4-axis joysticks we’ll
be using to control our servo-driven
robot heads, and some interesting considerations with respect to connecting
# d efine N U M _P A T T E R N S _I N _E F F E C T
5
# d efine M A X_L E D S _I N _E F F E C T
16
const uint8_t E ffectM ap [N U M _P A T T E R N S _I N _E F F E C T ][M A X_L E D S _I N _E F F E C T + 1] =
{
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, / /
{ 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0}, / /
{16, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, / /
{16, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, / /
{ 4, 41, 42, 43, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} / /
};
Fig.3. Specifying which LEDs appear in what rings.
R ing
R ing
R ing
R ing
R ing
0
1
2
3
4
by the folks at Adafruit (Fig.5). We can
use this table to remap the linear values
we would like to use into their gammacorrected counterparts that will provide
us with the colours we want to see.
Personally, I thought our original
gamma correction evaluations were rather
successful, so I decided to apply this to
our most recent SMAD experiment. In
this case, for each robot head, I displayed
the non-gamma-corrected colours on one
SMAD and the gamma-corrected colours
on the other SMAD.
One point that gave me pause for
thought was that, in our original tests,
we started off with our LEDs at full brightness insofar as the values required to represent the selected colour. When it comes
to our SMAD experiments, however, I’ve
been using a ModifyBrightness()
function to lower the brightness of the
LEDs so I don’t wash out the sensor in
my camera while taking videos.
This led me to wonder... should we
modify the brightness and then perform
the gamma correction (this is the way
you’ll see things in file CB-Feb22-04.
txt), or should we do things the other
way around. In the end, it didn’t matter,
because the results looked horrible both
ways. I think one of our problems is that
we are working with 8-bit values for
our colour channels, which is too low a
resolution for what we’re trying to do.
Also, we are using an overly simplistic
approach (we should really be applying custom gamma correction functions
for each channel based on the specific
characteristics of our LEDs). I’m sure we
could do better if we were desperate, but
I wasn’t desperate enough, so I decided
to chalk this one up to experience and
move on with my life.
For my third experiment, rather than change
the colour of only one
+ 1] =
group at a time, I decided
to change the colours of
all the groups simultaneously (file CB-Feb22-03.
txt). One point we should
note is that, thus far,
we’ve been displaying
the same colour patterns
Fig.4. Specifying which LEDs appear in what groups.
on all the SMADs. It’s
number of LEDs in that ring. Also, any
really interesting to see how different
‘0’ values are used only as placeholders
the colours look when diffused by the
(apart from the left-most ‘0’ associated
29-segment shells versus the stained-glass
with Ring 0, of course).
(some may say kaleidoscopic) effect ofMy first test was simply to illuminate
fered by the 45-segment shells. Speaking
the rings with white light in a sequence
of which, for the benefit of your cogistarting with Ring 0 and radiating out to
tations and ruminations, I just took a
Ring 4. You can feast your orbs on the
video that shows all of the SMAD-relatfull sketch (program) by downloading
ed effects just discussed in this column:
the code (file CB-Feb22-01.txt) from the
https://bit.ly/3GwQb5f
February 2022 page of the PE website:
https://bit.ly/3oouhbl
Gorgeous gammas
We introduced the concept of gamma
correction in the context of my 12 x 12
I’m so random
ping-pong ball array in an earlier Cool
For my next experiment, I decided to
Beans column (PE, January 2021). As
gather my LEDs into like-minded groups.
you may recall, our human eyes have
There are seven of these groups numevolved to accommodate a huge dybered from 0 to 6. Group 0 corresponds
namic range from moonlight to sunlight
to Ring 0 in our previous example and
and – as part of this – they have a sort of
therefore contains just one LED. Simibuilt-in non-linearity. Gamma correction
larly, Group 1 corresponds to Ring 1 in
Fabulous fades
involves tweaking the values of the red,
our previous example and therefore conIf we journey once more back into the
green, and blue channels in our LEDs to
tains the 8 innermost LEDs in our eight
past to the early days of our 12 x 12 pingcorrect for this non-linearity.
radiating spokes.
pong ball array experiments, you may
In our case, the way we’ve been doing
In the case of Ring 2, we are going to
remember when we introduced some
this in our programs is to use a gammadivide this into two groups – Group 2
rather cool functions that allowed us
correction cross-reference look-up table,
contains the eight LEDs in the eight little
to fade from one colour to another (PE,
the contents of which were kindly defined
triangular segments, while Group 3 conOctober 2020).
tains the eight outermost LEDs in our
eight radiating spokes. Similarly, we are
going to divide Ring 3 into two groups
const uint8_t GammaXref[] =
{
– Group 4 contains the eight ‘anticlock0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
wise’ LEDs in our eight outer ring seg0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
ments, while Group 5 contains the eight
2,
3,
3,
3,
3,
3,
3,
3,
4,
4,
4,
4,
4,
5,
5,
5,
‘clockwise’ LEDs in these segments. Last,
8,
8,
9,
9,
9, 10,
5,
6,
6,
6,
6,
7,
7,
7,
7,
8,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
but certainly not least, Group 6 contain
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
the four LEDs corresponding to Ring 4
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
in our previous program (Fig.4).
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
What we are going to do is to ran90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114,
domly select a group and illuminate all
115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142,
144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175,
the LEDs in that group with a randomly
177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213,
selected colour, and to continue to do
215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255
};
this over and over again. If you wish,
you can peruse and ponder this program
(file CB-Feb22-02.txt).
Fig.5. Gamma-correction cross-reference look-up table.
# d efine N U M _GR O U P S
7
# d efine M A X_L E D S _I N _GR O U P 8
const uint8_t E ffectM ap [N U M _GR O U P S ][M A X_L E D S _I N _GR O U P
{
{ 1, 0, 0, 0, 0, 0, 0, 0, 0 }, / / Group 0
{ 8, 1, 2, 3, 4, 5, 6, 7, 8 }, / / Group 1
{ 8, 9, 10, 11, 12, 13, 14, 15, 16 }, / / Group 2
{ 8, 17, 18, 19, 20, 21, 22, 23, 24 }, / / Group 3
{ 8, 25, 27, 29, 31, 33, 35, 37, 39 }, / / Group 4
{ 8, 26, 28, 30, 32, 34, 36, 38, 40 }, / / Group 5
{ 4, 41, 42, 43, 44, 0, 0, 0, 0 } / / Group 6
};
Practical Electronics | February | 2022
53
Fig.6. A CRT-based television displaying
white noise (‘static’).
When I was first trying to wrap my
brain around this, it actually took a bit
of thought to decide how to perform a
fade over a specific number of steps. Remember that, for each step, we first have
to split each 24-bit colour into its three
8-bit red, green, and blue colour channels. For each of these channels we calculate the new value based on the start
value, the end value, the total number
of steps, and the current step. Finally,
we gather the three 8-bit channels back
into a 24-bit colour. Phew!
Happily, we can reuse tweaked versions of our previously created functions.
For our fifth and final experiment in this
column, I removed our ghastly gamma
correction and added some fabulous fades
(file CB-Feb22-05.txt). As you’ll see in
the video, we start by creating a palette
of randomly selected colours, then we
assign these colours to the various groups
and display the groups. While keeping
track of the original palette, we generate
a new palette, assign these colours to the
various groups, and then fade from the
old colours to the new colours. And, of
course, we keep on doing all of this over
and over again.
Once again, if you watch the aforementioned video, I think you’ll find
it really interesting to see how different the colours look when presented
via the 29-segment versus the 45-segment shells.
That’s a lot of noise!
When most people hear the word ‘noise,’
their knee-jerk reaction is to think of a
cacophony of sound, but noise comes
in many forms. When I was a young
lad circa the 1960s, our family’s analogue television contained a bunch of
vacuum tubes along with a cathode ray
tube (CRT) for the display. At night, after
the TV stations had stopped broadcasting, the screen filled with ever-changing
random patterns of ‘snow on black’ flickering pixels (considering this was so dynamic, it’s somewhat paradoxical that it
was called ‘static’). This visual representation of noise was complemented by an
audible hiss (Fig.6).
54
Actually, the origin of the idea to apply
Both of these realisations – the visual
dither is rather interesting. In the early
static and the audible hiss – are of a
days of WWII, airplane bombers used
form known as ‘white noise.’ This is somechanical computers (essentially, boxes
called because it includes all frequenfilled with hundreds of shafts and gears
cies (more correctly, it has equal power
and cogs) to perform navigation and bomb
in any frequency of a given bandwidth),
trajectory calculations. The strange thing
which makes it analogous to white light,
was that these computers were found to
which is a mixture of all visible waveperform their calculations more accuratelengths of light.
ly when the airplane was in the air with
In audio engineering, electronics, physits engines running then when it was
ics and many other fields, we use terms
on the ground with its engines turned
like ‘white noise’, ‘pink noise’, ‘brown’
off. Eventually, engineers realised that
(or ‘Brownian’) ‘noise’, ‘blue noise’, ‘violet
the vibration from the aircraft reduced
noise’, ‘grey noise’ and even ‘black noise’.
the effects caused by ‘stiction’ (sticky
These terms refer to the different power
moving parts), so, instead of moving in
spectrums associated with the noise sigshort jerks, the vibration caused them to
nals. For example, while an instance of
move more smoothly. In order to replicate
white noise might be the sound of falling
this effect on the ground, small vibratraindrops, an illustration of pink noise
ing motors were built into the computcould be the ‘chuff, chuff, chuff’ sound
ers. The vibration from these motors was
of an old-fashioned steam engine.
called ‘dither’ from the Middle English
You might think that noise is a bad thing
verb ‘didderen,’ meaning ‘to tremble.’
to have in a system, but – if you know
what you are doing – it can be useful
on occasion. For example, when a conYou gotta love lava lamps
tinuous signal in the analogue domain
The reason for my waffling on about noise
is converted into a corresponding repis that it features in the answer to a poser
resentation in the digital realm, it’s conthat has long puzzled me. I’m sure you’ve
fined to a number of discrete states called
seen a lava lamp at least once on your
quanta. This results in something called
journey through life. These little beauquantisation error.
ties were invented by the British entreThe term dither (or dithering) refers
preneur Edward Craven Walker in 1963.
to intentionally overlaying the original
For quite some time, they were considsignal with a small amount of noise,
ered to be amazingly cool and – dare I
which serves to randomise the quantisay it (yes, I do) – groovy.
sation error. In the case of a computer,
A lava lamp consists of a glass vessel
dithering can be used to create the illucontaining a coloured waxy mixture at
sion of greater colour depth in images
the bottom. The rest of the vessel conon systems with a limited colour palette.
tains a clear or translucent liquid. When
Dithering can also be used to minimise
an incandescent light is placed under
visual artifacts like banding in images.
the lamp, in addition to lighting it up,
Another example that may be a little
the heat causes balls of the coloured
closer to home is when performing anawaxy mixture to decrease in density and
logue-to-digital conversion. Suppose we
randomly rise and undulate their way
have an 8-bit analogue-to-digital convertthrough the clear liquid.
er (ADC). Now suppose
that we superimpose a
noise signal on top of
the analogue signal feeding the ADC, where the
peak-to-peak amplitude
of this noise signal is
equivalent to one leastsignificant bit (LSB) of
the digital output. If we
now sample the signal
at four times the original
sampling rate and then
apply a low-pass filter to
the digital output (where
the simplest low-pass
filter would be to average the four samples), the
result is to make it look
like we have a 9-bit ADC.
(This is a bit too complicated to take any further
here – you’ll just have Fig.7. The FastLED NoisePlusPalette program meets my
to take my word for it!) 12 x 12 ping-pong ball array – do watch the video!
Practical Electronics | February | 2022
As I mentioned in my previous column,
on several occasions I’ve seen an effect
presented on arrays of tricolour LEDs that
is reminiscent of the oil wheels used in
1970s discos. This effect is also evocative of the undulations of the waxy balls
in a lava lamp.
The FastLED library (https://fastled.
io/) offers a great way to control tricolour LEDs like the NeoPixels I used
to populate my 12 x 12 array of pingpong balls. It turns out that there are
a couple of example programs called
‘Noise’ and ‘NoisePlusPalette’ that are
provided with this library. These programs use special noise functions that
are based on something called ‘Simplex
noise’, which was created by Ken Perlin
in 2001, and which provides a method
for constructing n-dimensional noise.
The great thing about Simplex noise is
that it generates values that are pseudorandom overall but that are locally related
at the same time. The easiest way to visualise the way this works is to imagine
that you are looking at a giant oil lamp. If
you were to take a series of photographs
ten minutes apart, it would appear that
there was no relation between the images.
By comparison, if you took a snapshot
every second, you could see how each
image was related to the one that came
before and the one that came after. That
is, what you are seeing is both random
and constrained at the same time.
The bottom line is that I tweaked the
existing NoisePlusPalette program to
drive my 12 x 12 ping-pong ball array
(Fig.7). To be honest, in addition to restricting the program to using only its
‘lava’ palette, the main thing I had to
change was the XY() function that accepts X and Y coordinates as inputs and
returns the number of the NeoPixel in
Want to build your own amazing array?
All the details are in previous Cool Beans
columns, starting in March 2020.
the chain (my NeoPixels are physically
wired – daisy-chained – together in a
serpentine pattern).
One thing I should note is that I originally tried to run this program on the
32-bit Seedunio XIAO microcontroller
that I introduced in an earlier column
(PE, July 2020), but... nothing happened.
I thought I’d messed something up while
performing my tweaks, and I was ready to
pull out what little I have left of my hair
when I thought to question my chum,
Steve Manley. Steve did what I should
have done, which was to have a quick
Google. This revealed that there’s a known
problem with the XIAO and the FastLED
library. A little later, Steve emailed me to
say that he’d discovered a workaround
(https://bit.ly/3oJuvNn), but he was too
late. By this time, I’d already swapped out
my XIAO for an Arduino Uno, after which
everything worked just fine and dandy.
I’m really happy with the result, which
you can see in a video I posted to my
Cool Beans YouTube channel (https://
bit.ly/3ysgEhP). Feel free to peruse and
ponder the modified version of the program (file CB-Feb22-06.txt) but remember that I’m not the author of this code,
just the tweaker.
Don’t lose your head
The definition of a cliffhanger is an adventure serial or melodrama that’s presented in installments, each ending in
suspense. When my mum was a little girl
about 10 years of age, her older brother used to take her to the kids’ show at
the local cinema on Saturday mornings.
The show was about two hours long and
included a bunch of items, including a
short thriller, like an episode of Flash
Gordon or Buck Rogers (the old blackand-white versions from the late 1930s),
which ended on a cliffhanger. My mom
says she could barely control her excitement and it made her want to see the
next episode all the more.
If only we could regain that sort of anticipation and suspense here. I know what we
can do... in my previous column, I promised to show you the progress we’ve been
making with regard to animating our robot
heads. When I say ‘we,’ I mean me and
my friend Steve Manley. And when I say
‘all of the progress we’ve been making,’ I
mean all of the progress Steve has been
making because he’s storming ahead with
something so super that you will squeal
with delight when you see it. Sad to relate,
however, this will have to wait until my
next column (Cliffhanger – Tra-la!)
Cool bean Max Maxfield (Hawaiian shirt, on the right) is emperor
of all he surveys at CliveMaxfield.com – the go-to site for the
latest and greatest in technological geekdom.
Comments or questions? Email Max at: max<at>CliveMaxfield.com
Practical Electronics | February | 2022
www.poscope.com/epe
- USB
- Ethernet
- Web server
- Modbus
- CNC (Mach3/4)
- IO
- PWM
- Encoders
- LCD
- Analog inputs
- Compact PLC
- up to 256
- up to 32
microsteps
microsteps
- 50 V / 6 A
- 30 V / 2.5 A
- USB configuration
- Isolated
PoScope Mega1+
PoScope Mega50
- up to 50MS/s
- resolution up to 12bit
- Lowest power consumption
- Smallest and lightest
- 7 in 1: Oscilloscope, FFT, X/Y,
Recorder, Logic Analyzer, Protocol
decoder, Signal generator
55
|