if (len > 0)
keybuf[—len] := “*”
Jon "JonnyMac" McPhalen
keypad buffer and head and tail pointers must all be longs
and defined in a contiguous group.
if ((len == 0) or (len == 4))
Lock It Up!
Okay, then; we have a nice little buffered keypad, so
let’s turn it into something. How about a simple electronic
lock? Easy peasy.
I prototyped the code on a Propeller Activity Board
(PAB) using a 4x4 matrix keypad and four-character/14-
segment display. Using the alpha-numeric display allows us
to have a nicer user experience. For simpler projects, we
could use seven-segment displays.
This method takes a true/false parameter that tells the
program if we’re editing or just entering. In edit mode, an
empty place in the display is filled with an asterisk — blank
spaces are just blank in simple entry mode. The asterisks
are there to remind the user the program is in edit mode.
On boot-up, the display shows “LOCK” and the
unlock output pin is low. In application, this would drive a
It’s important to create a user experience that — if not
obvious — requires only a single training session and is
easy to remember.
FET or BJT to drive a solenoid lock. When a key is pressed,
the screen is cleared and the current keys are displayed.
When four digits are in the display, the code compares the
user input to the embedded unlock code; if they match,
the display changes to “OPEN” and the unlock output is
driven high. Pressing any key will restore the lock state.
At the beginning, the display is filled with the
appropriate character(s) and drops into the entry loop. In
either mode, pressing a digit key adds the digit to the
display. In entry mode, four digits cause the method to
exit with the new input in the array called keybuf. The
What if we want to change the lock code? No
worries, we can do that, too; the process isn’t obvious —
for good reason — and does require knowledge of the
current code, just like changing an online password. While
developing my basic lock code, I originally had two entry
methods: one that simply accepted the code; then another
for entering and editing a new code. Once things were
working, these methods were consolidated in get_code():
Propeller doesn’t have a string data type, but there are a
couple methods that operate on an array of characters.
The requirement for these arrays is to be null-terminated.
For the four-digit lock, keybuf is an array of five bytes
(four keys plus the null). The and keys are used in edit
mode. If one or more keys for a new sequence have been
entered, pressing deletes the last entry. If a valid four-digit entry is in the display, pressing accepts the new
code and returns.
pub get_code(edit) | len, key
Okay, we have a method for entering or editing the
code. Let’s build a method for changing the current code.
I’m following the trend of escape room props and forcing
a special entry on power-up:
len := 0
bytefill(@keybuf, “*”, 4)
bytefill(@keybuf, 0, 4)
pub check_update | idx
time.pause(KEY_MS << 1)
if (read_keypad <> STAR_POUND)
key := key_to_ascii(read_keypad)
if (len < 4)
keybuf[len++] := key
if (edit == false)
if (len == 4)
ifnot (strcomp(@KeyCode, @keybuf))
if (get_code(true) == 4)
bytemove(@KeyCode, @keybuf, 4)
repeat idx from 0 to 3
July 2016 61