Elegant one-key code entry?

Innes

Member
I have created a motor-driven lock for a workshop door, and all is working well. The circuit is straightforward, utilising an 08M2 with:
  • two outputs to drive the motor through an L293D
  • one input for two limit switches (in parallel) so the motor knows when to stop
  • one output for a status LED
  • one input for the single control button
One press locks the door, another press unlocks it, and it's now working perfectly.

However, I'd like to up the ante and introduce a code to unlock the door. The limitation is that I only want to use the single control button to enter the code. I have a very elementary key-code check in place, which requires a single press, then a release, then a press for between x and y seconds (e.g. between 2 and 5 seconds), which is quite straightforward...

Code:
enter_key:    
    gosub check_sw_key_release ;loops until key is released
    
    pause 500
    
    time = 0 ; reset timer
    
    DO WHILE sw_key = 1 ;loop whilst key is held down
    LOOP


    for aloop = 1 to time ;this loop is for debug purposes - it flashes for however many seconds the key has been held down.
        high LED_RED
        pause 250
        low LED_RED
        pause 250
    next aloop
    
    if time > 2 AND time < 5 then ;if the key is held down for the appropriate time, then unlock
        gosub unlock
    endif

return

check_sw_key_release:
    if sw_key = 1 then goto check_sw_key_release
return
I'd like to introduce something more elegant that would require something like (e.g.) two quick presses and two short presses in order to unlock, but I can't think of how to code it more elegantly than hard coding the required presses using repeated elements of the above example code.

Can anyone suggest some ideas for a more elegant solution?

On a related note, I tried using the COUNT command with which I had limited success due to debouncing problems. Could I solve the debouncing with something as simple as a capacitor across the key button (it's a simple NO push-button). If so, what value cap should I use?

Thanks in advance.
 

geoff07

Senior Member
Think along the lines of a finite-state machine. Recognised states, receiving recognised events. The states could be waiting, one press received, two presses received, etc. The events could be long press, short press, no press before timeout.

In each state, it looks for the valid event to move to the next state. If no valid event, beep and revert to waiting. If the right sequence received, then the last state is reached and the lock released.

One input, one output (beep), any length sequence of long and short.

Only one set of i/o code but the FSM is defined by the state and events and actions are determined in a select statement or two. The valid code can be defined in eeprom data, so not hard-coded.

Your sequence of longs and shorts could be based on Morse letters.
 

techElder

Well-known member
As geoff07 mentions, I immediately thought of a Morse code "decoder" that is described elsewhere on this forum. You may find good code there that will allow some timing flexibility in the "mark" and "space" input from your switch. Thus making it forgiving.

On the debounce, you should always debounce a physical switch with something. Always. And don't scrimp on the quality of the switch especially if the switch is exposed to the outdoors.
 

westaust55

Moderator
Other options that can also be considered can include:
1. A 3x4 keypad with resistors to a PICAXE input . The input must accept ADC inputs so you can use the READADC command. Several past threads cover this analog keypad entry scheme.
2. Use a 1-wire iButton which Rev Rd and other sources sell. That gives you a unique key and you use the READOWSN to read the unique serial number for access control. You can even have multiple iButtons and compare each serial Number for a valid match.

IButtons by their nature are robust and weatherproof but you need a contact point.
There are weatherproof keypads that are relatively cheap as well in some electronics retail outlets.
 

AllyCat

Senior Member
Hi,

Personally, as suggested above, I'd probably use a 12-key matrix keypad with 5 resistors to create an analogue voltage on a single wire/pin, to create a "proper" PIN entry system.

However, your original request reminded me of this fairly recent thread which may give you some ideas.

Cheers, Alan.
 

grim_reaper

Senior Member
Since we're all coming up with alternatives rather than answering your question, I've got another one!

I use a RFID module for my workshop door - because there is no contact required, the sensor loop is inside the wall (of wood) and therefore weather-proof as long as the wall is. The downside is the same as the iButton - you still have to remember to carry the key to the workshop with you!

My 'lock' only enables/disables the alarm system; I'd be very interested to hear how your motor driven lock works :D
 

vttom

Senior Member
Perhaps you could record the time between the first button press and the second button press, and then use that as a threshold for subsequent presses. For example, here are 5 button presses (4 intervals) with the amount of time between each...

Press 1
1.0 sec
Press 2 (establishes a 1 second threshold)
1.5 sec
Press 3 (greater than the threshold, so call it a "1")
0.5 sec
Press 4 (less than the threshold, so call it a "0")
1.5 sec
Press 5 (greater than the threshold, so call it a "1")

You just entered a code of "101".
 

John West

Senior Member
I'm curious what happens if the power goes out or the motor or circuitry stop working correctly. I considered an electric lock for my motorhome, but decided against it due to my concerns about the system status during a variety of failure modes. I just wasn't sure I could cover every possible eventuality.

Ultimately, the whole motorhome drowned in the Boulder, Colorado floods last fall, just one more unforeseen circumstance.
 

westaust55

Moderator
I'm curious what happens if the power goes out or the motor or circuitry stop working correctly. I considered an electric lock for my motorhome, but decided against it due to my concerns about the system status during a variety of failure modes. I just wasn't sure I could cover every possible eventuality.

Ultimately, the whole motorhome drowned in the Boulder, Colorado floods last fall, just one more unforeseen circumstance.
Not greatly different to those new electronic safes.
The better units have the batteries accessible from the outside - otherwise you need some mechanism kept in a safe place (not in the safe) to allow you to open the door when the batteries are flat.
 

inglewoodpete

Senior Member
For an 'elegant' solution, write your program to determine the duration of a key press. You can easily get three different key input values this way Eg.Short (<300mS), Medium (300 to 1000mS) and Long (>1000mS).

While this type of solution is best done using the X2's programmable background timer, you can also do it with loop-counting techniques that I have described here for an 08M.

one input for two limit switches (in parallel) so the motor knows when to stop"
This is not a safe solution! The limit switch must cut the power to the motor. The motor will not 'know' when to stop. What would happen with your solution if the PICAXE code was not checking the input pin when the limit had been reached?

It would be very wise to include some code that cut the power to the motor if the input key was even briefly pressed while the motor is energised. Young children and pets (and old men and...) have a habit putting their heads and bodies in the wrong place at the wrong time!
 
Top