■ FIGURE 8. Matrix
keypad after “surgery.”
The question remains: What
specific values should we use in
place of the question marks? The
safest (i.e., most error-free) choice is
the mid-point between the values for
each pair of adjacent keys. I’ll use our
theoretical values to clarify this point,
but you should substitute the actual
values you obtained from running the
keypad1.bas program. The theoretical
value associated with the “1” key is
361 and the value associated with
“ 2” is 376, so the mid-point is about
369. Therefore, in our select case
statement, we’ll say that any value
less than 369 will be decoded as the
“1” character. If we use the same
approach to each of the characters,
our select case statement becomes:
■ FIGURE 7. Breadboard layout for
ADC matrix keypad circuit.
select case key
case < 369
char = 49
shortly. (At this point, you may want
to print out both programs for
reference throughout the following
discussion.) Keypad1.bas has two
purposes: to make sure that the
breadboard circuit is configured
correctly, and to determine how
closely our “real-world” ADC values
match the theoretical values that
were presented back in Figure 4. The
main program loop repetitively
carries out the following tasks:
times because you may get
slightly variable readings
for some of the keys. If
so, just choose the most
typical value for each key
and make a note of it. (As
I mentioned earlier, your
results will most likely differ somewhat
from the theoretical values back in
Figure 4, but they should be close.)
‘ ASCII code
‘ for “1”
case < 385
char = 50
DECODING THE
KEYPRESSES
‘ ASCII code
‘ for “2”
‘ etc., etc.
Since our select case statement
involves 16 distinct cases, it will be fairly
long. To compress it a bit, we’re going
to use a little shortcut. Similarly to many
dialects of BASIC, PICAXE BASIC
supports the use of the “:” symbol to
separate multiple statements on the
same line which means we can write:
• Wait for keypress. (Theoretically
junk = 0 if no key is pressed, but “<
5” is safer.)
Now that you know the ADC
value that’s produced by each keypress
in your breadboard setup, we need to
modify keypad1.bas so that it actually
decodes each keypress and outputs the
appropriate character. Naturally, we’ll
need another variable in which to store
the resulting character; let’s call it char.
Also, we’re going to use a select case
statement to accomplish the decoding,
but we can’t use a series of equalities to
convert each ADC value to the correct
character because (as we just discovered)
some of the values are slightly variable.
Also, additional slight variation can be
introduced by changes in temperature
and other factors. To make sure that we
always decode the correct character,
we’ll use a series of “less than” phrases
in the select case statement and work
our way up the list. Using this approach,
our select case statement will take the
form of the following code fragment:
select case key
case < 369 : char = 49
‘‘‘‘‘‘‘‘ ASCII code for “1”
case < 385 : char = 50
‘‘‘‘‘‘‘‘ ASCII code for “2”
case < 402 : char = 51
‘‘‘‘‘‘‘‘ ASCII code for “3”
case < 434 : char = 65
‘‘‘‘‘‘‘‘ ASCII code for “A”
‘‘‘ etc., etc.
70 October 2010
When you have downloaded
keypad1.bas to your 08M, pressing any
key should produce a value between 0
and 1023 in the terminal window. If it
doesn’t, you’ll need to trouble-shoot
your breadboard setup. When your
circuit is functioning properly, jot down
the value you obtain for each keypress.
We’re going to use these results soon,
when we get to the keypad2.bas
program. Be sure to press each key a few
select case key
case < ?
char = 49 ‘ ASCII code
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ for “1”
case < ?
char = 50 ‘ ASCII code
‘‘‘‘‘‘‘‘‘‘‘‘‘‘ for “2”
‘ etc., etc.
The “:” shortcut is not something
I’m suggesting you use frequently in
your programs; it can easily make
code much more difficult to read.
However, our long select case
statement is perfectly readable in this
form, and also much shorter. One
final point – don’t forget, as soon as
one of the case conditions evaluates
to true, the associated code is
executed and the remainder of the
select case statement is skipped. For
example, if key equals 375, char is set
equal to 50 and the compiler jumps
ahead to the program line that
follows the endselect statement.