Lesson 23: Code lock
Cette page nécessite un navigateur récent (avec support SharedArrayBuffer). Veuillez mettre à jour Chrome, Edge, Firefox ou Safari vers la dernière version.
English
A door that knows the code
- Hotel safes and office doors open with a PIN: a secret number.
- Here you type the code in the Serial Monitor and press Send. A servo is the latch: at 0° the door is locked, at 90° it is open.
- Green means open, red means wrong. Then you make the lock safe against someone who just tries many codes.
Words for this lesson
| Word | Meaning |
|---|---|
| PIN | a secret number that opens something |
| latch | the small bar that keeps a door shut |
| parseInt | reads the digits you typed and makes one number |
| == | "is equal to?": compares two values |
| return | leave the function now |
English
What the lock does
| You type | What the lock does |
|---|---|
| 1111 | "Wrong code": red LED (13) on for 1 s, latch stays at 0° |
| 2468 | "Open": green LED (12) on and latch (9) at 90° for 3 s |
| a 3rd wrong code | "LOCKED": red LED on for good, latch at 0° for good |
English
One = or two?
void setup() {
Serial.begin(9600);
int code = 1111;
if (code == 2468) {
Serial.println("same");
} else {
Serial.println("different");
}
}
void loop() {
}
code == 2468asks a question and the answer is "different".code = 2468would put 2468 intocode: a common mistake.
English
How safe is 3 tries?
- A 4-digit code has 10 × 10 × 10 × 10 = 10 000 possible codes.
- With 3 tries a thief has 3 chances in 10 000. Without the limit, a fast machine could try all 10 000.
English
In the exam
- 0478/11/M/J/22 Q7: Adele uses a biometric password (a fingerprint or her face) on her phone instead of a PIN like your code. (a) A biometric password uses biological data: features of a person's body. (b) It is more secure than a PIN because it cannot be guessed, it is very hard to fake, a keylogger cannot record it, and nobody can see it by looking over your shoulder.
- Your lock's limit of 3 tries is another way to make a PIN safer: guessing all 10 000 codes is no longer possible.
Difficulté 4/5 · 280 pièces + un petit bonus lors de la première réussite aujourd'hui
A door lock opens with the code 2468. Type a code in the Serial Monitor and press Send. If it is right: open the latch (servo to 90°), green LED on, print Open, wait 3 s, then close it again. If it is wrong: red LED on for 1 s and print Wrong code.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 5/5 · 400 pièces + un petit bonus lors de la première réussite aujourd'hui
A thief could try every code. Make the lock safer: count the wrong codes in wrong. After 3 wrong codes, print LOCKED, keep the red LED on, and never open again, even for the right code.
Cliquez sur Exécuter pour voir le résultat ici.