-
Notifications
You must be signed in to change notification settings - Fork 1
/
lights.ino
246 lines (210 loc) · 5.81 KB
/
lights.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "MovingAverageFilter.h"
#define N 3
#define NUM_INPUTS 9 // 6 on the front + 12 on the back
#define MIN_THRESHOLD 333
#define MAX_THRESHOLD 666
#define COIN_THRESHOLD 111
#define HINT_TIMEOUT 4000
#define BLINK_TIMEOUT 500
#define BLINK_TIMES 3
float inputs[NUM_INPUTS];
bool pressed[NUM_INPUTS];
bool newLevelPressed = false; // We don't need this because we have a delay.
int coinPinVal;
MovingAverageFilter movingAverageFilters[NUM_INPUTS];
// A0 – D18
// A1 – D19
// A2 – D20
// A3 – D21
// A4 – D22
// A5 – D23
// A6 – D4
// A7 – D6
// A8 – D8
// A9 – D9
// A10 – D10
// A11 – D12
const int inPinNumbers[NUM_INPUTS] = {
A0, A3, A6, A1, A4, A7, A2, A5, A8 // basically first 9 analog pins for the 9 touch wires
};
const int outPinNumbers[NUM_INPUTS] = {
// 0, 1, 2, 3, 5, 7, 9, 10, 11 // basically first unused 9 digital pins for the 9 lights
0, 3, 9, 1, 5, 10, 2, 7, 11
};
const int newLevelPin = 13; // Digital.
const int coinPin = A11; // Analog 11 is actually Digital 12.
bool level[N][N];
bool state[N][N];
bool solution[N][N];
bool inBoard(int row, int col) {
return row >= 0 && row < N && col >= 0 && col < N;
}
bool complete() {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (!state[row][col]) {
return false;
}
}
}
return true;
}
void turnOffAllLights() {
for (int i = 0; i < NUM_INPUTS; i++) {
digitalWrite(outPinNumbers[i], LOW);
}
}
void turnLights(bool matrix[N][N]) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
int lightIndex = row * N + col;
digitalWrite(outPinNumbers[lightIndex], (matrix[row][col] ? HIGH : LOW));
}
}
}
void flip(int row, int col) {
if (inBoard(row, col)) {
state[row][col] = !state[row][col];
}
}
void logMatrix(bool matrix[N][N], String header) {
Serial.println(header);
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
Serial.print(matrix[row][col] ? "1" : "0");
}
Serial.println();
}
Serial.println();
}
void click(int row, int col) {
Serial.println("You clicked " + String(row) + ", " + String(col));
//logMatrix(level, "Level Before Click:");
//logMatrix(state, "State Before Click:");
flip(row, col);
flip(row - 1, col);
flip(row + 1, col);
flip(row, col - 1);
flip(row, col + 1);
turnLights(state);
if (complete()) { // Blink all lights.
for (int i = 0; i < BLINK_TIMES; i++) {
delay(BLINK_TIMEOUT);
turnOffAllLights();
delay(BLINK_TIMEOUT);
turnLights(state);
}
}
//logMatrix(state, "State After Click:");
}
void newLevel() {
int randNumber = random(1 << (N * N));
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
state[row][col] = level[row][col] = (randNumber % 2 == 1);
randNumber >>= 1;
}
}
turnLights(state);
logMatrix(level, "New Level!");
}
void solveCorner(int row, int col) {
solution[row][col] = (!state[row][col] + !state[row][2 - col] + !state[2 - row][col] + !state[1][2 - col] + !state[2 - row][1]) % 2 == 1;
}
// Magic. Don't touch.
// Assumes N==3!
void solve() {
solveCorner(0, 0);
solveCorner(0, 2);
solveCorner(2, 0);
solveCorner(2, 2);
solution[0][1] = (!state[1][1] + !state[2][0] + !state[2][1] + !state[2][2]) % 2 == 1;
solution[1][0] = (!state[1][1] + !state[0][2] + !state[1][2] + !state[2][2]) % 2 == 1;
solution[1][2] = (!state[1][1] + !state[0][0] + !state[1][0] + !state[2][0]) % 2 == 1;
solution[2][1] = (!state[1][1] + !state[0][0] + !state[0][1] + !state[0][2]) % 2 == 1;
solution[1][1] = (!state[0][1] + !state[1][0] + !state[1][1] + !state[1][2] + !state[2][1]) % 2 == 1;
}
void showHint() {
solve(); // Update solution[][] matrix.
// Blink hint
for (int i = 0; i < BLINK_TIMES; i++) {
turnLights(solution); // Show the solution.
delay(BLINK_TIMEOUT);
turnOffAllLights();
delay(BLINK_TIMEOUT);
}
turnLights(state); // Hide the solution and show the state back.
}
void intializeHardware() {
/* Set up input pins
DEactivate the internal pull-ups, since we're using external resistors */
for (int i = 0; i < NUM_INPUTS; i++)
{
pinMode(inPinNumbers[i], INPUT);
pinMode(outPinNumbers[i], OUTPUT);
pressed[i] = false;
inputs[i] = 1023;
}
// pinMode(testLed, OUTPUT);
// digitalWrite(testLed, LOW);
pinMode(newLevelPin, INPUT);
pinMode(coinPin, INPUT);
}
void initializeGame() {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
level[row][col] = false;
state[row][col] = false;
}
}
randomSeed(analogRead(coinPin));
}
void setup() {
intializeHardware();
initializeGame();
turnOffAllLights();
}
int count = 0;
void loop() {
count++;
// New Level.
if (!newLevelPressed && digitalRead(newLevelPin)) {
newLevelPressed = true;
newLevel();
return;
}
newLevelPressed = (digitalRead(newLevelPin) == HIGH);
// Hint.
coinPinVal = analogRead(coinPin);
if (count % 50 == 0){
Serial.print(coinPinVal);
Serial.print(" ");
}
if (coinPinVal > COIN_THRESHOLD) {
showHint();
return;
}
// Gameplay.
for (int i = 0; i < NUM_INPUTS; i++) {
// Filter input for noise reduction.
inputs[i] = movingAverageFilters[i].process(analogRead(inPinNumbers[i]));
if (count % 50 == 0) {
Serial.print(inputs[i]);
Serial.print(" ");
}
if (inputs[i] < MIN_THRESHOLD) { // Pressed a button. Call click().
if (!pressed[i]) {
pressed[i] = true;
// advance game state
click(i / N, i % N);
}
} else if (inputs[i] > MAX_THRESHOLD) { // Released the button. We currently don't do anything in this case.
if (pressed[i]) {
pressed[i] = false;
}
}
}
if(count %50 == 0) {
Serial.println();
}
}