-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton-sequence.ino
147 lines (132 loc) · 3.65 KB
/
button-sequence.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* BUTTON SEQUENCE PUZZLE
*
* Push buttons in the correct order to:
* - Turn on LED
* - Play a melody on speaker
* - Open a magnetic lock
*
* by Trapped Room Escape
*/
// LED and speaker pins
const int LOCK = 9;
const int LED = 13;
const int SPEAKER = 3;
// Enter number of buttons and their pins here
const int BUTTONS[5] = {4,5,6,7,8};
// Enter correct sequence length and button pins
const int SEQUENCE[5] = {6,8,7,4,5};
/*
* Don't edit below this
*/
const int PRESSED_STATE = LOW;
const int BUTTONS_LENGTH = sizeof(BUTTONS)/sizeof(BUTTONS[0]);
const int SEQUENCE_LENGTH = sizeof(SEQUENCE)/sizeof(SEQUENCE[0]);
bool complete = false;
int lastPressedButton;
int input[SEQUENCE_LENGTH];
int counter;
void setup() {
// Init serial (for testing)
Serial.begin(9600);
// Init outputs: Lock, LED and speaker
pinMode(LOCK, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(SPEAKER, OUTPUT);
// Init button pins (with internal pull-up resistors enabled)
int i;
for ( i=0; i<BUTTONS_LENGTH; i++ ) {
pinMode(BUTTONS[i], INPUT_PULLUP);
}
// Activate lock on startup
digitalWrite(LOCK, HIGH);
}
void loop() {
// Once puzzle is completed, do nothing
if ( !complete ) {
// Allow input (0-9) thru serial monitor for testing
int serialInput;
if ( Serial.available() > 0 ) {
serialInput = Serial.read();
if ( serialInput >= '0' && serialInput <= '9' ) {
serialInput = serialInput - 48;
}
}
// Check if a button was pressed
int pressedButton = -1;
int i;
for ( i=0; i<BUTTONS_LENGTH; i++ ) {
if ( digitalRead(BUTTONS[i]) == PRESSED_STATE || serialInput == BUTTONS[i] ) {
// Consecutive presses of the same button are ignored
if ( lastPressedButton != BUTTONS[i] ) {
lastPressedButton = BUTTONS[i];
pressedButton = BUTTONS[i];
}
}
}
// Only do stuff if a button press was detected
if ( pressedButton > -1 ) {
int correct = 0;
int i;
// Create input array of button press history
// Also check if entries match the correct sequence
for ( i=0; i<SEQUENCE_LENGTH-1; i++ ) {
// Push entries left and check them
input[i] = input[i+1];
if ( input[i] == SEQUENCE[i] ) { correct++; }
}
// Insert current entry at end of array and check it
input[SEQUENCE_LENGTH-1] = pressedButton;
if ( input[SEQUENCE_LENGTH-1] == SEQUENCE[SEQUENCE_LENGTH-1] ) { correct++; }
// Print stuff to serial
Serial.print("Input history: ");
for ( i = 0; i < SEQUENCE_LENGTH; i++ ) {
Serial.print(input[i], DEC);
}
Serial.println();
// If all entries are correct
if ( correct == SEQUENCE_LENGTH ) {
// Print success message to serial
Serial.println();
Serial.println("CODE CORRECT!");
Serial.println();
// Reset correct counter and trigger completion
correct = 0;
complete = true;
success();
}
// If sequence isn't correct, make sure LED is off
else {
digitalWrite(LED, LOW);
}
// Increment button press counter
counter++;
}
}
}
void success() {
digitalWrite(LED, HIGH);
playSound();
openLock();
delay(4000);
digitalWrite(LED, LOW);
}
void playSound() {
Serial.println("Play sound");
const int TIME = 150;
tone(SPEAKER, 880, TIME);
delay(TIME);
tone(SPEAKER, 1109, TIME);
delay(TIME);
tone(SPEAKER, 1319, TIME);
delay(TIME);
tone(SPEAKER, 1760, 2*TIME);
delay(2*TIME);
tone(SPEAKER, 1661, TIME);
delay(TIME);
tone(SPEAKER, 1760, 4*TIME);
}
void openLock() {
Serial.println("Open lock");
digitalWrite(LOCK, LOW);
}