March
2005, Issue 176
Joystick
Technology
GOT
BRAINS?
Many
digital potentiometers are available with serial-type
interfaces because of the small number of I/Os required
for communication. SPI and I2C are the most popular
interfaces. These devices have expanded from the first
32-tap potentiometers to up to 256 taps, which allow
the wiper to be positioned in increments of 0.4% of
the total resistance, almost an order of magnitude better
than their 32-tap predecessors.
Instead
of the aforementioned simple interface, these parts
now require some smarts so a simple switch input device
like the JS1100AQ can control them. A microcontroller
with at least eight I/Os can interpret the switch inputs
and set the digital potentiometer. Five inputs monitor
the JS1100AQ switches. You can use three output bits
to communicate with the digital potentiometer (see Figure
2). I ended up with nil after a cursory search for small
I/O microcontrollers with a serial interface, so the
SPI interface had to be a software function. This wasn't
a big deal because little else required lengthy time
requirements.
|

(Click
here to enlarge)
|
Figure
2—By choosing a microcontroller with at least eight
I/Os, all five switches in the JS1100AQ can be monitored
with enough outputs left over for a software SPI
interface. |
The
microcontroller's basic functions (besides communicating
with the digital potentiometers) are to monitor the
JS1100AQ for up, down, left, and right contact closures
and to increment or decrement the x and y variables.
The algorithm used for this might be as simple as incrementing
or decrementing the variable once for each contact closure.
You may wish to implement a more complicated scheme
to automatically step the variable's value if the contact
remains closed.
This
will become more complicated if you dynamically change
the step size based on time. Much of this would depend
on how you'll use the output. If it's used as a simple
volume control, then an adjustment response in the seconds
range might be suitable. Note that 1-s responses might
be required for pointing purposes. Sub-second moves
are necessary for gaming because a perceivable time
lag makes gaming difficult. Whether position feedback
is a bar graph, a cursor, or a paddle, your eye, brain,
and finger close the feedback loop by automatically
compensating for errors.
Figure
3 shows a simple program loop that looks at the JS1100AQ
input switches and determines which way the JS1100AQ
is being tilted—position 1 (north) to position 8 (northwest).
This allows you to assign a position based on any combination
of switches, and you can compensate for a JS1100AQ rotation
or the swap of an axis. Depending on the position, subroutines
are called to adjust XVal or YVal. These values are
used to set the taps on digital potentiometer 1 and
potentiometer 2 every time through the main loop during
the call to the SPI subroutine. The SPI subroutine requires
500 µs, which is the longest length of time to execute.
It's executed every time to act as a timer. Therefore,
timers aren't necessary.
|

(Click
here to enlarge)
|
Figure
3—A simple loop is used to monitor switch inputs,
increment and decrement x- and y-axis variables,
and communicate the values to an external digital
poteniometer via an SPI interface. |
The
X_INC, X_DEC, Y_INC, and Y_DEC subroutines are all similar.
Figure 4 shows the X_INC subroutine. A flag for each
switch is cleared unless the switch is held in the closed
position. The XSkip and YSkip variables begin with a
value of 255. These variables count down each time an
X or Y subroutine is executed (potentially each time
through the main loop), allowing a subroutine to be
skipped until the variable reaches zero. If the subroutine
is skipped 256 times, then a change to XVal or YVal
can happen only every 128 ms (256 × 500 µs), which is
the automatic repeat rate.
|

(Click
here to enlarge)
|
Figure
4—This routine is one of four used to adjust the
XVal and YVal variables. The rate that XVal is incremented
depends on the XInit variable. A simple algorithm
(XInit = XInit – 1) will change the rate in a linear
fashion. Tune for the best picture according to
the application. |
In
this simplest form, 256 increments would take 33 s (256
× 128 ms). So, even though the time for a single increment
seems short (128 ms), waiting for a maximum change takes
a long time (33 s). By substituting different algorithms
in the Calculate New XInit in the X INC/DEC subroutines,
you can affect the number loaded into the XSkip variable
(reloaded from XInit). The simplest algorithm is to
decrement XInit each time. This would reduce the automatic
repeat rate from 128 ms by 500 µs for each change in
Xval in automatic Repeat mode. For a maximum change,
this cuts the total time in half to approximately 16
s.
You
can see that a linear change might not be the best approach.
If XInit is reduced by 12.5% of itself each time XInit
is divided by eight (three rotations), the total time
is reduced to less than 2 s. This has a side effect
of limiting the minimum step time because 12.5% of anything
less than eight equals zero, and XInit is no longer
reduced. With this algorithm, the step time is reduced
for the first 32 taps in 1 s, while the remaining 223
taps are stepped at 4 ms each over the next 1 s.