Make Electronics

(nextflipdebug2) #1

Experiment 36: The Lock, Revisited


314 Chapter 5


I have chosen an arbitrary combination of 7-4-1 for our combination lock. Us-
ing these numbers, the main section of the program looks like this:
main:
low 1
gosub getkey
if b1<>7 then main
gosub getkey
if b1<>4 then main
gosub getkey
if b1<>1 then main
high 1
end
I should explain that the <> pair of symbols mean “is not equal to.” So the
fourth line of the program means, “if b1 is not equal to 7.”
The value of b1 is supposed to be 7 if the user is putting in the correct com-
bination. So if it’s not 7, the user has entered the wrong value, and the if-then
statement sends the PICAXE back to the beginning. In fact anytime the user
inputs a number that is not in the correct 7-4-1 sequence, the program sends
the PICAXE back to the beginning. This is the way the pure-hardware version
of this experiment was set up.
But what is this word “gosub”? It means “go to a subroutine.” A subroutine is
any sequence of program statements that ends with the instruction to “return.”
So “gosub getkey” tells the PICAXE to mark its current place in the program
while it skips to the getkey: section of code, which it obeys, until it finds the
word “return,” which returns it to the place from where it came.
The PICAXE continues in this fashion until it reaches the word “end.” I had to in-
sert the word “end” because otherwise the PICAXE will continue executing the
program and will fall into the subroutine. “End” stops it from doing so. Figure
5-148 shows a screenshot of the complete listing.
So—is that all? Yes, that’s it. If you enter the code into the Programming Editor
exactly as I have supplied it, you should be able to run it in simulation mode,
and in the simulation window, click the right-arrow beside Logical Pin A2 to
increase its value in steps. Each time you pass one of the values in the getkey:
subroutine, you should see the value for variable b1 change in the display.
This is really all you need to perform the functions of the combination lock.
When the PICAXE runs this program, it waits for the correct combination. If it
receives the combination, it sends the output from logical pin 1 high; other-
wise, logical pin 1 stays low.
The only additional item you need is a transistor or CMOS gate between logi-
cal pin 1 and the relay that unlocks the computer, because the PICAXE cannot
deliver enough current to operate the relay by itself.
Putting this procedure into a controller chip not only simplifies the circuit, but
offers another advantage: you can change the combination simply by rewrit-
ing the program and downloading the new version into the chip.
Free download pdf