Skip to content

Commit 67e4bf0

Browse files
committed
Add new example sketch: four digit code entry
1 parent 9a13957 commit 67e4bf0

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <KeypadShield.h>
2+
3+
// This example sketch shows how to wait until someone enters a particular
4+
// four-digit secret code into your Lectrobox Keypad. When the code is entered,
5+
// the secretCodeEntered() function is called.
6+
7+
// You can change the code by changing "1234" to any code you want.
8+
#define SECRET_CODE "1234"
9+
10+
// Make sure the CODE_LENGTH constant matches the length of the code above.
11+
#define CODE_LENGTH 4
12+
13+
///////////////////////////////////////////////////////////////
14+
15+
KeypadShield keypad;
16+
char lastChars[CODE_LENGTH];
17+
18+
void setup() {
19+
keypad.begin();
20+
for (int i = 0; i < CODE_LENGTH; i++) {
21+
lastChars[i] = 0;
22+
}
23+
}
24+
25+
// This function is executed when the code is entered correctly.
26+
void secretCodeEntered() {
27+
// Write code here that should be executed when the code has been entered.
28+
}
29+
30+
void loop() {
31+
char nextChar = keypad.getNextKeypress();
32+
33+
if (nextChar != 0) {
34+
// Move all previously seen chars forward one position in the array.
35+
for (int i = 0; i < CODE_LENGTH-1; i++) {
36+
lastChars[i] = lastChars[i+1];
37+
}
38+
39+
// Add the newly received character to the end.
40+
lastChars[CODE_LENGTH-1] = nextChar;
41+
42+
// Check to see if the code is now correct.
43+
bool codeCorrect = true;
44+
for (int i = 0; i < CODE_LENGTH; i++) {
45+
if (lastChars[i] != SECRET_CODE[i]) {
46+
codeCorrect = false;
47+
}
48+
}
49+
50+
// If so, execute the secret code function!
51+
if (codeCorrect) {
52+
secretCodeEntered();
53+
}
54+
}
55+
56+
delay(100);
57+
}
58+

0 commit comments

Comments
 (0)