Make Electronics

(nextflipdebug2) #1
What Next? 313

Experiment 36: The Lock, Revisited

b0 receives the value supplied by the analog-digital converter when it looks at
the keypad. After storing the number in b0, the routine has to figure out which
keypad key it matches. The key identity (0 through 9) will be stored in another
variable, b1.


The program starts by assigning value 9 to b1. Then it checks to see wheth-
er b0 < 84. This means “if b0 is less than 84.” If it is, then the routine tells the
PICAXE to “finish,” which means “jump to the finish: label.” But if b0 is not less
than 84, by default the PICAXE continues on to the next line, which makes a
second attempt at guessing which key has been pressed. It assigns number 6
to b1. Now there’s another if-then test—and so on. This process of reassigning
values to b1 stops only when it gets to the point where b0 is greater than a
number in the table.


If you’re familiar with other dialects of BASIC, this may seem a bit laborious to
you. You may wonder why we can’t use a statement such as this:


if b0 > 70 and b0 < 84 then b1 = 9

The answer is that PICAXE BASIC isn’t sufficiently sophisticated to allow this.
An if-then statement has to result in a jump to another section of the program.
That’s the only permitted outcome.


If you don’t have any prior programming experience, the routine may still
seem laborious to you, and perhaps a bit puzzling, too. This is understandable,
because you’re getting a crash course in software design without any formal
preparation. Still, the PICAXE Programming Editor can be a big help, because
it has its simulation feature. Before you can use this, though, you have to pre-
cede the routine that I just supplied with a control routine that you must type
above it. The screenshot in Figure 5-148 shows you how it should look.


0

1 2 3

4 5 6

7 8 9

#
*

Reset
relay

PICAXE
ADC pin

PICAXE power supply

500

600

100

100

100

100

100

100

100

100

100

Figure 5-147. A quick and simple way of attaching a keypad to provide numeric input to the
PICAXE uses a chain of resistors totalling 2,000Ω. When a button is pressed, it connects the
ADC input pin to a point in the chain. The resistance detected by the input pin can then be
converted by the program in the chip to determine which key has been pressed.

Free download pdf