1#include <Keypad.h>
2#include <Password.h>
3
4String newPasswordString;
5char newPassword[6];
6
7Password password = Password( "0248163264" );
8
9byte maxPasswordLength = 10;
10byte currentPasswordLength = 0;
11const byte ROWS = 4;
12const byte COLS = 4;
13
14boolean Alpha = false;
15
16char keys[ROWS][COLS] =
17{
18 {'0', '4', '8', 'C'},
19 {'1', '5', '9', 'D'},
20 {'2', '6', 'A', 'E'},
21 {'3', '7', 'B', 'F'}
22};
23
24byte rowPins[ROWS] = {6, 7, 8, 9};
25
26byte colPins[COLS] = {2, 3, 4, 5};
27
28Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
29
30void setup()
31{
32 pinMode(11, OUTPUT);
33 digitalWrite(11, LOW);
34 Serial.begin(9600);
35
36}
37
38void loop()
39{
40 char key = keypad.getKey();
41 if (key != NO_KEY)
42 {
43 delay(60);
44 switch (key)
45 {
46 case 'A':
47 Alpha = false;
48 Serial.println("Console goes OFF state");
49 break;
50 case 'B': break;
51 case 'C': break;
52 case 'D': break;
53 case 'E': checkPassword(); break;
54 case 'F': resetPassword(); break;
55 default: processNumberKey(key);
56 }
57 switch (Alpha)
58 {
59 case true : Motor__ON(); break;
60 case false : Motor__OFF(); break;
61 default: processNumberKey(key);
62 }
63 }
64}
65
66void processNumberKey(char key) {
67 Serial.print(key);
68 currentPasswordLength++;
69 password.append(key);
70 if (currentPasswordLength == maxPasswordLength) {
71 checkPassword();
72 }
73}
74
75void checkPassword() {
76 if (password.evaluate()) {
77 Serial.println(" OK.");
78 Alpha = true;
79 } else {
80 Serial.println(" Wrong passwowrd!");
81 Alpha = false;
82 }
83 resetPassword();
84}
85
86void resetPassword() {
87 password.reset();
88 currentPasswordLength = 0;
89}
90void Motor__ON()
91{
92 digitalWrite(11, HIGH);
93}
94void Motor__OFF()
95{
96 digitalWrite(11, LOW);
97}
98