■ BY JON MCPHALEN THE SPIN ZONE
Put a Button On It
New actors are often encouraged by casting
directors to "put a button on it" — this
means to end the piece with something
simple yet memorable. Well, then, let's put
a button on 2014 by talking about ...
buttons! In our high tech world of
touchscreens and voice-activated
everything, simple buttons are often abused
both physically (which I can do nothing
about) and in code (where I can help). The
simple fact of the matter is that most
electronic devices have buttons of some
sort, hence we should learn to use them
properly. Whether your project uses just
one button or a bunch of them, I'm going
to show you some tips and tricks that can
be incorporated into your Propeller
the button is open, the pin will read 0; when the button is
pressed, the pin will read 1.
Some will ask about resistor R2. This resistor limits the
current into the pin if there is a programming error or if
the input voltage is 5V (which is frequently present in
Propeller projects), plus it protects the pin (as well as one
can) from static discharge we humans tend to barrage
electronic devices with.
Simple, right? Of course —
but there's more to it.
We tend to think of a
short button press creating
an input on the pin that
looks like Figure 2. If we
put a 'scope on the input,
what we'd see looks more
like Figure 3. The ragged
on and off switching at
the beginning of the input
is called contact bounce.
■ FIGURE 2. Clean input.
■ FIGURE 3. Bouncy input.
This is, in fact, caused by the mechanical contacts of the
button bouncing as it closes.
Ithink it's Nature's cruel trick that the smallest things tend to bite the hardest — I learned this lessen well growing up in the desert catching lizards and snakes.
The same holds true with buttons and switches. If we
don't treat them well, we may end up with a nasty
surprise. I'll give you a real world example.
Some will say, "So, what?" Okay, let's get back to my
friend in New York. His particular situation created a
perfect storm of problems with programs using less-than-industrial techniques. His props used very long wires to
connect physical triggers (buttons and switches) to the
controllers. Long wires act like antennas. A radio that's
transmitting generates RF that can be picked up by these
long trigger wires.
One of the entertainment venues that I work in is the
Halloween industry. Most modern Halloween attractions
use a wide range of electronic controls. An EFX-TEK
customer called me from New York in an absolute panic —
every time a walkie-talkie was keyed on, random props
around his haunt would start operating. This was not the
kind of nightmare he was looking for in his attraction.
The voltage induced across the trigger wire created
noise on the trigger pin. Inappropriate trigger code in the
controller allowed this noise to create a false positive, and
his props went crazy. Thankfully, the solution was simple —
so simple it is often overlooked.
Before we get to the solution, let's have a look at the
problem. Here is an atrocious (yes, atrocious) example of
button input code:
In Figure 1, you
see a typical button
input circuit. This is
configured for active-high input to the pin
if (ina == 1)
The only redeeming quality of this method is that it
can be re-used. Where it goes off the rails is in two places:
(you can reverse the
1) The input pin is hard-wired as a "magic number;" and 2)
■ FIGURE 1. Button circuit.
3.3V and ground
connections to make
it active-low). When
The scan of the input pin is so quick that it can allow a
false positive if noise is present on the pin when this
method is called.
18 December 2014