-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_oct18a_simon.ino
144 lines (119 loc) · 2.67 KB
/
sketch_oct18a_simon.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
const int PIN_LIGHT[] = {6, 8, 10, 12};
const int PIN_SWITCH[] = {5, 7, 9, 11};
int *sequence;
int length;
int position;
int gameState;
bool inputBreak;
void setup() {
int i;
for(i = 0; i < sizeof(PIN_LIGHT)/sizeof(int); i++) {
pinMode(PIN_LIGHT[i], OUTPUT);
}
for (i = 0; i < sizeof(PIN_SWITCH)/sizeof(int); i++) {
pinMode(PIN_SWITCH[i], INPUT_PULLUP);
}
// leave 0 unused.
randomSeed(analogRead(0));
length = 2;
gameState = 0;
inputBreak = true;
}
void loop() {
// put your main code here, to run repeatedly:
if (gameState == 0) {
flashAll();
generateSequence();
displaySequence();
position = 0;
gameState = 1;
}
else if (gameState == 1) {
// Playback
checkState();
}
}
void generateSequence() {
sequence = (int *) malloc(sizeof(int)*length);
for(int i = 0; i < length; i++) {
sequence[i] = random(sizeof(PIN_LIGHT)/sizeof(int));
}
}
void displaySequence() {
for(int i = 0; i < length; i++) {
digitalWrite(PIN_LIGHT[sequence[i]], HIGH);
delay(1000);
digitalWrite(PIN_LIGHT[sequence[i]], LOW);
delay(100);
}
}
void checkState() {
/* Check if a read matches the current position. */
int win = sequence[position];
int found = -1;
for(int i = 0; i < sizeof(PIN_SWITCH)/sizeof(int); i++) {
if (digitalRead(PIN_SWITCH[i]) == LOW) {
found = i;
}
}
/*
Okay.
So, inputbreak = true;
if found, inputbreak = false;
*/
if (found > -1 && inputBreak) {
inputBreak = false;
if (found == win) {
// Win condition
if (position < length - 1) {
position = position + 1;
} else {
length = length + 1;
gameState = 0;
showWin();
}
} else if (found != -1) {
// Mistake
length = 2;
gameState = 0;
showLose();
}
} else if (found == -1) {
inputBreak = true;
}
delay(10);
}
void flashAll() {
int i;
for(i = 0; i < sizeof(PIN_LIGHT)/sizeof(int); i++) {
digitalWrite(PIN_LIGHT[i], HIGH);
}
delay(2000);
for(i = 0; i < sizeof(PIN_LIGHT)/sizeof(int); i++) {
digitalWrite(PIN_LIGHT[i], LOW);
}
delay(500);
}
void showWin() {
for(int j = 0; j < 3; j++) {
for(int i = 0; i < sizeof(PIN_LIGHT)/sizeof(int); i++) {
digitalWrite(PIN_LIGHT[i], HIGH);
delay(75);
digitalWrite(PIN_LIGHT[i], LOW);
delay(25);
}
}
}
void showLose() {
int i;
for(int j = 0; j < 3; j++) {
for(int i = 0; i < sizeof(PIN_LIGHT)/sizeof(int); i++) {
digitalWrite(PIN_LIGHT[i], HIGH);
}
delay(200);
for(int i = 0; i < sizeof(PIN_LIGHT)/sizeof(int); i++) {
digitalWrite(PIN_LIGHT[i], LOW);
}
delay(100);
}
}