9. Case 06: Mechanical Password Switch Door¶
Level:
9.2. Background¶
What is a Mechanical Password Switch Door?
Nowadays, digital locks are common, apart from preventing theft going in, it also provides convenience. For instance, home owner no need to bring the physical key, just use the password or Biometric information, the door can be opened or closed without pushing or pulling it.
In this case, it will demonstrate the use of the basic switches with micro:bit to perform a mechanical password switch door.
Password Switch Door Principle
The home owner set the password (A=2 & B=3), the door will be opened.
Initialize the 2 variable “count_A” and “count_B”.
For button A pressed, the count_A will be added 1. For button B pressed, the count_B will be added 1.
After the user pressed the password. he/she then press button A+B to check the password, if it is correct, the micro:bit will show tick. Otherwise, it will show cross.
User press the button near the door.
If it is correct, the door will be opened and after 5 second, it will be closed.
9.3. Part List¶
9.4. Assembly step¶
Step 1
In this case, build the “Open Style Model” as a home base.
Step 2
To build a smart door, attach button module onto Model B1 using M4 * 10mm screws and nuts, align with the holes nearby the door on model B1. And the connecting wire should be bended to the hole above.
Step 3
Attach the L-shape stand to the back of Model B1 using M3 * 10mm screws and nuts.
Step 4
Install 180ᵒ servo by using M2 * 7.5mm sharp screw and then attach it to model A.
Step 5
Use a paper clip as a hook to connect the L shape stand and 180ᵒ servo.
Step 6
Assembly Completed!
9.5. Hardware connect¶
Connect the button module to P0
Connect the 180 degree servo to P1
Pull up the buzzer switch to disconnect from buzzer
9.6. Programming (MakeCode)¶
Step 1. Create variable and initialize the servo
Create a variable called
count_A
,count_B
,unlock
In
on Start
, initialize the state of door to closed byset unlock to false
andTurn 180 Servo to 45 degree at P1
, also set the count variablecount_A
andcount_B
to0
Step 2. Monitoring the button pressing state and take action
Snap the
When Button at P0 pressed
block to editorPut a
if
statement in theWhen Button at P0 pressed
blockSet the condition to
unlock = true
When
unlock = true
, that’s means it is unlock successfully, the servo should open the door byTurn 180 servo to 180 degree at P1
, after that, wait for 5 second bypause (ms) 5000
, then close and lock the door again byTurn 180 servo to 45 degree at P1
and set variableunlock
tofalse
Step 3. password input method
Snap
on button A pressed
to editorUse
change count_A by 1
to increase the count variable by 1Repeat the step with minor different on target to let button B also can increase
count_B
Step 4. Examine the password
Snap
on button A+B pressed
to editorPut a
if-else
statement insideUse
count_A = 2 and count_B = 3
as condition, you may also change it to your favourite passwordIn the
if
segment, that’s means the input is correct,show icon tick
, andset unlock to true
to allow open the door when press the extended buttonIn the
else
segment, that’s means the input is not correct,show icon cross
, andset unlock to false
to denied open the door when press the extended buttonReset the two count variable
count_A
andcount_B
to0
for next input
Full Solution
MakeCode: https://makecode.microbit.org/_agKTsmi8tD4C
You could also download the program from the following website:
9.7. Results¶
After pressing the correct number of button A and B, press A+B to do the validation.
If it is the correct password, press the extended button, the door will open. After 5 seconds, the door will close and lock again. If wrong, do not open the door.
9.8. Think¶
Apart from using a physical button, are there better ways of controlling the open and close of the door?
Can it make a doorbell to produce sound when the door is opening?
Other than the door, can the switch apply to other usage? (e.g Turn on/off LED)
Can you add a rule, if input is incorrect more than 3 times, show the alert message and lock the door for 5 minutes?