File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments